无法设置适当的上下文来取消警报管理器

编程入门 行业动态 更新时间:2024-10-15 04:26:32
本文介绍了无法设置适当的上下文来取消警报管理器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我创建了一个带有通知的警报和一个用于停止警报的按钮,但是 Context 似乎有一些问题,我必须这样做取消 AlarmManager 。

I have created an alarm with a notification and a button to stop the alarm but it seems that there is some issue with the Contextthat I have to get in order to cancel the AlarmManager.

我进行了很多研究,我认为我已经应用了发现的大部分内容,但无法以某种方式使它起作用。

I have researched a lot and I think I have applied most of the things I found but somehow can't get this to work.

我知道 PendingIntent 必须相同,在我看来它们的请求代码相同太。

I am aware the PendingIntent has to be the same and it appears to me they are with the same request codes too.

AlarmSlave.class-设置警报

AlarmSlave.class - Sets Alarm

public class AlarmSlave /*extends Service*/ { private static final String STOP_ACTION = "STOP"; private int hourOfDay; private int minuteOfHour; private AlarmManager alarmManager; private PendingIntent pendingIntent; public AlarmSlave() { } AlarmSlave(int hour, int minute) { hourOfDay = hour; minuteOfHour = minute; } public void setAlarm(Context context) { alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); /*Intent intent = new Intent(context, AlarmReceiver.class);*/ pendingIntent = PendingIntent.getBroadcast(context, 327, new Intent(context, AlarmReceiver.class), 0); Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, hourOfDay); calendar.set(Calendar.MINUTE, minuteOfHour); alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); Log.d("Ctx setAlarm ", context.toString()); }

AlarmReceiver.class-播放铃声很多。

AlarmReceiver.class - Plays Ringtone Pretty Much.

public class AlarmReceiver extends WakefulBroadcastReceiver{ @Override public void onReceive(final Context context, Intent intent){ /*MainActivity inst = MainActivity.instance(); Log.d("Testing","this is here"+inst);*/ Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM); if (alarmUri == null){ alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE); } Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri); ringtone.play(); ComponentName componentName = new ComponentName(context.getPackageName(), AlarmService.class.getName()); startWakefulService(context, (intent.setComponent(componentName))); setResultCode(Activity.RESULT_OK); } }

AlarmService.class-这是显示通知的内容,内容意图运行良好,但对于动作而言却并非如此。

AlarmService.class - This is what shows the notification, the content intents are working fine but for the action they are not.

public class AlarmService extends IntentService { public static final String STOP_ACTION = "STOP"; public AlarmService() { super("AlarmService"); } @Override public void onHandleIntent(Intent intent) { sendNotification("Wake Up! Wake Up"); } private void sendNotification(String msg) { Log.d("AlarmService", "Preparing to send notification...:" + msg); NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE); //PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 327, new Intent(getApplicationContext(), AlarmActivity.class), 0); int notificationId = 1; // just use a counter in some util class... PendingIntent dismissIntent = AlarmActivity.getDismissIntent(notificationId, getApplicationContext()); /*Intent stopIntent = new Intent(this, AlarmSlave.class); stopIntent.setAction(STOP_ACTION); PendingIntent stopPendingIntent = PendingIntent.getBroadcast(this, 10, stopIntent, 0);*/ NotificationCompat.Builder alarmNotificationBuilder = new NotificationCompat.Builder(getApplicationContext()) .setContentTitle("Alarm") .setSmallIcon(R.mipmap.ic_launcher_round) .setStyle(new NotificationCompat.BigTextStyle().bigText(msg)) .setContentText(msg) .addAction(R.mipmap.ic_launcher_round, "STOP", dismissIntent); alarmNotificationBuilder.setContentIntent(dismissIntent); notificationManager.notify(1, alarmNotificationBuilder.build()); Log.d("AlarmService", "Notification Sent"); } }

ContextApp.class-这是生成上下文给我。

ContextApp.class - This is what generates Context for me.

public class ContextBitchApp extends Application { private static Context mContext; @Override public void onCreate(){ super.onCreate(); mContext = getApplicationContext(); } public static Context getContext(){ return mContext; } }

请原谅语言的命名方式,是在两天的时间里尝试了多种不同的尝试之后,完全出于沮丧而完成的。

Pardon for some of the language in the naming, done out of utter frustration after trying several different things over the period of two days.

编辑:这就是我的 Log 显示上下文

This is what my Log is showing for Context

05-13 09:37:55.208 24235-24235/<omitted> D/Ctx setAlarm: <omitted>.ContextBitchApp@7709bb7 05-13 09:41:18.760 3244-3244/<omitted>:remote D/Ctx onReceive: android.app.ReceiverRestrictedContext@2c317d9 05-13 09:41:18.760 3244-3244/<omitted>:remote D/Ctx cancelAlarm: android.app.ReceiverRestrictedContext@2c317d9

编辑:更新代码

推荐答案

上下文导致了您的问题。您绝对肯定不能这样做:

All your mucking around with Context is causing your problem. You absolutely positively cannot do this:

private static Context ctx = ContextBitchApp.getContext(); private static final Context ctxCopy = ctx;

a static 变量在上课时被初始化已加载。目前,您不知道 ContextBitchApp.getContext()将返回什么。

a static variable gets initialized when the class is loaded. At this time, you have no idea what ContextBitchApp.getContext() will return.

无论如何,您无需尝试保存 Context 等。仅在需要 Context 作为参数传递给 PendingIntent时,始终使用 getApplicationContext() .getBroadcast()。

In any case, you don't need to try to save Context and all that. Just always use getApplicationContext() when you need a Context to pass as a parameter to PendingIntent.getBroadcast().

此外,在调用 AlarmManager.cancel()之后,您还可以通过调用 cancel()来取消 PendingIntent 。

Also, after calling AlarmManager.cancel(), you can also cancel the PendingIntent by calling cancel() on it.

更多推荐

无法设置适当的上下文来取消警报管理器

本文发布于:2023-11-27 13:07:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638201.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上下文   管理器   警报

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!