【问题标题】:Android notification keeps launching app (even when I don't tap the notification)?Android 通知不断启动应用程序(即使我没有点击通知)?
【发布时间】:2012-10-10 04:46:34
【问题描述】:

我正在通过扩展 Activity 创建通知,并且每次出现通知时,我的应用都会不断启动。它不会每次都启动相同的活动。相反,它会启动我的应用程序中最后使用的活动。我的通知正在通过 AlarmManager 在特定日期/时间启动。如何阻止我的通知启动我的应用程序?我需要它在用户点击它时启动应用程序(该部分工作正常),但我不希望它在通知第一次出现在状态栏中时启动应用程序。这是我的通知代码:

public class DisplayReminderNotification extends SherlockActivity {

private Context context = this;

//Grab a handle onto the database and store the name of the reminder.
protected RemindersDAO remindersDAO;

@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //Get the notification ID.
    int notifID = getIntent().getExtras().getInt("Reminder_Primary_Key");

    String reminderName = getIntent().getExtras().getString("Reminder_Name");

    //PendingIntent stores the Activity that should be launched when the user taps the notification.
    Intent i = new Intent(this, ViewLocalRemindersDetail.class);
    i.putExtra("NotifID", notifID - randomInt);
    i.putExtra("notification_tap", true);

    displayIntent = PendingIntent.getActivity(this, notifID, i, 0);

    NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Notification notif = new Notification(R.drawable.launcher_icon, reminderName, System.currentTimeMillis());

    CharSequence from = "Here's your reminder:";

    CharSequence message = reminderName;        
    notif.setLatestEventInfo(this, from, message, displayIntent);

    //Fire up the notification.
    nm.notify(notifID, notif);

    //Destroy the activity/notification.
    finish();

} 
}

我该如何解决我的问题?

【问题讨论】:

    标签: java android notifications alarmmanager


    【解决方案1】:

    经过一些小的修改后,我尝试了您的代码,它在我的最后运行良好。以下是代码,我只是将活动更改为使用名为 MyActivity 的空白活动启动:

    public class DisplayReminderNotification extends Activity {
    
        private Context context = this;
    
        @SuppressWarnings("deprecation")
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            // Get the notification ID.
            int notifID = 1234;
    
            String reminderName = "reminder";
    
            // PendingIntent stores the Activity that should be launched when the
            // user taps the notification.
            Intent i = new Intent(this, MainActivity.class);
            i.putExtra("NotifID", 123);
            i.putExtra("notification_tap", true);
    
            PendingIntent displayIntent = PendingIntent.getActivity(this, notifID,
                    i, 0);
    
            NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            Notification notif = new Notification(R.drawable.ic_launcher,
                    reminderName, System.currentTimeMillis());
    
            CharSequence from = "Here's your reminder:";
    
            CharSequence message = reminderName;
            notif.setLatestEventInfo(this, from, message, displayIntent);
    
            // Fire up the notification.
            nm.notify(notifID, notif);
    
            // Destroy the activity/notification.
            finish();
    
        }
    }
    

    我将此作为主启动器活动,每当我启动此活动时,它只会在通知栏添加通知,而不是启动应用程序。我建议你试试这个代码是一个单独的示例应用程序。可能问题出在代码的其他部分。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多