AlarmManager开始只是发送通知的应用程序,而不是 [英] AlarmManager starting application instead of just sending notification

查看:123
本文介绍了AlarmManager开始只是发送通知的应用程序,而不是的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有一个应用程序,当pressed一个按钮,并在一定的时间段之后,一个状态通知被设置

一切正常,不同之处在于,如果用户没有在应用程序中打开时,会出现通知时,应用程序也重新开放。这不是我想的事情发生。我想通知出现在它自己(无论用户)。

在我的按钮preSS我使用:

  //获取当前时间日历对象
日历CAL = Calendar.getInstance();
//添加分钟到日历对象
cal.add(Calendar.SECOND,时间);
意图alarmintent =新的意图(getApplicationContext(),AlarmReceiver.class);
alarmintent.putExtras(包);
//在现实中,你会希望有请求code一个静态变量,而不是192837
PendingIntent发件人= PendingIntent.getBroadcast(getApplicationContext(),HELLO_ID,alarmintent,0);
//获取AlarmManager服务
AlarmManager AM =(AlarmManager)getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),发送方);
 

这需要我AlarmReceiver.class,使用这种code打电话给我通知类:

 意图myIntent =新的意图(背景下,Note.class);
    myIntent.putExtras(bundle2中);


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

    context.startActivity(myIntent);
 

notification.class:

 公共类注延伸活动{




    / **第一次创建活动时调用。 * /
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);

        字符串NS = Context.NOTIFICATION_SERVICE;
        最后NotificationManager mNotificationManager =(NotificationManager)getSystemService(NS);

        INT图标= R.drawable.launcher;
        CharSequence的tickerText =提醒我!;
        时长= System.currentTimeMillis的();

        最后通知的通知=新的通知(图标,tickerText时);

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

    最终的上下文语境= getApplicationContext();
    CharSequence的contentTitle =提醒我!;
    CharSequence的contentText =参数1;

    意图notificationIntent =新的意图(背景下,Completed.class);
    PendingIntent contentIntent = PendingIntent.getActivity(上下文,0,notificationIntent,0);

    notification.setLatestEventInfo(背景下,contentTitle,contentText,contentIntent);


            mNotificationManager.notify(HELLO_ID,通知);

            HELLO_ID ++;


         }
}
 

解决方案

这是预期的结果,正如你在AlarmReceiver类中创建的意图明确地启动你的注意活动

为什么就不能创建在AlarmReceiver通知? (而不是启动您的活动)

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++;


         }
}

解决方案

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)

这篇关于AlarmManager开始只是发送通知的应用程序,而不是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆