Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop/cancel alarm manager in another activity?

I am creating alarm in Activity A & stopping/cancelling in another Activity B. I tried hard but no luck, below is my code :

MainAcitivity

public class MainActivity extends Activity 
{

    private PendingIntent pendingIntent;
    private static Context context;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent myIntent = new Intent(this, MyReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent,0);

    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 12 * 60 * 60, pendingIntent); 

    } //end onCreate

}

MyReceiver

    public class MyReceiver extends BroadcastReceiver
    {

        @Override
         public void onReceive(Context context, Intent intent)
        {
           Intent service1 = new Intent(context, MyAlarmService.class);
           context.startService(service1);

         }

    }

MyAlarmService

public class MyAlarmService extends Service 

{
     private NotificationManager mManager;

     @Override
     public IBinder onBind(Intent arg0)
     {
       // TODO Auto-generated method stub
        return null;
     }

    @Override
    public void onCreate() 
    {
       // TODO Auto-generated method stub  
       super.onCreate();
    }

   @SuppressWarnings("static-access")
   @Override
   public void onStart(Intent intent, int startId)
   {
       super.onStart(intent, startId);

       mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
       Intent intent1 = new Intent(this.getApplicationContext(),MainActivity.class);

       Notification notification = new Notification(R.drawable.ic_launcher,"This is a test message!", System.currentTimeMillis());

       intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);

       PendingIntent pendingNotificationIntent = PendingIntent.getActivity( this.getApplicationContext(),0, intent1,PendingIntent.FLAG_UPDATE_CURRENT);
       notification.flags |= Notification.FLAG_AUTO_CANCEL;
       notification.setLatestEventInfo(this.getApplicationContext(), "AlarmManagerDemo", "This is a test message!", pendingNotificationIntent);

       mManager.notify(0, notification);
    }

    @Override
    public void onDestroy() 
    {
        // TODO Auto-generated method stub
        super.onDestroy();
    }

}

NextActivity

public class NextActivity extends Activity
{

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

           Intent intentt = new Intent(this, MyReceiver.class);
           PendingIntent pintent = PendingIntent.getBroadcast(this, 10, intentt, 0);
           AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
           stopService(intentt);
           alarm.cancel(pintent);
    }   
}

Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="10" />


    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name="com.example.NextActivity"
            android:label="Next Screen">
        </activity>

        <service android:name=".MyAlarmService"
                 android:enabled="true" />

        <receiver android:name=".MyReceiver"/>

    </application>

</manifest>

It is still firing alarm, unable to stop/cancel after doing this.

like image 971
VVB Avatar asked Nov 22 '22 21:11

VVB


1 Answers

When creating your alarm, you use this PendingIntent:

Intent myIntent = new Intent(MainActivity.getAppContext(), MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 10, myIntent,0);

When you try to cancel it, you are using this PendingIntent:

Intent intentt = new Intent(MainActivity.getAppContext(), NextActivity.class);
PendingIntent pintent = PendingIntent.getService(this, 10, intentt, 0);

You need to use the same code to create the PendingIntent when you want to cancel the alarm. Use getBroadcast() instead of getService() and use MyReceiver.class instead of NextActivity.class.

Also, you don't need to save the application context in MainActivity. You can remove all this static stuff. You can just use this as the Context parameter when doing new Intent(Context, Thing.class)

like image 195
David Wasser Avatar answered Nov 24 '22 13:11

David Wasser