如何从后台以编程方式恢复 Android Activity

How to resume Android Activity programmatically from background

情况:

  1. 假设我目前正在启动应用程序 Activity A.
  2. 一段时间后,我按下了 "Home" 按钮。应用程序 A 进入后台。
  3. 此时,我开始使用另一个应用程序 B - 例如 youtube 等
  4. 当前最小化到后台的应用程序 A 发生了一些事情(在这种情况下无关紧要,假设计时器完成了时间计算)。
  5. 事件发生时,应用程序 A activity 自动从后台恢复。

问题:

如何完成第5步?基本上我需要知道如何以编程方式从后台恢复应用程序。

我尝试向 "restart" 我的应用程序 activity 启动 intent,但没有成功:

Intent intent = new Intent(context, MainActivity.class);
            intent.setAction(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
            context.startActivity(intent);

我的清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.taxti"
    android:versionCode="43"
    android:versionName="1.5.3" >     

    <uses-sdk
        android:minSdkVersion="13"
        android:targetSdkVersion="21" />

    <permission
        android:name="com.example.taxti.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <uses-permission android:name="com.example.taxti.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />       
    <permission android:name="com.taxti.permission.C2D_MESSAGE"
                android:protectionLevel="signature" />
    <uses-permission android:name="com.taxti.permission.C2D_MESSAGE" />     
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />   
    <uses-permission android:name="android.permission.CALL_PHONE" />  
    <uses-permission android:name="android.permission.EXPAND_STATUS_BAR" />
    <uses-permission android:name="android.permission.CLEAR_APP_CACHE" />

    <application
        android:allowBackup="true"        
        android:icon="@drawable/ic_launcher"
        android:hardwareAccelerated="true"
        android:label="@string/app_name"
        android:name="com.taxti.Globals"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.taxti.InitialActivity"
            android:label="@string/app_name"
            android:windowSoftInputMode="adjustPan"
            android:screenOrientation="sensorLandscape"
            android:theme="@style/MyTheme" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.taxti.MainActivity"
            android:label="@string/app_name"
            android:launchMode="singleTop"
            android:windowSoftInputMode="adjustPan"
            android:screenOrientation="sensorLandscape"
            android:theme="@style/MyTheme">
        </activity>

        <service android:name=".MainActivityForegroundService" />

        <intent-filter>
                <action android:name="android.net`enter code here`.conn.CONNECTIVITY_CHANGE" />                
        </intent-filter>

       <intent-filter>
            <action android:name="android.intent.action.CALL_PRIVILEGED" />
            <category android:name="android.intent.category.DEFAULT" />     
            <action android:name="android.intent.action.DIAL" />
            <action android:name="android.intent.action.CALL_BUTTON" />     
            <category android:name="android.intent.category.BROWSABLE" />           
            <data android:scheme="tel" />
       </intent-filter>

        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="xxxx" />             
        <uses-library android:name="com.google.android.maps" />

        <receiver
            android:name=".GcmBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >   
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />               
                <category android:name="com.taxti" />
            </intent-filter>

        </receiver>

    </application>

</manifest>

以编程方式从后台恢复应用程序将

Make your activity FOREGROUND, User's current activity in BACKGROUND. User's work is GONE

从用户的角度来看,这是不正确的行为。此外,如果我们以编程方式恢复 activity,activity 生命周期将被打破。 activity 个状态将丢失。

备选方案:

当您的计时器、任务(任何)在后台完成时,您可以提供 NOTIFICATION。如果用户有兴趣查看您的 activity,那么 he/she 可以在不中断当前工作的情况下从通知 进行查看

只需建立一个服务,在后台为所欲为。或者甚至更好:在后台做一些事情,用一个监听器来监听它,一旦你等待的事件发生(定时器等)就绑定一个服务。 现在你在服务中,你只需调用应该在前台的 Activity 就像你在其他任何地方一样:

Intent i = new Intent(MyService.this, MyActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
MyService.this.startActivity(i);

为了将您的应用程序置于前台,您必须从另一个上下文(ServiceBroadcastReceiver)调用 startActivity()。仅从 Activity 中调用 startActivity() 不会将您的应用带到前台。

您不需要 Intent 中的 ACTION 和 CATEGORY,但您需要设置 Intent.FLAG_ACTIVITY_NEW_TASK

David Wasser 的回答帮助解决了我的问题。

我的 Activity 是 运行 在前台 Service 所以我不得不从那个 Service 上下文中调用 startActivity() 方法。

Intent intent = new Intent(context, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mServiceContext.startActivity(intent);

如果您的活动针对不同的任务,您可以使用它来将 activity 的任务置于前台:

ActivityManager activityManager = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE); 
activityManager.moveTaskToFront(getTaskId(), ActivityManager.MOVE_TASK_NO_USER_ACTION);

您需要 android.permission.REORDER_TASKS 才能这样做。它甚至可以在 Android M 中工作,但是在某些情况下从服务获取意图效果更好。

我认为最好的方法是通过 IntentService。它也以这种方式工作,这可能是最简单的方法。所以序列是:在Activity(在onCreate)中注册一个接收器来监听你想要的事件,然后当接收器得到事件时,它以标准方式启动一个IntentService,这反过来启动Activity.
这样,它会将应用程序置于其他应用程序(包括活动应用程序和隐藏应用程序)的前面。 (但这应该谨慎使用。通常情况下,用户不会喜欢在他正在使用的应用程序前面弹出另一个应用程序,除非有很好的理由。)
在我的 KitKat 设备 (4.4.x) 上,我对其进行了测试,并且我可以通过从 Activity 中的接收器调用 startActivity 将应用程序置于最前面,仅当没有打开其他应用程序,那是我的应用程序隐藏在主屏幕下。然后它会排在最前面。如果另一个应用程序打开,即使它也被隐藏(但在我的应用程序前面;我实际上没有测试看看如果两个应用程序都被隐藏而我的应用程序在前面会发生什么,但这不是很重要),那么它会赢' 如果从同一个 Activity 开始,则排在首位。因此,要使其始终弹出,您需要使用 IntentService(或其他方式,但据我所知,这是最简单的方式)。