我正在尝试在一天中的特定时间启用/禁用移动数据。这是我到目前为止所拥有的:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Calendar objCal = Calendar.getInstance();
objCal.set(Calendar.HOUR_OF_DAY, 11);
objCal.set(Calendar.MINUTE, 0);
objCal.set(Calendar.SECOND, 0);
//
// Intent intent = new Intent(this, MainActivity.class);
// intent.setAction("com.sang.mobiledata.ACTION");
//
PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(
this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager)
this.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, objCal.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pi);
基本上,我想在晚上 10 点关闭数据连接。我知道这将通过这一行来实现:
objNetwork.setMobileDataEnabled(getBaseContext(), false);
然后在早上 6 点再次打开它
objNetwork.setMobileDataEnabled(getBaseContext(), true);
我只是不知道如何(以及在哪里)给出这个条件。
编辑:
public class Network {
public void setMobileDataEnabled(Context context, boolean enabled) {
try {
ConnectivityManager conman = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
Method setMobileDataEnabledMethod = ConnectivityManager.class
.getDeclaredMethod("setMobileDataEnabled", boolean.class);
setMobileDataEnabledMethod.setAccessible(true);
setMobileDataEnabledMethod.invoke(conman, enabled);
} catch (Exception e) {
e.printStackTrace();
}
}
编辑2(完整代码):
ConnBroadCastReceiver.java
public class ConnBroadcastReceiver extends BroadcastReceiver {
private static final String CONN_ACTION = "com.sang.mobiledata.IntentAction.RECEIVE_CONN_UPDATE";
Network objNetwork = new Network();
@Override
public void onReceive(Context context, Intent intent) {
if(CONN_ACTION.equals(intent.getAction())) {
boolean enableConn = intent.getBooleanExtra("FLAG_KEY", false);
objNetwork.setMobileDataEnabled(context, enableConn);
}
}
}
MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
triggerEnable(true, 6);
triggerEnable(false, 22);
}
private void triggerEnable(boolean enableData, int hourInDay) {
Calendar calendar = Calendar.getInstance();
if (enableData) {
calendar.set(Calendar.HOUR_OF_DAY, hourInDay);
} else {
calendar.set(Calendar.HOUR_OF_DAY, hourInDay);
}
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
Intent broadcastIntent = new Intent("com.sang.mobiledata.IntentAction.RECEIVE_CONN_UPDATE");
broadcastIntent.putExtra("FLAG_KEY", enableData);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, broadcastIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pi);
}
网络.java
public class Network {
public void setMobileDataEnabled(Context context, boolean enabled) {
try {
ConnectivityManager conman = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
Method setMobileDataEnabledMethod = ConnectivityManager.class
.getDeclaredMethod("setMobileDataEnabled", boolean.class);
setMobileDataEnabledMethod.setAccessible(true);
setMobileDataEnabledMethod.invoke(conman, enabled);
} catch (Exception e) {
e.printStackTrace();
}
}
清单.xml
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver
android:name="com.sang.mobiledata.ConnBroadcastReceiver"
android:exported="false" >
<intent-filter>
<action android:name="com.sang.mobiledata.IntentAction.RECEIVE_CONN_UPDATE" />
</intent-filter>
</receiver>