当前位置: 首页 > 知识库问答 >
问题:

应用程序被销毁后立即发出通知

凌宏大
2023-03-14
@Override
    public void onDestroy() {
        super.onDestroy();


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

        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
        //alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 3600*1000*4, 3600*1000*4, pendingIntent);
    }
public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent mainIntent = new Intent(context, Main.class);

        NotificationManager notificationManager
            = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        android.app.Notification noti = new NotificationCompat.Builder(context)
            .setAutoCancel(true)
            .setContentIntent(PendingIntent.getActivity(context, 0, mainIntent,
                              PendingIntent.FLAG_UPDATE_CURRENT))
            .setContentTitle("Title")
            .setContentText("It's been so Long!!!")
            .setSubText("Please return back to App & Learn more Duas.")
            .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
            .setVibrate(new long[] {700,700,700,700})
            .setSmallIcon(R.drawable.ic_launcher)
            .setTicker("Important Notification")
            .setWhen(System.currentTimeMillis())
            .build();

        notificationManager.notify(0, noti);
    }

}

共有1个答案

楚奇逸
2023-03-14

是因为小错误

只要把这行改一下

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 3600*1000*4, 3600*1000*4, pendingIntent);

对此

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, (System.currentTimeMillis()+(3600*1000*4)), 3600*1000*4, pendingIntent);
 类似资料:
  • 一调用方法,我的计时器应用程序就崩溃了,它显示的对象引用为空,尽管我做得正确,但我找不到问题。请,某人,看看下面的代码:(我也附上logcat) 原因:java.lang.NullPointerException:尝试对空对象引用调用虚拟方法“void android.os.CountDownTimer.Cancel()”

  • 问题内容: 我需要能够动态加载/卸载角度应用程序而不会引起内存泄漏。在jQuery中,您可以执行相应的销毁代码,事件处理程序未绑定等。 我一直无法在有角度的文档中找到任何内容,提及启动应用程序后可能会拆除应用程序的可能性。 我的第一次尝试是像这样破坏rootScope: 但这似乎不起作用,而且我不确定即使清除了注入器和服务也将如何清理。 应该怎么做? 问题答案: 2013年3月10日更新: 我发现

  • 我已经为这个问题挣扎了几天了 我有一个android应用程序,用户可以指定植物浇水的频率。我想根据植物浇水的频率发送通知。(例如,每3天发送一次通知) 我知道有很多问题可以解决这个问题,但我仍然无法让它发挥作用 我从使用报警管理器实现/启动服务开始 主要问题是,一旦我的应用程序被终止,通知将不再发送 我正在使用firebase存储所有数据 我读了一些关于firebase函数的内容,但我不知道这是否

  • 我有一个片段,它设置一个列表视图,并创建一个处理程序来定期更新列表视图。然而,看起来处理程序在片段被销毁后仍然运行。 以下是代码。 在被销毁后更新会导致应用程序崩溃。如何在被销毁时使停止?我还想知道如果应用程序的任何暂停对也有什么影响。

  • 我是一个蟒蛇初学者。试着做一个新的按钮来关闭窗口。我得到了错误消息: 异常在Tkinter回调Traceback(最近调用最后):文件/系统/库/框架/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py,第1536行,在调用返回self.func(*args)文件tk_cp_successful.py,第138行,在按钮推送s