我已經得到了我從通知發佈的活動。我包括使用TaskStackBuilder,這樣當用戶點擊home鍵(動作條標題按鈕)或使用返回鍵,它會返回給應用程序一個回堆棧。但是,它不是以這種方式工作,而是按回或操作欄標題按鈕始終導致應用程序關閉。按BACK/HOME從通知發佈活動退出應用程序
對於它的價值,我的項目結構的組織使所有的UI組件是在Android庫(com.be.commotion)和外部「包裝」項目使用圖書館。
下面是如何打造的通知:
// This is the activity that will be launched when tapping the notification
Intent intentNotification = new Intent(this, NowPlaying.class);
intentNotification.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
// Create the back stack so the user can get back to the main activity when pressing the back button
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(NowPlaying.class);
stackBuilder.addNextIntent(intentNotification);
PendingIntent pendingNowPlayingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
// Setup the custom notification view
RemoteViews notificationContent = new RemoteViews(getPackageName(), R.layout.notification_now_playing);
....
// Setup the intent that will play/stop music when the stop button is tapped
Intent musicControlIntent = new Intent(this, CommotionMediaPlayer.class);
PendingIntent musicPendingIntent = PendingIntent.getService(this, 0, musicControlIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notificationContent.setOnClickPendingIntent(R.id.ibMediaControl, musicPendingIntent);
// Update the notification with this info
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(song.text)
.setContentText(song.artist)
.setContentIntent(pendingNowPlayingIntent)
.setLargeIcon(artwork)
.setContent(notificationContent)
.setOngoing(true);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Using the nowPlayingNotificationId allows the notification to be updated with later calls instead of causing a new notification to show up
mNotificationManager.notify(nowPlayingNotificationId, builder.build());
這裏是適用的定義在我的AndroidManifest.xml(用於包裝項目):
<activity android:name="com.be.commotion.ui.StartupActivity"
android:label="@string/app_name"
android:noHistory="true"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Holo.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name="com.be.commotion.ui.NowPlaying"
android:label="Now Playing"
android:parentActivityName="com.be.commotion.ui.StartupActivity"
android:screenOrientation="portrait">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.be.commotion.ui.StartupActivity" />
</activity>
爲什麼操作欄按鈕是這樣做的,我不知道 - 在這裏沒有看到任何代碼。 但我想指出的是您要使用後退按鈕做什麼違背了Android流量,並會阻礙用戶體驗。後退按鈕應該回到用戶以前的位置。將他們帶到主要活動意味着他們前進而不是後退。 – Navarr
此外,它聽起來像你正在使用主頁按鈕來觸發一個「後退」,當你應該有主頁按鈕啓動MainActivity或ParentActivity(這並不總是應該回來) – Navarr
@Navarr - 謝謝你的評論...我不確定我是否同意你的回覆:後退按鈕。如果我使用Gmail應用程序並從我的通知欄中打開電子郵件,按下後退按鈕會將我從電子郵件中取出,並向我顯示收件箱視圖。這對我來說很自然,是我想用我的應用程序實現的。 – bugfixr