android代码打开数据,java – Android使用代码打开/关闭移动数据

我试图解决一个问题,我必须禁用,然后启用移动数据之间的一些延迟(重置移动数据2G).

第1步:禁用移动数据

第2步:等到移动数据被禁用

第3步:有些延迟说2秒

第4步:启用移动数据

第5步:等到移动数据启用

第6步:继续执行程序…..

做了一些研究我想出了这个……

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Button start = (Button)findViewById(R.id.button1);

start.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View arg0) {

// TODO Auto-generated method stub

if(!mobileDataEnabled(getApplicationContext())){

setMobileDataEnabled(getApplicationContext(),true);

Toast.makeText(getApplicationContext(), "ENABLED", Toast.LENGTH_SHORT).show();

}else{

setMobileDataEnabled(getApplicationContext(),false);

Toast.makeText(getApplicationContext(), "DISABLED", Toast.LENGTH_SHORT).show();

}

}

});

}

//the method below enables/disables mobile data depending on the Boolean 'enabled' parameter.

private void setMobileDataEnabled(Context context, boolean enabled) {

final ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

Class conmanClass = null;

try {

conmanClass = Class.forName(conman.getClass().getName());

final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");

iConnectivityManagerField.setAccessible(true);

final Object iConnectivityManager = iConnectivityManagerField.get(conman);

final Class iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());

final Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);

setMobileDataEnabledMethod.setAccessible(true);

setMobileDataEnabledMethod.invoke(iConnectivityManager, enabled);

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (NoSuchFieldException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IllegalAccessException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IllegalArgumentException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (NoSuchMethodException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (InvocationTargetException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

// below method returns true if mobile data is on and vice versa

private boolean mobileDataEnabled(Context context){

boolean mobileDataEnabled = false; // Assume disabled

ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

try {

Class cmClass = Class.forName(cm.getClass().getName());

Method method = cmClass.getDeclaredMethod("getMobileDataEnabled");

method.setAccessible(true); // Make the method callable

// get the setting for "mobile data"

mobileDataEnabled = (Boolean)method.invoke(cm);

} catch (Exception e) {

// Some problem accessible private API

// TODO do whatever error handling you want here

}

return mobileDataEnabled;

}

上面的代码将打开/关闭移动数据,但它发生得非常快.这很快,移动数据实际上甚至没有关闭.如何在两者之间添加延迟并实现上面提到的步骤?任何帮助,将不胜感激.

谢谢!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值