简体   繁体   English

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

[英]Avoid re-create activity when click on notification in android

I am now working on a notification reminder, if the user click on the notification , it will open the app. 我现在正在处理通知提醒,如果用户单击通知,它将打开应用程序。

However, the problem is , if I have already open the app, when I click on the notification it still create a new activity, so , there are two activity in total. 但是,问题是,如果我已经打开该应用程序,则在单击通知时它仍会创建一个新活动,因此,总共有两个活动。

How can I achieve like this? 我该如何实现? If the user has already open the app , just do nothing, if not , then open the app. 如果用户已经打开了该应用程序,则什么也不做,否则请打开该应用程序。 Thanks for helping. 感谢您的帮助。

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

}

Try below code. 尝试下面的代码。

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

    }

Thats it... 而已...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Android的Orintation之后不重新创建活动? - How NOT to re-create activity after orintation in android? Android活动需要恢复或不重新显示 - Android activity need to resume or display not to re-create 重新创建主题更改活动 - Re-create an Activity on Theme change Android-按返回按钮,然后返回,但不想重新创建活动 - Android - press back button button, then return, but don't want re-create activity 我如何获得重新启动但不重新创建的活动? - how do i get an activity to restart but not re-create? 每次访问选项卡时如何重新创建活动 - How to re-create activity each time the tab is visited 重新确定Activity时如何确定片段还原? - What to determine a Fragment restore upon Activity re-create? 启动应用程序时如何从Android删除SQLite数据库中的重新创建表? - How to delete re-create tables in a SQLite database from Android when an application is started? Android SQLite - 删除并重新创建具有相同表名的表时列错误 - Android SQLite - Wrong column when drop and re-create table with same table name 有没有一种方法可以防止在使用Android导航组件时在bottomNavigation中重新创建片段 - Is there a way to prevent re-create fragment in bottomNavigation when using android navigation component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM