单击android中的通知时,避免重新创建活动

时间:2014-07-22 08:34:15

标签: android android-activity android-service android-notifications

我正在制作通知提醒,如果用户点击通知,则会打开该应用。

然而,问题是,如果我已经打开了应用程序,当我点击通知时它仍会创建一个新活动,因此,总共有两个活动。

我怎样才能达到这个目的?如果用户已经打开了应用程序,则不执行任何操作,如果没有,则打开应用程序。谢谢你的帮助。

public class AlarmService extends Service {
    private static final int MY_NOTIFICATION_ID = 1;
    NotificationManager notificationManager;
    Notification myNotification;
    Vibrator v;

    public AlarmService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onStart(Intent intent, int startId) {
        v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        v.vibrate(5000);

        Context context = getApplicationContext();
        Intent myIntent = new Intent(this, SplashScreen.class);
        myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pendingIntent =
                PendingIntent.getActivity(this, 0, myIntent, Intent.FLAG_ACTIVITY_SINGLE_TOP);

        myNotification = new Notification.Builder(context)
                .setContentTitle(getResources().getString(R.string.notify_title))
                .setContentText(getResources().getString(R.string.notify_msg) + "\n" 
                        + getResources().getString(R.string.reminder_1) + "aaa\n"
                        + getResources().getString(R.string.reminder_2) + "vvv\n"
                        + getResources().getString(R.string.reminder_3) + "cccc\n"
                        + getResources().getString(R.string.reminder_5) + "dddd\n")
                .setContentIntent(pendingIntent)
                .setDefaults(Notification.DEFAULT_SOUND).setAutoCancel(false)
                .setSmallIcon(R.drawable.ic_launcher).build();

        notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(MY_NOTIFICATION_ID, myNotification);
    }

}

1 个答案:

答案 0 :(得分:1)

尝试以下代码。

public static  void startNotification(Service service,  String message) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(service);
        if(prefs.getBoolean("pref_NotificationDisplayed", true)){
            // Creates an Ongoing Notification
            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(service.getApplicationContext()).setSmallIcon(R.drawable.ic_launcher).setContentTitle("Title").setContentText(message);

            Intent toLaunch = new Intent(service.getApplicationContext(),MainActivity.class);
            toLaunch.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
            PendingIntent intentBack = PendingIntent.getActivity(service.getApplicationContext(),   0,toLaunch, PendingIntent.FLAG_UPDATE_CURRENT);

            //PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
            mBuilder.setContentIntent(intentBack);
            NotificationManager mNotificationManager = (NotificationManager) service.getSystemService(Context.NOTIFICATION_SERVICE);

            // Send Notification
            Notification primaryNotification = mBuilder.build();
            primaryNotification.flags = Notification.FLAG_ONGOING_EVENT;
            mNotificationManager.notify(10001,primaryNotification);
        }

    }

多数民众赞成......