【问题标题】:how to start activity when the main activity is running in background?当主要活动在后台运行时如何启动活动?
【发布时间】:2010-01-10 12:13:38
【问题描述】:

我创建了一个应用程序,使用户能够设置他是否希望在应用程序在后台模式下运行时接收通知。如果启用了通知,则应该启动一个活动(对话框应该出现在屏幕上)。

我尝试通过以下方式启用它:

@Override
public void onProductsResponse(List<Product> products) {
    this.products = products;
    moboolo.setProducts(products);
    if(moboolo.getAutomaticNotificationsMode() != 0 && products.size() > 0){
        if(isRunningInBackground)
        {
            Intent intent = new Intent(this, ProductListActivity.class);
            intent.setAction(Intent.ACTION_MAIN);
            startActivity(intent);
        }
    }
    drawProducts(products);

}

这是来自主要活动的方法。执行 onPause() 时,isRunningInBackground 设置为 true。 当我在主应用程序在后台运行时尝试调试它时,

startActivity(intent) 没有效果(活动没有出现)。

当主活动在后台运行时(在调用 onPause() 之后),有谁知道如何调整逻辑以便从主活动启动一个活动?

谢谢。

【问题讨论】:

    标签: android android-activity


    【解决方案1】:

    您不能强制Activity 在后台运行的应用程序中出现。 The documentation says:

    如果应用程序在后台运行并且需要用户注意,应用程序应该创建一个通知,允许用户在方便时做出响应。

    如果您的Activity 被暂停,则用户可能正在其他应用程序中执行其他操作,并且可能不希望您的Activity 突然出现在他们当前正在执行的操作之上。

    您应该使用Status Bar Notification。这允许您的应用程序在状态栏中放置一个图标。然后用户可以向下滑动状态栏抽屉并单击您的通知以打开您的应用程序并显示相关的Activity。这是绝大多数 Android 应用在后台运行时通知用户的方式。

    【讨论】:

      【解决方案2】:

      要完成 Hemendra 的回答,除了 FLAG_ACTIVITY_REORDER_TO_FRONT 之外,您不需要任何这些标志。您只需要从您的正常意图创建一个 PendingIntent 并调用新的 PendingIntent 的 send() 方法来调度该意图。这是我的做法:

      Intent yourIntent = new Intent(this, YourActivity.class);
      // You can send extra info (as a bundle/serializable) to your activity as you do 
      // with a normal intent. This is not necessary of course.
      yourIntent.putExtra("ExtraInfo", extraInfo); 
      // The following flag is necessary, otherwise at least on some devices (verified on Samsung 
      // Galaxy S3) your activity starts, but it starts in the background i.e. the user
      // doesn't see the UI
      yourIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
      PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, 
                                                              yourIntent, 0);
      try {
          pendingIntent.send(getApplicationContext(), 0, yourIntent);
      } catch (Exception e) {
          Log.e(TAG, Arrays.toString(e.getStackTrace()));
      }
      

      【讨论】:

      • 谢谢,在这里使用 PendingIntent 对我来说很关键。之前我尝试直接调用startActivity,没有成功。
      【解决方案3】:
      Intent i= new Intent("android.intent.category.LAUNCHER");
      i.setClass(getApplicationContext(), MyActivity.class);
      i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
      PendingIntent i2 = PendingIntent.getActivity(getApplicationContext(), 0, insIntent,Intent.FLAG_ACTIVITY_NEW_TASK);
      try {
           i2.send(getApplicationContext(), 0, i);
      } catch (Exception e) {
           e.printStackTrace();
      }
      

      在 MyActivity 的 onCreate...

      getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
      getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
      getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
      

      如果主要活动在后台运行,这会将您的活动带到最前面。

      【讨论】: