是否有恢复应用程序,或再次打开它的机器人意图是什么? [英] Is there an android intent that resumes the app or opens it again?
问题描述
在我的Android应用程序,这code将打开/恢复活动
In my android app, this code will open/resume a activity
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notIntent = new Intent(context, SpeciesScreen.class);
notIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pIntent = PendingIntent.getActivity(context.getApplicationContext(), 0, notIntent, 0);
Notification noti = new Notification.Builder(context)
.setContentTitle("Scan Complete")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent)
.setAutoCancel(true)
.build();
notificationManager.notify(0, noti);
行为是,如果应用程序正在运行,并尽量减少对SpeciesScreen,它会恢复它,如果它已经运行或退出,那么它会尝试打开SpeciesScreen。注:SpeciesScreen不是最初运行的默认行为
The behavior is that if the app is running and minimized on SpeciesScreen, it will resume it, if its already running or exited, then it will attempt to open SpeciesScreen. Note: SpeciesScreen is not the default activity that runs initially.
但我要的是(在运行的意图通知当点击):
But what I want is (when clicking on the notification that runs the intent):
如果其最小化(不管是什么活动了目前的),只是恢复它,不要打开另一个活动。
If its minimized (regardless of what activity its currently on), just resume it, don't open another activity.
如果它运行(不管是什么活动了目前),什么也不做。
If its running (regardless of what activity its currently on), do nothing.
如果其关闭,然后打开应用程序,如果你点击主屏幕上的图标。
If its closed, then open the app as if you clicked on the icon on the home screen.
有谁知道如何做到这一点?
Does anyone know how to do this?
感谢
推荐答案
好吧,这里是黑客攻击的方式我得到了,也许有人可以改善它。
Ok, here is the hack way I got, maybe someone can improve on it.
public Boolean paused = false;
@Override
protected void onPause() {
super.onPause();
paused = true;
}
@Override
protected void onResume() {
super.onResume();
paused = false;
}
private void setUpScreen() {
if (getIntent().getExtras() != null) {
// ran normally
} else {
// opened from notification
finish();
// open default activity
Intent myIntent = new Intent(this, SplashScreen.class);
startActivity(myIntent);
}
}
在异步
@Override
protected void onPostExecute(List<CompareResult> compareResults) {
if (context.paused) {
showNotification();
}
}
private void showNotification() {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notIntent = new Intent(context, SpeciesScreen.class);
notIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pIntent = PendingIntent.getActivity(context.getApplicationContext(), 0, notIntent, 0);
Notification noti = new Notification.Builder(context)
.setContentTitle("Scan Complete")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent)
.setAutoCancel(true)
.build();
notificationManager.notify(0, noti);
}
这篇关于是否有恢复应用程序,或再次打开它的机器人意图是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!