I make an application download file repeat with alarm manager and my problem is how i call alarmmanager.cancel() with button on click for stop it.My code is...
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(
setContentView(R.layout.
sendBroadcast(new Intent(this,
}
public void stop(){
//How i call cancel alarmmanager?
}
public class MyScheduleReceiver extends BroadcastReceiver {
// Restart service every 30 seconds
private static final long REPEAT_TIME = 1000 * 30;
@Override
public void onReceive(Context context, Intent intent) {
AlarmManager service = (AlarmManager) context
.getSystemService(Context.
Intent i = new Intent(context, MyStartServiceReceiver.class);
PendingIntent pending = PendingIntent.getBroadcast(
PendingIntent.FLAG_CANCEL_
Calendar cal = Calendar.getInstance();
// Start 30 seconds after boot completed
cal.add(Calendar.SECOND, 30);
//
// Fetch every 30 seconds
// InexactRepeating allows Android to optimize the energy consumption
service.setInexactRepeating(
cal.getTimeInMillis(), REPEAT_TIME, pending);
// service.setRepeating(
// REPEAT_TIME, pending);
}
}
public class MyStartServiceReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Download repeat!",
Toast.LENGTH_LONG).show();
Intent service = new Intent(context, DownloadService.class);
//Messenger messenger = new Messenger(handler);
//intent.putExtra("MESSENGER", messenger);
service.setData(Uri.parse("htt
service.putExtra("urlpath", "http://www.mpla.net/
context.startService(service);
}
}
public class MyStopReceiver extends BroadcastReceiver {
// Restart service every 30 seconds
private static final long REPEAT_TIME = 1000 * 30;
@Override
public void onReceive(Context context, Intent intent) {
AlarmManager service = (AlarmManager) context
.getSystemService(Context.
Intent i = new Intent(context, MyStartServiceReceiver.class);
PendingIntent pending = PendingIntent.getBroadcast(
PendingIntent.FLAG_CANCEL_
Calendar cal = Calendar.getInstance();
// Start 30 seconds after boot completed
cal.add(Calendar.SECOND, 30);
service.cancel( pending);
}
}
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
No comments:
Post a Comment