Android通知会不断启动应用(即使我没有点按通知)?

时间:2012-10-10 04:46:35

标签: java android notifications alarmmanager

我正在通过扩展活动来创建通知,每次通知出现时,我的应用都会继续启动。它不会每次都启动到同一个活动。相反,它会在我的应用中启动上次使用的活动。我的通知是通过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();

} 
}

如何解决我的问题?

1 个答案:

答案 0 :(得分:0)

我在经过一些小修改后尝试了你的代码,它在我的最后工作正常。以下是代码,我刚刚将活动更改为使用名为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();

    }
}

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