当应用处于背景状态时用户点击通知,该应用程序已打开并跳转到PickUpActivity
NotificationManager mNotifyMgr = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
int notificationId = (int) System.currentTimeMillis();
Intent picukUpIntent = new Intent(context, PickUpActivity.class);
picukUpIntent.putExtra(MainScreenActivity.ORDER_ID, orderId);
picukUpIntent.putExtra(NOTI_TYPE, 3);
pendingIntent = PendingIntent.getActivity(
context,
notificationId,
picukUpIntent,
PendingIntent.FLAG_ONE_SHOT
);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_mini_logo)
.setContentTitle("Title")
.setContentText(message)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setAutoCancel(true);
builder.setContentIntent(pendingIntent);
mNotifyMgr.notify(notificationId, builder.build());
当用户在应用程序处于后台时单击通知时,它会按照我的预期跳转到PickUpActivity。问题是,当用户点击或退回按钮时,它会退出应用程序。我希望当用户单击后退或按钮时它会跳转到MainScreenActivity。当用户在foregroud中点击通知时,该工作正常。所以我不想覆盖PickUpActivity中的后退和按钮行为。 有没有办法设置PickUpActivity的父级是MainScreenActivity 我在Manifest中设置了父级,但它不起作用
<activity
android:name=".screen.rating.PickUpActivity"
android:parentActivityName=".screen.main.MainScreenActivity"
android:screenOrientation="portrait">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".screen.main.MainScreenActivity" />
</activity>
答案 0 :(得分:1)
在您的通知活动中将其添加到onBackPressed
。
在通知活动开始后putextra()之后。 &#34; onCreat()&#34;
@Override
public void onBackPressed() {
Bundle extras = getIntent().getExtras();
boolean launchedFromNotif = false;
if (extras.containsKey("EXTRA_LAUNCHED_BY_NOTIFICATION")) {
launchedFromNotif = extras.getBoolean("EXTRA_LAUNCHED_BY_NOTIFICATION");
}
if (launchedFromNotif) {
// Launched from notification, handle as special case
Intent intent = new Intent(this, MainScreenActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
mActivity.startActivity(intent);
finish();
} else {
super.onBackPressed();
}
}
答案 1 :(得分:1)
当您在后台或前台使用应用程序时,您需要识别应用程序状态,因此使用以下方法可以识别您的应用程序状态。
public static boolean isAppIsInBackground(Context context) {
boolean isInBackground = true;
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) {
List<ActivityManager.RunningAppProcessInfo> runningProcesses = am.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) {
if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
for (String activeProcess : processInfo.pkgList) {
if (activeProcess.equals(context.getPackageName())) {
isInBackground = false;
}
}
}
}
} else {
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity;
if (componentInfo.getPackageName().equals(context.getPackageName())) {
isInBackground = false;
}
}
return isInBackground;
}
然后在下面添加一行代码进入你的背压事件
@Override
public void onBackPressed() {
super.onBackPressed();
if(NotificationUtils.isAppIsInBackground(context)){
//open your main screen activity MainScreenActivity.class
}else{
// what you want
}
}
答案 2 :(得分:0)
Try to start activity your main Activity on BackPressed of Notification Screen
Implement the below code in your PickUpActivity Screen.
@Override
public void onBackPressed()
{
super.onBackPressed();
Intent intent = new Intent(PickUpActivity.this, MainScreenActivity.class);
startActivity(intent);
finish();
}