Tuesday, December 11, 2012

[android-developers] How stop alarm manager and call cancel


 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(
savedInstanceState);
        setContentView(R.layout.activity_main);
       
        sendBroadcast(new Intent(this,MyScheduleReceiver.class));//Start Repeat
   
    }

    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.ALARM_SERVICE);
    Intent i = new Intent(context, MyStartServiceReceiver.class);
    PendingIntent pending = PendingIntent.getBroadcast(context, 0, i,
        PendingIntent.FLAG_CANCEL_CURRENT);
    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(AlarmManager.RTC_WAKEUP,
        cal.getTimeInMillis(), REPEAT_TIME, pending);

    // service.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
    // 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("http://www.power7.net/LEDstate.txt"));
    service.putExtra("urlpath", "http://www.power7.net/LEDstate.txt");  
    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.ALARM_SERVICE);
    Intent i = new Intent(context, MyStartServiceReceiver.class);
    PendingIntent pending = PendingIntent.getBroadcast(context, 0, i,
        PendingIntent.FLAG_CANCEL_CURRENT);
    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