如何从后台以编程方式恢复 Android Activity [英] How to resume Android Activity programmatically from background
问题描述
情况:
- 假设我目前正在处理已启动的应用程序 Activity A.
- 一段时间后,我按下主页"按钮.应用程序 A 进入后台.
- 此时,我开始使用另一个应用程序 B - 例如 youtube 等.
- 在当前最小化到后台的应用程序 A 中发生了一些事情(在此上下文中无关紧要,假设计时器已完成计算时间).
- 在事件发生时,应用程序 A 活动会自动从后台恢复.
问题:
如何完成第 5 步?基本上我需要知道如何以编程方式从后台恢复应用程序.
How to accomplish step 5? Basically I need to know how to resume application from background programmatically.
我尝试启动 Intent 以重新启动"我的应用程序活动,但没有奏效:
I tried to launch intent to "restart" my application activity but it didn't worked:
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>
推荐答案
为了将您的应用程序带到前台,您必须从另一个上下文(Service
或 BroadcastReceiver
).仅从 Activity
中调用 startActivity()
不会将您的应用带到前台.
In order to bring your app to the foreground, you must call startActivity()
from another context (either a Service
or a BroadcastReceiver
). Just calling startActivity()
from within an Activity
won't bring your app to the foreground.
Intent
中不需要 ACTION 和 CATEGORY,但需要设置 Intent.FLAG_ACTIVITY_NEW_TASK
.
You don't need the ACTION and CATEGORY in your Intent
, but you do need to set Intent.FLAG_ACTIVITY_NEW_TASK
.
这篇关于如何从后台以编程方式恢复 Android Activity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!