AlarmManager 启动应用程序而不仅仅是发送通知

发布于 2024-10-12 16:33:00 字数 2346 浏览 6 评论 0原文

我目前有一个应用程序,当按下按钮时,在一段时间后,会设置状态栏通知。

一切正常,除了如果用户没有打开应用程序,当通知出现时,应用程序也会重新打开。这不是我希望发生的事情。我希望通知单独出现(无论用户在哪里)。

按我的按钮,我使用:

    // get a Calendar object with current time
Calendar cal = Calendar.getInstance();
// add minutes to the calendar object
cal.add(Calendar.SECOND, time);
Intent alarmintent = new Intent(getApplicationContext(), AlarmReceiver.class);
alarmintent.putExtras(bundle);
// In reality, you would want to have a static variable for the request code instead of 192837
PendingIntent sender = PendingIntent.getBroadcast(getApplicationContext(), HELLO_ID, alarmintent, 0);
// Get the AlarmManager service
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);

这调用我的 AlarmReceiver.class,它使用此代码调用我的通知类:

        Intent myIntent = new Intent(context, Note.class);
    myIntent.putExtras(bundle2);


     myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);

    context.startActivity(myIntent);

notification.class:

public class Note extends Activity {




    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        String ns = Context.NOTIFICATION_SERVICE;
        final NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

        int icon = R.drawable.launcher;
        CharSequence tickerText = "Remind Me!";
        long when = System.currentTimeMillis();

        final Notification notification = new Notification(icon, tickerText, when);

    notification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_NO_CLEAR;

    final Context context = getApplicationContext();
    CharSequence contentTitle = "Remind Me!";
    CharSequence contentText = param1;

    Intent notificationIntent = new Intent(context, Completed.class);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);


            mNotificationManager.notify(HELLO_ID, notification);

            HELLO_ID++;


         }
}

I currently have an app which, when a button is pressed, and after a certain period of time, a statusbar notification is set.

Everything works fine apart from the fact if the user does not have the application open, when the notification appears, the app also reopens. This is not what i would like to happen. I would like the notification to appear on it's own (wherever the user is).

On my button press i use:

    // get a Calendar object with current time
Calendar cal = Calendar.getInstance();
// add minutes to the calendar object
cal.add(Calendar.SECOND, time);
Intent alarmintent = new Intent(getApplicationContext(), AlarmReceiver.class);
alarmintent.putExtras(bundle);
// In reality, you would want to have a static variable for the request code instead of 192837
PendingIntent sender = PendingIntent.getBroadcast(getApplicationContext(), HELLO_ID, alarmintent, 0);
// Get the AlarmManager service
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);

This calls my AlarmReceiver.class, which uses this code to call my notification class:

        Intent myIntent = new Intent(context, Note.class);
    myIntent.putExtras(bundle2);


     myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);

    context.startActivity(myIntent);

notification.class:

public class Note extends Activity {




    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        String ns = Context.NOTIFICATION_SERVICE;
        final NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

        int icon = R.drawable.launcher;
        CharSequence tickerText = "Remind Me!";
        long when = System.currentTimeMillis();

        final Notification notification = new Notification(icon, tickerText, when);

    notification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_NO_CLEAR;

    final Context context = getApplicationContext();
    CharSequence contentTitle = "Remind Me!";
    CharSequence contentText = param1;

    Intent notificationIntent = new Intent(context, Completed.class);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);


            mNotificationManager.notify(HELLO_ID, notification);

            HELLO_ID++;


         }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

¢蛋碎的人ぎ生 2024-10-19 16:33:00

这是预期的结果,因为您在 AlarmReceiver 类中创建的 Intent 显式启动您的 Note Activity。

为什么不在 AlarmReceiver 中简单地创建通知呢? (而不是启动您的活动)

This is the expected result, as the Intent you create in your AlarmReceiver class explicitly launches your Note Activity.

Why not simply create the Notification in your AlarmReceiver? (rather than launching your activity)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文