0

我正在通过扩展 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();

} 
}

我该如何解决我的问题?

4

1 回答 1

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();

    }
}

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

于 2012-10-10T09:19:40.880 回答