2017-08-16 83 views
0

通知上的用戶點擊,而應用程序是在研究背景和應用程序被打開,並躍升至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 我做一套父母的清單,但它不工作

<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> 

回答

1

在notifiaction活動一下添加到onBackPressed

通知活動中的putextra()開始後。 「onCreat()」。

@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(); 
    } 
} 
+0

這是一個好主意。謝謝 –

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(); 

} 
+0

我不想重寫BackPressed –

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 
     } 
    } 
+0

它不能解決我的問題,因爲如果用戶可以點擊向上或向後按鈕,當然應用程序在前臺。但我也需要在我的應用程序中使用此代碼,並且您的代碼完美無缺。非常感謝! –

+0

這意味着你對我說用戶按下後退按鈕時? –

+0

當用戶按下主頁按鈕或返回按鈕時,在兩個地方打一個功能並撥打電話 –