I've never tried to toggle data connection - assuming above will work if you provide proper permissions (if any is supposed to have) and above code doesn't make use of reflection as I have seen other people are shooting themselves in the foot ...
However, if you intend to open an application activity at a specific time, then I would say that's a bad practice at least from user experience point of view. Instead of doing that, you could send a broadcast message through PendingIntent.getBroadcast(...)
.
That being said:
- Construct an intent with an action of your own, add a boolean flag inside that is meaningfull (true to enable connectivity, false otherwise).
- Pass that intent to your
PendingIntent.getBroadcast
- Create a
BroadcastReceiver
in your class, register it in the Manifest and handle properly that action. Something like:
The BroadcastReceiver class:
public class ConnBroadcastReceiver extends BroadcastReceiver {
private static final String CONN_ACTION = "my_package_name.IntentAction.RECEIVE_CONN_UPDATE";
@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);
}
}
}
register it in the Manifest:
<receiver
android:name="my_package_name.ConnBroadcastReceiver"
android:exported="false" >
<intent-filter>
<action android:name="my_package_name.IntentAction.RECEIVE_CONN_UPDATE" />
</intent-filter>
</receiver>
and trigger the broadcast from your above code:
Intent broadcastIntent = new Intent("my_package_name.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, objCal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);
EDIT Above onCreate
can be re-writen to:
@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("my_package_name.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);
}