Android:应用多个实例

时间:2016-09-09 08:03:34

标签: android android-notifications

当我在序列之后从启动器启动我的应用程序时:

  1. SplashActivity.java

    <activity
      android:name=".splash.SplashActivity"
      android:launchMode="singleTop"
      android:screenOrientation="portrait"
      android:theme="@style/SplashActivityTheme">
    <intent-filter>
      <action android:name="android.intent.action.MAIN"/>
    
      <category android:name="android.intent.category.LAUNCHER"/>
    </intent-filter>
    

  2. HomeActivity.java

  3. 重现问题的步骤:

    1. 应用程序未在后台运行,也未处于暂停状态。

    2. 收到通知并显示在状态栏上。

    3. 用户点击通知并启动了HomeActivity.java

                         

      FirebaseMessagingService.java

      private void sendNotification(String messageBody, String title) {
      
          Intent intent = new Intent(this, HomeActivity.class);
          intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
          PendingIntent pendingIntent =
              PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT);
      
          Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
          NotificationCompat.Builder notificationBuilder =
              new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_stat_ic_notification)
                  .setContentTitle(title)
                  .setContentText(messageBody)
                  .setAutoCancel(true)
                  .setSound(defaultSoundUri)
                  .setColor(ContextCompat.getColor(this, R.color.pantone_375))
                  .setContentIntent(pendingIntent);
      
          NotificationManager notificationManager =
              (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
          notificationManager.notify(++count /* ID of notification */, notificationBuilder.build());
      }
      
    4. 保持HomeActivity.java打开并从启动器启动应用程序。启动相同应用程序的另一个实例,序列为SplashActivity.java - &gt; HomeActivity.java

    5. 预期结果

      1. 应用程序的一个实例应存在于任何给定点。

      2. 如果应用程序已处于打开状态且用户在HomeActivity.java,则应该会收到焦点。

      3. 通过单击通知启动应用程序,应用程序处于打开状态。用户单击启动器图标,然后旧应用程序实例将获得焦点。

2 个答案:

答案 0 :(得分:0)

如果您在点击Activity时打开Notification,请打开以下Activity。即您的PendingIntent将在Activity

之后打开

请阅读以下comments中所写的所有Activity,以便了解为何已创建

public class NotificationHandlerActivity extends AppCompatActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //deep linking - resuming app code
        if (isTaskRoot()) {
            // This Activity is the only Activity, so
            //  the app wasn't running. So start the app from the
            //  beginning (redirect to MainActivity)
        } else {
            // App was already running, so just finish, which will drop the user
            //  in to the activity that was at the top of the task stack
            Intent intent = getIntent();
            Uri data = intent.getData();
            //you can put your extra's if any
            finish();
        }
    }
}

答案 1 :(得分:0)

据我了解,我想要开展每项活动的单一任务。如果我在这种情况下是正确的你必须使用android:launchMode =“singleInstance” 在activty AndroidManifest.xml文件中

或者查看本手册中的simular标志

https://developer.android.com/guide/topics/manifest/activity-element.html

电子。 G.

  

<activity android:name=". MainActivity" 
> 
>android:launchMode="singleInstance"label="@string/app_name">