2

我正在开发一个用户可以登录登录活动的应用程序。登录用户可以在仪表板活动中查看他们的评论。如果有任何新评论进入服务器,它将推送通知。一切都运行完美除了一件事,即当用户单击通知时,如果仪表板活动在前面,它每次都会打开一个新的仪表板活动。

我想要的是,如果用户单击通知,它将仅在应用程序未运行时打开仪表板活动。否则,如果仪表板活动在前面,那么如果用户单击它,它将首先关闭仪表板活动,然后重新打开活动页面。这是我为进行仪表板活动而编写的代码。

    Intent startActivityIntent = new Intent(this, DashboardActivity.class); 
    startActivityIntent.putExtra("NotificationMessage", service_notification);
    startActivityIntent.putExtra("flag", 1);
    startActivityIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK| Intent.FLAG_ACTIVITY_NEW_TASK );
    PendingIntent launchIntent = PendingIntent.getActivity(this, 0,
            startActivityIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    earthquakeNotificationBuilder
                .setContentIntent(launchIntent)
                .setContentTitle(no_of_review + " " + "new review is there")
                .setAutoCancel(true);   

我尝试了很多东西,但没有得到解决方案。谁能帮我?提前致谢。

4

3 回答 3

3

并且android:launchMode="singleTask"在您的 androidmanifest.xml 中,这样您就可以 anddandroid:clearTaskOnLaunch="true

        <activity
            android:name=".DashboardActivity"
            android:launchMode="singleTask"
            android:clearTaskOnLaunch="true" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
于 2013-04-26T05:54:54.903 回答
1

如果打开此代码将重新打开活动,否则应用程序将重新启动

        ActivityManager am = (ActivityManager)context.getSystemService(context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
        ActivityManager.RunningTaskInfo task = tasks.get(0); // get current task
        ComponentName rootActivity = task.baseActivity;

        Intent notificationIntent;
        if(rootActivity.getPackageName().equalsIgnoreCase("your package name")){
            //your app is open
            // Now build an Intent that will bring this task to the front
             notificationIntent = new Intent();
            notificationIntent.setComponent(rootActivity);
            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);

        }
        else
        {
//your app is not open,start it by calling launcher activity
             notificationIntent = new Intent(context, SplashActivity.class);


        }
        notificationIntent.setAction(Intent.ACTION_MAIN);
        notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
         notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);





        PendingIntent intent = PendingIntent.getActivity(context, 0,
                notificationIntent, 0);

//use this pending intent in notification..

在此处查看完整示例..

于 2015-02-26T09:43:35.530 回答
0

您所做的是每次用户单击通知时都会创建一个新活动。现在您要做的就是将此FLAG_ACTIVITY_CLEAR_TOP添加到您的意图中,并在ManifestFile中更改您的活动的启动模式。还覆盖DashboardActivity中的OnNewIntent()方法

这是一些可以帮助您的代码

startActivityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

并像这样更改您的 ManifestFile

 <activity 
  android:name=".DashboardActivity"
  android:launchMode="singleTop">
 </activity>

然后像这样在您的仪表板活动中覆盖 OnNewIntent 方法

@Override
    protected void onNewIntent(Intent intent) {

        DashboardActivity.this.finish();

        // -----Start New Dashboard  when notification is clicked----

        Intent i = new Intent(DashboardActivity.this, DashboardActivity.class);

        startActivity(i);

        super.onNewIntent(intent);
    }

只要您的活动存在并单击通知,就会触发 OnNewIntent 方法

如果您希望您的活动在不同情况下表现不同,也请研究启动模式。

于 2013-04-26T06:07:57.020 回答