我怎样才能获得其他活动的背景下? [英] How can I get the context of other activity?
问题描述
我正在报警的应用程序,并且有麻烦删除现有警报。我要创建相同的未决意图删除其中列出了我的应用程序的所有报警活动报警,但正如我预期它不工作。我想从我用来添加一个报警的活动得到了语境。
I'm working on the alarm app, and have trouble removing an existing alarm. I have to create the same pending intent to remove the alarm in the activity which lists all alarms in my app, but It doesn't work as I expected. I want to get the "Context" from the activity which I used to add an alarm.
下面是code从ListAddActivity添加报警。
Here is the code to add an alarm from ListAddActivity.
private void addAlarm()
{
final int ALARM_CODE = this.getAlarmCode(1);
final int HOUR_OF_DAY = vo.getHour1();
final int MINUTE = vo.getMinute1();
AlarmManager alarmManager = (AlarmManager) getApplicationContext()
.getSystemService(Context.ALARM_SERVICE);
Calendar calendar = CalendarCreator.getCalendar(HOUR_OF_DAY, MINUTE);
Intent intent = new Intent(ListAddActivity.this, PushMainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(
ListAddActivity.this, ALARM_CODE, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
pendingIntent);
}
下面是code去除ListMainActivity的预定报警。
Here is the code to remove an scheduled alarm from ListMainActivity.
private void removeAlarm(int number)
{
final int alarmCode = AlarmCodeCreator.CreateNum(1, clickedPosition, 0);
System.out.println(alarmCode);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(getApplicationContext(),
PushMainActivity.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getApplicationContext(), alarmCode, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.cancel(pendingIntent);
}
正如你所看到的,我做我自己的算法创建报警code,这样我就可以找回它,并且有相同的标志了。然而,得到同样的背景是我与挣扎的人。
As you can see, I made my own algorithm to create an alarmCode, so I can retrieve it, and had the same flag, too. However, getting the same "Context" is the one that I'm struggling with.
推荐答案
这有没有关系背景。这样做的目的,你的时间表与getActivity创建。那是一种别样的意图不是与getBroadcast创建的一种。不能使用后者取消了前者。
This has nothing to do with contexts. The intent you schedule was created with getActivity. That is a different kind of intent than the one created with getBroadcast. You cannot use the latter to cancel the former.
(编辑补充code)
Intent intent = new Intent(
getApplicationContext(),
PushMainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(
ListAddActivity.this,
ALARM_CODE,
intent,
PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.cancel(pendingIntent);
这篇关于我怎样才能获得其他活动的背景下?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!