问题描述
我试图接近中,我必须禁用,然后在两者之间有一些延迟使移动数据有问题(重置移动数据2G)。
I am trying to approach a problem in which I have to disable and then enable mobile data with some delay in between (reset mobile data 2G).
第1步:关闭移动数据
第2步:等到移动数据获取禁用
step 2: wait till mobile data gets disabled
第3步:一些延迟例如2秒。
step 3: some delay say 2 seconds
步骤4:启用移动数据
第5步:等到移动数据获取启用
step 5: wait till mobile data gets enabled
第六步:继续执行程序.....
step 6: continue with the program.....
在做一些研究,我想出了这个...
doing some research I came up with this...
@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;
}
以上code将开启/关闭移动数据,但它发生的真快。这种快速的移动数据甚至不关掉其实。我怎么在两者之间添加一个延迟,实现我上面提到的步骤是什么?任何帮助将是AP preciated。
谢谢!
The above code will turn on/off mobile data but it happens really quick. this quick that the mobile data doesn't even turn off actually. how do I add a delay in between and achieve the steps I mentioned above? any help would be appreciated.thanks!
推荐答案
只要把
Thread.sleep(1000);
在code语句之间(之前的 setMobileData STRONG>的API)来实现延迟。延迟参数为毫秒。因此,根据您的要求进行更改。
in between the code statements (before setMobileData APIs) to achieve delay. The delay parameter is in milliseconds. So change it according to your requirement.
编辑:尝试将延迟到处理程序,使用这种code:
Try putting the delay into a handler, using this code:
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//Whatever you want to do
}
}, 1000);
这篇关于Android的使用code开启/关闭移动数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!