eclipse 如何从后台以编程方式恢复 Android Activity
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29766270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to resume Android Activity programmatically from background
提问by user2999943
Situation:
情况:
- Let's say I have currently working launched application Activity A.
- After some time I am pressing "Home" button. Application A goes to background.
- At this time, I am starting to use another app B - youtube for example or etc.
- Something happens (doesn't matter what in this context, let's say timer finished calculating time) in the application A which currently is minimized to background.
- On the event occurrence, application A activity automatically resumes from background.
- 假设我目前正在运行已启动的应用程序 Activity A。
- 一段时间后,我按下“主页”按钮。应用程序 A 进入后台。
- 此时,我开始使用另一个应用程序 B - 例如 youtube 等。
- 在当前最小化到后台的应用程序 A 中发生了一些事情(在这种情况下无关紧要,假设计时器已完成计算时间)。
- 在事件发生时,应用程序 A 活动会自动从后台恢复。
Question:
题:
How to accomplish step 5? Basically I need to know how to resume application from background programmatically.
如何完成第5步?基本上我需要知道如何以编程方式从后台恢复应用程序。
I tried to launch intent to "restart" my application activity but it didn't worked:
我尝试启动 Intent 以“重新启动”我的应用程序活动,但没有奏效:
Intent intent = new Intent(context, MainActivity.class);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
context.startActivity(intent);
My manifest file:
我的清单文件:
<?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>
回答by David Wasser
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.
为了将您的应用程序带到前台,您必须startActivity()
从另一个上下文( aService
或 a BroadcastReceiver
)调用。只是startActivity()
从内部调用Activity
不会将您的应用程序带到前台。
You don't need the ACTION and CATEGORY in your Intent
, but you do need to set Intent.FLAG_ACTIVITY_NEW_TASK
.
您不需要 ACTION 和 CATEGORY 在您的Intent
,但您确实需要设置Intent.FLAG_ACTIVITY_NEW_TASK
.
回答by Alexey Ozerov
If your activities are on different tasks, you can use this to bring the activity's task to foreground:
如果您的活动在不同的任务上,您可以使用它来将活动的任务置于前台:
ActivityManager activityManager = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
activityManager.moveTaskToFront(getTaskId(), ActivityManager.MOVE_TASK_NO_USER_ACTION);
You would need android.permission.REORDER_TASKS
in order to do so. It works even in Android M, however getting an intent from a service works better in some cases.
你需android.permission.REORDER_TASKS
要这样做。它甚至可以在 Android M 中使用,但是在某些情况下从服务中获取意图效果更好。
回答by tritop
Just establish a Service, doing whatever you want it to in the background. Or even better: Do something in the background, listen to it with a listener, bind a Service as soon as the event you waited for occurs (timer etc). Now that you are in the Service, you just call the Activity that should be on foreground like you would from anywhere else:
只需建立一个服务,在后台做任何你想做的事情。或者甚至更好:在后台做一些事情,用监听器听它,一旦你等待的事件发生(定时器等)就绑定一个服务。现在您在服务中,您只需像从其他任何地方一样调用应该在前台的活动:
Intent i = new Intent(MyService.this, MyActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
MyService.this.startActivity(i);
回答by user2999943
David Wasser answer helped to solve my problem.
David Wasser 的回答帮助解决了我的问题。
My Activity is running in a foreground Service
so I had to call startActivity()
method from that Service
context.
我的 Activity 在前台运行,Service
所以我必须startActivity()
从该Service
上下文调用方法。
Intent intent = new Intent(context, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mServiceContext.startActivity(intent);
回答by Kushal
To Resume application from background programmatically will
以编程方式从后台恢复应用程序将
Make your activity FOREGROUND, User's current activity in BACKGROUND. User's work is GONE
将您的活动设为前台,将用户当前的活动设为后台。用户的工作消失了
This is not right behavior from user perspective. Additionally, if we resume activity programmatically, the activity lifecycle will be broken. The activity states will be lost.
从用户的角度来看,这不是正确的行为。此外,如果我们以编程方式恢复活动,活动生命周期将被破坏。活动状态将丢失。
Alternative :
选择 :
You can provide NOTIFICATION
when your timer, task (anything) is complete in background. If user is interested in checking your activity then he/she can check from NOTIFICATION without interrupting current work
您可以提供NOTIFICATION
计时器、任务(任何事情)何时在后台完成。如果用户有兴趣查看您的活动,那么他/她可以在不中断当前工作的情况下从 NOTIFICATION 中查看
回答by VSim
The best way to do it I think is via an IntentService. It works this way too, and it's probably the easiest way. So the sequence is: register a receiver in the Activity (in onCreate) to listen for the event you want, then when the receiver gets the event, it launches an IntentService the standard way and this in turn starts the Activity.
This way it will put the app in front of other apps, both active and hidden ones. (But this should be used with caution. Normally the user won't like another app popping up in front of the one he's working with, except if there's a very good reason for it.)
On my KitKat device (4.4.x), I tested it and I can bring the app to front by calling startActivity from a receiver in the Activity only if there is no other app open, that is my app is hidden under the home screen. Then it will come on top. If another app is open, even if it's also hidden (but in front of my app; I actually didn't test to see what happens if both apps are hidden and mine is in front, but that's not very important), then it won't come on top if started from within the same Activity. So to make it always pop up you need to use an IntentService (or another way but this is as far as I know the simplest one).
我认为最好的方法是通过 IntentService。它也是这样工作的,这可能是最简单的方法。所以顺序是:在 Activity 中注册一个接收器(在 onCreate 中)来监听你想要的事件,然后当接收器收到事件时,它以标准方式启动一个 IntentService,这反过来又启动了 Activity。
这样,它将把应用程序放在其他应用程序的前面,包括活动的和隐藏的应用程序。(但这应该谨慎使用。通常用户不会喜欢在他正在使用的应用程序前面弹出另一个应用程序,除非有很好的理由。)
在我的 KitKat 设备 (4.4.x) 上,我对其进行了测试,并且仅当没有其他应用程序打开时,我才能通过从 Activity 中的接收器调用 startActivity 将应用程序置于前台,那是我的应用程序隐藏在主屏幕下。然后它会出现在顶部。如果另一个应用程序是打开的,即使它也是隐藏的(但在我的应用程序前面;我实际上没有测试看看如果两个应用程序都隐藏而我的应用程序在前面会发生什么,但这不是很重要),那么它就赢了如果从同一个活动中开始,则不会名列前茅。因此,要使其始终弹出,您需要使用 IntentService(或其他方式,但据我所知,这是最简单的方式)。