2013-07-16 41 views
1

我已經得到了我從通知發佈的活動。我包括使用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> 
+0

爲什麼操作欄按鈕是這樣做的,我不知道 - 在這裏沒有看到任何代碼。 但我想指出的是您要使用後退按鈕做什麼違背了Android流量,並會阻礙用戶體驗。後退按鈕應該回到用戶以前的位置。將他們帶到主要活動意味着他們前進而不是後退。 – Navarr

+0

此外,它聽起來像你正在使用主頁按鈕來觸發一個「後退」,當你應該有主頁按鈕啓動MainActivity或ParentActivity(這並不總是應該回來) – Navarr

+0

@Navarr - 謝謝你的評論...我不確定我是否同意你的回覆:後退按鈕。如果我使用Gmail應用程序並從我的通知欄中打開電子郵件,按下後退按鈕會將我從電子郵件中取出,並向我顯示收件箱視圖。這對我來說很自然,是我想用我的應用程序實現的。 – bugfixr

回答

2

您可以通過2步做到這一點,而無需使用TaskStackBuilder
1 /設置標誌爲您的意圖

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK); 
爲您的PendingIntent

2 /套標誌

pendingIntent = PendingIntent.getActivity(MainActivity.this,0,intent,PendingIntent.FLAG_CANCEL_CURRENT); 

它爲我,好運氣的傢伙

+0

需要建議。所有是正確的,但是當我從應用程序中單擊事件單擊設備後退按鈕和出(播放/暫停/前進/關閉)按鈕不work.Please幫助我。 –

0

添加的主要活動堆棧你添加所需的活動(直接調用時通知是活動前點擊)。 例如:

stackBuilder.addNextIntent(new Intent(this,MainActivity.class)); 
stackBuilder.addNextIntent(new Intent(this,DesiredActivity.class)); 
相關問題