Android AlarmManager - Cancel from another Activity

社会主义新天地 提交于 2019-12-04 20:41:20

If it helps, you could access your context from another activity, by making a static getter for the current context. In your above class, just make a private field:

private static Context context;

public static Context getAppContext(){
    return MyActivity.context;
}

Then, simply add in the onCreate method a:

MyActivity.context = getApplicationContext();

Accessing the context from another activity is now pretty easy. You can use the context retrieved from "MyActivity" to cancel your alarm.

joe_deniable

According to this Android: Does context affect filterEquals(), used to cancel alarm? it doesn't matter if you use different contexts, this doesn't affect the recognition of the PendingIntent as matching. I have confirmed this in my own app, I set an alarm from one activity using the activity as the context, and cancelled it from a different activity using that activity's context, and it was successfully cancelled (the action wasn't triggered).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!