毕竟,我需要通知我在互联网上搜索没有成功......
我的问题很简单。我的应用程序可以随时显示推送通知(应该打开Event.class
)。但是,当显示通知时,应用可能位于前台或后台。
按照谷歌的建议,我有以下代码
Intent resultIntent = new Intent(this, Event.class);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(getApplicationContext());
stackBuilder.addParentStack(Event.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent contentIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
这是我的清单:
<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity android:name=".MainActivity" android:screenOrientation="portrait" android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Event" android:screenOrientation="portrait"></activity>
<activity android:name=".HomeContainer" android:windowSoftInputMode="adjustPan" android:screenOrientation="portrait"></activity>
</application>
当应用在后台运行时
当我打开通知时,Event.class
被创建。当我单击back_button时,MainActivity.class
被创建。这很正常,这就是我想要的。
当应用位于前台时
我在
MainActivity.class
HomeContainer.class
.然后我打开通知,这样就创建了Event.class
。当我单击back_button时,MainActivity.class
被重新创建。我不想这样。在这种情况下,只想resume()
MainActivity.class
Homecontainer.class
。
我该怎么办?我在Android文档上搜索了不同的INTENT_FLAGS,但似乎没有任何调整。
提前谢谢。
根据David Wasser的要求,结果为:adb shell dumpsys activity activities
ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities)
Display #0 (activities from top to bottom):
Stack #1:
mFullscreen=true
mBounds=null
Task id #3259
mFullscreen=true
mBounds=null
mMinWidth=-1
mMinHeight=-1
mLastNonFullscreenBounds=null
* TaskRecord{1d5ebe7 #3259 A=me.APPNAME.app U=0 StackId=1 sz=1}
userId=0 effectiveUid=u0a132 mCallingUid=u0a132 mUserSetupComplete=true mCallingPackage=me.APPNAME.app
affinity=me.APPNAME.app
intent={flg=0x18000000 cmp=me.APPNAME.app/.Event}
realActivity=me.APPNAME.app/.Event
autoRemoveRecents=false isPersistable=true numFullscreen=1 taskType=0 mTaskToReturnTo=0
rootWasReset=false mNeverRelinquishIdentity=true mReuseTask=false mLockTaskAuth=LOCK_TASK_AUTH_PINNABLE
Activities=[ActivityRecord{d8edb34 u0 me.APPNAME.app/.Event t3259}]
askedCompatMode=false inRecents=true isAvailable=true
lastThumbnail=null lastThumbnailFile=/data/system_ce/0/recent_images/3259_task_thumbnail.png
stackId=1
hasBeenVisible=true mResizeMode=RESIZE_MODE_RESIZEABLE isResizeable=true firstActiveTime=1519781736501 lastActiveTime=1519781736501 (inactive for 5s)
* Hist #0: ActivityRecord{d8edb34 u0 me.APPNAME.app/.Event t3259}
packageName=me.APPNAME.app processName=me.APPNAME.app
launchedFromUid=10132 launchedFromPackage=me.APPNAME.app userId=0
app=ProcessRecord{1e57f14 19075:me.APPNAME.app/u0a132}
Intent { flg=0x18000000 cmp=me.APPNAME.app/.Event (has extras) }
frontOfTask=true task=TaskRecord{1d5ebe7 #3259 A=me.APPNAME.app U=0 StackId=1 sz=1}
taskAffinity=me.APPNAME.app
realActivity=me.APPNAME.app/.Event
baseDir=/data/app/me.APPNAME.app-1/base.apk
dataDir=/data/user/0/me.APPNAME.app
splitDir=[/data/app/me.APPNAME.app-1/split_lib_directories_apk.apk]
stateNotNeeded=false componentSpecified=true mActivityType=0
compat={384dpi} labelRes=0x7f100029 icon=0x7f0d0000 theme=0x7f110006
config={1.0 208mcc15mnc [fr_FR] ldltr sw449dp w449dp h775dp 384dpi nrml long port finger -keyb/v/h -nav/h s.11}
taskConfigOverride={1.0 ?mcc?mnc ?localeList ?layoutDir ?swdp ?wdp ?hdp ?density ?lsize ?long ?orien ?uimode ?night ?touch ?keyb/?/? ?nav/?}
taskDescription: iconFilename=null label="null" color=ff1976d2
launchFailed=false launchCount=1 lastLaunchTime=-5s734ms
haveState=false icicle=null
state=RESUMED stopped=false delayedResume=false finishing=false
keysPaused=false inHistory=true visible=true sleeping=false idle=true mStartingWindowState=STARTING_WINDOW_SHOWN
fullscreen=true noDisplay=false immersive=false launchMode=0
frozenBeforeDestroy=false forceNewConfig=false
mActivityType=APPLICATION_ACTIVITY_TYPE
waitingVisible=false nowVisible=true lastVisibleTime=-5s9ms
resizeMode=RESIZE_MODE_RESIZEABLE
Task id #3258
mFullscreen=true
mBounds=null
mMinWidth=-1
mMinHeight=-1
mLastNonFullscreenBounds=null
* TaskRecord{1a00394 #3258 A=me.APPNAME.app U=0 StackId=1 sz=1}
userId=0 effectiveUid=u0a132 mCallingUid=u0a132 mUserSetupComplete=true mCallingPackage=me.APPNAME.app
affinity=me.APPNAME.app
intent={act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=me.APPNAME.app/.MainActivity}
realActivity=me.APPNAME.app/.MainActivity
autoRemoveRecents=false isPersistable=true numFullscreen=1 taskType=0 mTaskToReturnTo=1
rootWasReset=true mNeverRelinquishIdentity=true mReuseTask=false mLockTaskAuth=LOCK_TASK_AUTH_PINNABLE
Activities=[ActivityRecord{e5f96d1 u0 me.APPNAME.app/.HomeContainer t3258}]
askedCompatMode=false inRecents=false isAvailable=true
lastThumbnail=null lastThumbnailFile=/data/system_ce/0/recent_images/3258_task_thumbnail.png
stackId=1
hasBeenVisible=true mResizeMode=RESIZE_MODE_RESIZEABLE isResizeable=true firstActiveTime=1519781736451 lastActiveTime=1519781736451 (inactive for 5s)
* Hist #0: ActivityRecord{e5f96d1 u0 me.APPNAME.app/.HomeContainer t3258}
packageName=me.APPNAME.app processName=me.APPNAME.app
launchedFromUid=10132 launchedFromPackage=me.APPNAME.app userId=0
app=ProcessRecord{1e57f14 19075:me.APPNAME.app/u0a132}
Intent { flg=0x10000 cmp=me.APPNAME.app/.HomeContainer }
frontOfTask=true task=TaskRecord{1a00394 #3258 A=me.APPNAME.app U=0 StackId=1 sz=1}
taskAffinity=me.APPNAME.app
realActivity=me.APPNAME.app/.HomeContainer
baseDir=/data/app/me.APPNAME.app-1/base.apk
dataDir=/data/user/0/me.APPNAME.app
splitDir=[/data/app/me.APPNAME.app-1/split_lib_directories_apk.apk]
stateNotNeeded=false componentSpecified=true mActivityType=0
compat={384dpi} labelRes=0x7f100029 icon=0x7f0d0000 theme=0x7f110006
config={1.0 208mcc15mnc [fr_FR] ldltr sw449dp w449dp h775dp 384dpi nrml long port finger -keyb/v/h -nav/h s.11}
taskConfigOverride={1.0 ?mcc?mnc ?localeList ?layoutDir ?swdp ?wdp ?hdp ?density ?lsize ?long ?orien ?uimode ?night ?touch ?keyb/?/? ?nav/?}
taskDescription: iconFilename=null label="null" color=ff1976d2
launchFailed=false launchCount=0 lastLaunchTime=-35s758ms
haveState=true icicle=Bundle[mParcelledData.dataSize=14188]
state=STOPPED stopped=true delayedResume=false finishing=false
keysPaused=false inHistory=true visible=false sleeping=false idle=true mStartingWindowState=STARTING_WINDOW_NOT_SHOWN
fullscreen=true noDisplay=false immersive=false launchMode=0
frozenBeforeDestroy=false forceNewConfig=false
mActivityType=APPLICATION_ACTIVITY_TYPE
waitingVisible=false nowVisible=false lastVisibleTime=-33s336ms
resizeMode=RESIZE_MODE_RESIZEABLE
Task id #3185
mFullscreen=true
mBounds=null
mMinWidth=-1
mMinHeight=-1
mLastNonFullscreenBounds=null
* TaskRecord{ec402a4 #3185 I=com.google.android.googlequicksearchbox/com.google.android.apps.gsa.staticplugins.opa.OpaActivity U=0 StackId=1 sz=1}
userId=0 effectiveUid=u0a43 mCallingUid=u0a43 mUserSetupComplete=true mCallingPackage=com.google.android.googlequicksearchbox
intent={flg=0x10808000 cmp=com.google.android.googlequicksearchbox/com.google.android.apps.gsa.staticplugins.opa.OpaActivity}
realActivity=com.google.android.googlequicksearchbox/com.google.android.apps.gsa.staticplugins.opa.OpaActivity
autoRemoveRecents=false isPersistable=false numFullscreen=1 taskType=0 mTaskToReturnTo=1
rootWasReset=false mNeverRelinquishIdentity=true mReuseTask=false mLockTaskAuth=LOCK_TASK_AUTH_PINNABLE
Activities=[ActivityRecord{6387e63 u0 com.google.android.googlequicksearchbox/com.google.android.apps.gsa.staticplugins.opa.OpaActivity t3185}]
askedCompatMode=false inRecents=true isAvailable=true
lastThumbnail=null lastThumbnailFile=/data/system_ce/0/recent_images/3185_task_thumbnail.png
stackId=1
hasBeenVisible=true mResizeMode=RESIZE_MODE_UNRESIZEABLE isResizeable=false firstActiveTime=1519761319987 lastActiveTime=1519761319987 (inactive for 20422s)
* Hist #0: ActivityRecord{6387e63 u0 com.google.android.googlequicksearchbox/com.google.android.apps.gsa.staticplugins.opa.OpaActivity t3185}
packageName=com.google.android.googlequicksearchbox processName=com.google.android.googlequicksearchbox:search
launchedFromUid=10043 launchedFromPackage=com.google.android.googlequicksearchbox userId=0
app=ProcessRecord{54c0ce7 16531:com.google.android.googlequicksearchbox:search/u0a43}
Intent { flg=0x10808000 cmp=com.google.android.googlequicksearchbox/com.google.android.apps.gsa.staticplugins.opa.OpaActivity (has extras) }
frontOfTask=true task=TaskRecord{ec402a4 #3185 I=com.google.android.googlequicksearchbox/com.google.android.apps.gsa.staticplugins.opa.OpaActivity U=0 StackId=1 sz=1}
taskAffinity=null
realActivity=com.google.android.googlequicksearchbox/com.google.android.apps.gsa.staticplugins.opa.OpaActivity
baseDir=/data/app/com.google.android.googlequicksearchbox-1/base.apk
dataDir=/data/user/0/com.google.android.googlequicksearchbox
stateNotNeeded=false componentSpecified=true mActivityType=0
compat={384dpi} labelRes=0x7f140c2b icon=0x7f02047c theme=0x7f1501ec
config={1.0 208mcc15mnc [fr_FR] ldltr sw449dp w449dp h775dp 384dpi nrml long port finger -keyb/v/h -nav/h s.11}
taskConfigOverride={1.0 ?mcc?mnc ?localeList ?layoutDir ?swdp ?wdp ?hdp ?density ?lsize ?long ?orien ?uimode ?night ?touch ?keyb/?/? ?nav/?}
taskDescription: iconFilename=null label="null" color=ff4285f4
launchFailed=false launchCount=0 lastLaunchTime=-5h31m17s606ms
haveState=true icicle=Bundle[mParcelledData.dataSize=5644]
state=STOPPED stopped=true delayedResume=false finishing=false
keysPaused=false inHistory=true visible=false sleeping=false idle=true mStartingWindowState=STARTING_WINDOW_NOT_SHOWN
fullscreen=true noDisplay=false immersive=false launchMode=2
frozenBeforeDestroy=false forceNewConfig=false
mActivityType=APPLICATION_ACTIVITY_TYPE
waitingVisible=false nowVisible=false lastVisibleTime=-5h9m27s823ms
resizeMode=RESIZE_MODE_UNRESIZEABLE
Running activities (most recent first):
TaskRecord{1d5ebe7 #3259 A=me.APPNAME.app U=0 StackId=1 sz=1}
Run #2: ActivityRecord{d8edb34 u0 me.APPNAME.app/.Event t3259}
TaskRecord{1a00394 #3258 A=me.APPNAME.app U=0 StackId=1 sz=1}
Run #1: ActivityRecord{e5f96d1 u0 me.APPNAME.app/.HomeContainer t3258}
TaskRecord{ec402a4 #3185 I=com.google.android.googlequicksearchbox/com.google.android.apps.gsa.staticplugins.opa.OpaActivity U=0 StackId=1 sz=1}
Run #0: ActivityRecord{6387e63 u0 com.google.android.googlequicksearchbox/com.google.android.apps.gsa.staticplugins.opa.OpaActivity t3185}
mResumedActivity: ActivityRecord{d8edb34 u0 me.APPNAME.app/.Event t3259}
mLastPausedActivity: ActivityRecord{e5f96d1 u0 me.APPNAME.app/.HomeContainer t3258}
Stack #0:
mFullscreen=true
mBounds=null
Task id #2488
mFullscreen=true
mBounds=null
mMinWidth=-1
mMinHeight=-1
mLastNonFullscreenBounds=null
* TaskRecord{f9fd3c2 #2488 I=com.google.android.apps.nexuslauncher/.NexusLauncherActivity U=0 StackId=0 sz=1}
userId=0 effectiveUid=u0a83 mCallingUid=u0a29 mUserSetupComplete=true mCallingPackage=com.android.systemui
intent={act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10000100 cmp=com.google.android.apps.nexuslauncher/.NexusLauncherActivity}
realActivity=com.google.android.apps.nexuslauncher/.NexusLauncherActivity
autoRemoveRecents=false isPersistable=true numFullscreen=1 taskType=1 mTaskToReturnTo=1
rootWasReset=false mNeverRelinquishIdentity=true mReuseTask=false mLockTaskAuth=LOCK_TASK_AUTH_PINNABLE
Activities=[ActivityRecord{871cda8 u0 com.google.android.apps.nexuslauncher/.NexusLauncherActivity t2488}]
askedCompatMode=false inRecents=true isAvailable=true
lastThumbnail=null lastThumbnailFile=/data/system_ce/0/recent_images/2488_task_thumbnail.png
stackId=0
hasBeenVisible=true mResizeMode=RESIZE_MODE_RESIZEABLE isResizeable=false firstActiveTime=1519781704569 lastActiveTime=1519781704569 (inactive for 37s)
* Hist #0: ActivityRecord{871cda8 u0 com.google.android.apps.nexuslauncher/.NexusLauncherActivity t2488}
packageName=com.google.android.apps.nexuslauncher processName=com.google.android.apps.nexuslauncher
launchedFromUid=0 launchedFromPackage=null userId=0
app=ProcessRecord{275537b 13195:com.google.android.apps.nexuslauncher/u0a83}
Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10000100 cmp=com.google.android.apps.nexuslauncher/.NexusLauncherActivity }
frontOfTask=true task=TaskRecord{f9fd3c2 #2488 I=com.google.android.apps.nexuslauncher/.NexusLauncherActivity U=0 StackId=0 sz=1}
taskAffinity=null
realActivity=com.google.android.apps.nexuslauncher/.NexusLauncherActivity
baseDir=/data/app/com.google.android.apps.nexuslauncher-1/base.apk
dataDir=/data/user/0/com.google.android.apps.nexuslauncher
stateNotNeeded=true componentSpecified=false mActivityType=1
compat={384dpi} labelRes=0x7f0c008c icon=0x7f02002b theme=0x7f120002
config={1.0 208mcc15mnc [fr_FR] ldltr sw449dp w449dp h775dp 384dpi nrml long port finger -keyb/v/h -nav/h s.11}
taskConfigOverride={1.0 ?mcc?mnc ?localeList ?layoutDir ?swdp ?wdp ?hdp ?density ?lsize ?long ?orien ?uimode ?night ?touch ?keyb/?/? ?nav/?}
taskDescription: iconFilename=null label="null" color=fff5f5f5
launchFailed=false launchCount=0 lastLaunchTime=-3d3h30m22s60ms
haveState=true icicle=Bundle[mParcelledData.dataSize=5208]
state=STOPPED stopped=true delayedResume=false finishing=false
keysPaused=false inHistory=true visible=false sleeping=false idle=true mStartingWindowState=STARTING_WINDOW_NOT_SHOWN
fullscreen=true noDisplay=false immersive=false launchMode=2
frozenBeforeDestroy=false forceNewConfig=false
mActivityType=HOME_ACTIVITY_TYPE
waitingVisible=false nowVisible=false lastVisibleTime=-38s713ms
connections=[]
resizeMode=RESIZE_MODE_RESIZEABLE
Task id #2490
mFullscreen=true
mBounds=null
mMinWidth=-1
mMinHeight=-1
mLastNonFullscreenBounds=null
* TaskRecord{39e75d3 #2490 A=com.android.systemui U=0 StackId=0 sz=1}
userId=0 effectiveUid=u0a29 mCallingUid=u0a29 mUserSetupComplete=true mCallingPackage=com.android.systemui
affinity=com.android.systemui
intent={flg=0x10804000 cmp=com.android.systemui/.recents.RecentsActivity bnds=[38,1727][1042,3206]}
realActivity=com.android.systemui/.recents.RecentsActivity
autoRemoveRecents=false isPersistable=false numFullscreen=1 taskType=2 mTaskToReturnTo=1
rootWasReset=false mNeverRelinquishIdentity=true mReuseTask=false mLockTaskAuth=LOCK_TASK_AUTH_PINNABLE
Activities=[ActivityRecord{52e27f8 u0 com.android.systemui/.recents.RecentsActivity t2490}]
askedCompatMode=false inRecents=true isAvailable=true
lastThumbnail=null lastThumbnailFile=/data/system_ce/0/recent_images/2490_task_thumbnail.png
stackId=0
hasBeenVisible=true mResizeMode=RESIZE_MODE_RESIZEABLE isResizeable=true firstActiveTime=1519781703116 lastActiveTime=1519781703116 (inactive for 39s)
* Hist #0: ActivityRecord{52e27f8 u0 com.android.systemui/.recents.RecentsActivity t2490}
packageName=com.android.systemui processName=com.android.systemui
launchedFromUid=10029 launchedFromPackage=com.android.systemui userId=0
app=ProcessRecord{cd948d2 12010:com.android.systemui/u0a29}
Intent { flg=0x10804000 cmp=com.android.systemui/.recents.RecentsActivity bnds=[38,1727][1042,3206] }
frontOfTask=true task=TaskRecord{39e75d3 #2490 A=com.android.systemui U=0 StackId=0 sz=1}
taskAffinity=com.android.systemui
realActivity=com.android.systemui/.recents.RecentsActivity
baseDir=/system/priv-app/SystemUI/SystemUI.apk
dataDir=/data/user_de/0/com.android.systemui
stateNotNeeded=true componentSpecified=true mActivityType=2
compat={384dpi} labelRes=0x7f0d02b9 icon=0x7f020175 theme=0x7f1301ea
config={1.0 208mcc15mnc [fr_FR] ldltr sw449dp w449dp h775dp 384dpi nrml long port finger -keyb/v/h -nav/h s.7}
taskConfigOverride={1.0 ?mcc?mnc ?localeList ?layoutDir ?swdp ?wdp ?hdp ?density ?lsize ?long ?orien ?uimode ?night ?touch ?keyb/?/? ?nav/?}
taskDescription: iconFilename=null label="null" color=ff212121
launchFailed=false launchCount=0 lastLaunchTime=-3d3h11m55s774ms
haveState=true icicle=Bundle[mParcelledData.dataSize=500]
state=STOPPED stopped=true delayedResume=false finishing=false
keysPaused=false inHistory=true visible=false sleeping=false idle=true mStartingWindowState=STARTING_WINDOW_NOT_SHOWN
fullscreen=true noDisplay=false immersive=false launchMode=3
frozenBeforeDestroy=false forceNewConfig=false
mActivityType=RECENTS_ACTIVITY_TYPE
waitingVisible=false nowVisible=false lastVisibleTime=-41s902ms
resizeMode=RESIZE_MODE_RESIZEABLE
Running activities (most recent first):
TaskRecord{f9fd3c2 #2488 I=com.google.android.apps.nexuslauncher/.NexusLauncherActivity U=0 StackId=0 sz=1}
Run #1: ActivityRecord{871cda8 u0 com.google.android.apps.nexuslauncher/.NexusLauncherActivity t2488}
TaskRecord{39e75d3 #2490 A=com.android.systemui U=0 StackId=0 sz=1}
Run #0: ActivityRecord{52e27f8 u0 com.android.systemui/.recents.RecentsActivity t2490}
mLastPausedActivity: ActivityRecord{871cda8 u0 com.google.android.apps.nexuslauncher/.NexusLauncherActivity t2488}
mFocusedActivity: ActivityRecord{d8edb34 u0 me.APPNAME.app/.Event t3259}
mFocusedStack=ActivityStack{690a510 stackId=1, 10 tasks}
mLastFocusedStack=ActivityStack{690a510 stackId=1, 10 tasks}
mSleepTimeout=false
mCurTaskIdForUser={0=3259}
mUserStackInFront={}
mActivityContainers={0=ActivtyContainer{0}A, 1=ActivtyContainer{1}A}
mLockTaskModeState=NONE mLockTaskPackages (userId:packages)=
0:[]
mLockTaskModeTasks[]
我尝试的更新代码(遵循David Wasser的建议):
Intent resultIntent = new Intent(this, Event.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
/** remove uninteresting lines */
NotificationCompat.Builder notificationBuilder = new
NotificationCompat.Builder(getApplicationContext(), channel_id);
notificationBuilder.setContentIntent(contentIntent);
/** other parameters as title, icons etc */
notificationManager.notify(_id, notificationBuilder.build());
不要使用Intent.FLAG_ACTIVITY_MULTIPLE_TASK
。这是非常特别的,会给你带来麻烦,不是必需的,也不是你想要的。
请勿使用TaskStackBuilder
。这将构造一个Intent
,该将在打开Notification
时清除任何现有任务。这就是为什么当应用程序运行时,您会获得与应用程序未运行时相同的行为。
只需这样做:
Intent resultIntent = new Intent(this, Event.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
这将启动Event
到现有任务(如果应用正在运行),或者创建一个新任务并在该任务中启动Event
。如果您希望在用户按 BACK 键(并且应用尚未运行)时启动MainActivity
请执行以下操作:
在Event
覆盖onBackPressed()
:
@Override
public void onBackPressed() {
if (isTaskRoot()) {
// This Activity is the root Activity, so start `MainActivity`
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
} else {
// Just use the default implementation
super.onBackPressed();
}
}