Android AlarmManager正在启动应用程序,而不仅仅是发送通知

Android AlarmManager正在启动应用程序,而不仅仅是发送通知,android,notifications,android-intent,alarmmanager,Android,Notifications,Android Intent,Alarmmanager,我目前有一个应用程序,当按下一个按钮时,在一段时间后,会设置一个状态栏通知 除了用户没有打开应用程序之外,一切正常,当通知出现时,应用程序也会重新打开。这不是我希望发生的事情。我希望通知自己出现(无论用户在哪里) 在我的按钮上按下我使用的按钮: // get a Calendar object with current time Calendar cal = Calendar.getInstance(); // add minutes to the calendar object cal

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

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

在我的按钮上按下我使用的按钮:

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


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

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


         }
}
通知。类:

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


         }
}

这是预期的结果,因为您在AlarmReceiver类中创建的意图会显式启动
Note
活动


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

这是预期的结果,因为您在AlarmReceiver类中创建的意图会显式启动您的
注释
活动


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

启动Notes活动很好,因为它所做的只是设置通知,对吗?问题是,如果应用程序关闭,它会再次打开看起来像主活动的内容。@Jez:否。调用
startActivity
启动活动!正如Dave所说,将通知创建代码移动到AlarmReceiver中。啊,好的。谢谢大家的帮助:-)好的,我已经尝试将通知代码移到AlarmReceiver类,但是我得到了很多语法错误,因为许多方法没有为类型BroadcastReceiver和AlarmReceiver定义。@Jez:您不能一字不差地使用通知代码。您需要为一些调用指定上下文,例如
getSystemService(ns)
应该变成
Context.getSystemService(ns)
as
broadcastReceiver
不会从
Context
继承,而
Activity
继承。启动Note活动很好,因为它所做的一切都是设置通知,对吗?问题是,如果应用程序关闭,它会再次打开看起来像主活动的内容。@Jez:否。调用
startActivity
启动活动!正如Dave所说,将通知创建代码移动到AlarmReceiver中。啊,好的。谢谢大家的帮助:-)好的,我已经尝试将通知代码移到AlarmReceiver类,但是我得到了很多语法错误,因为许多方法没有为类型BroadcastReceiver和AlarmReceiver定义。@Jez:您不能一字不差地使用通知代码。您需要为一些调用指定上下文,例如
getSystemService(ns)
应该成为
Context。getSystemService(ns)
作为
broadcastrecever
不会从
Context
继承,而
Activity
会继承。