如何从活动中获取服务上下文?

时间:2013-11-11 17:00:48

标签: android android-activity service android-context

我需要从活动中获取服务上下文,因为我在服务中创建了一个警报,我想在活动中将其删除,因此必须使用相同的上下文,我可以将此服务的上下文保存在{ {1}}作为字符串但是如何将此字符串转换为上下文?

我的闹钟代码是:

sharedpreferences

我如何删除它?

 Intent intentAlarm = new Intent(getApplicationContext(), TimeAlarm.class);            
           //add the notification body to the intent:
           intentAlarm.putExtra("Notif_body", Notif_Body);             
           // create the object
            AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
           //set the alarm for particular time
           alarmManager.set(AlarmManager.RTC_WAKEUP,new GregorianCalendar().getTimeInMillis()+60*1000, PendingIntent.getBroadcast(getApplicationContext(),1,intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));

为什么这不起作用?

1 个答案:

答案 0 :(得分:0)

  

嗨我需要从活动中获取服务上下文

不,你没有。

  

所以必须使用相同的上下文

不,你没有。

  

我可以将此服务的上下文保存为共享首选项作为字符串

不,你不能。

  

我认为需要相同的背景

不,你没有。

  

当我使用其他上下文时,我无法删除闹钟

然后您的代码还有其他问题。您可以使用完全不同的流程取消闹钟,更不用说其他Context