2014-05-04 28 views
1

場景: 我的應用程序有一個包含ViewPager的活動,它顯示兩個片段(稱爲頁面A和頁面B)。在與PendingIntent相同的活動中選擇通知,但不同的ViewPage

問題: 當在後臺事件觸發在網頁A的通知,並且用戶在網頁B,選擇通知不執行任何;即沒有回調的onCreate()的onResume(),onNewIntent(),onResumeFragments(),或在onStart()和用戶停留在頁面B.

問:

  • 怎麼回事當用戶 選擇了通知並且在頁面B上時,可能使ViewPager顯示頁面A?

  • 是否有可能,活動內,當用戶已經選擇 指向相同活性的通知來檢測?

代碼通知:

// Create a notification 
    final Context ctx = this; 
    Intent notificationIntent = new Intent(ctx, ActivityMain.class); 
    notificationIntent.putExtra(INTENT_EXTRA_NOTIFICATION, true); 

    NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE); 
    Resources res = ctx.getResources(); 

    PendingIntent pendingIntent = PendingIntent.getActivity(ctx, REQUESTCODE_ACTIVITYMAIN_LISTFRAGMENT, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT); 

    Notification.Builder builder = new Notification.Builder(ctx); 
    builder.setContentIntent(pendingIntent) 
       .setSmallIcon(R.drawable.ic_launcher)//required 
       .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.ic_launcher)) 
       .setContentTitle(res.getString(R.string.notification_title))//required 
       .setContentText(data.description)//required 
       .setTicker(res.getString(R.string.notification_title)) 
       .setWhen(System.currentTimeMillis()) 
       .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS) 
       .setAutoCancel(true); 

    Notification notification = builder.build(); 

    notificationManager.notify(mNotificationID, notification); 

也許有一個標誌/配置可添加到通知的意圖迫使活動認識到所選擇的通知?

回答

1

它採取了一些努力,但這裏的時候,選擇了一個活動已在前臺運行的通知是切換ViewPager標籤解決方案:

1)用標誌FLAG_ACTIVITY_NEW_TASK創建通知和一個額外的證明的通知推出這個活動

// Create a notification 
final Context ctx = this; 
Intent notificationIntent = new Intent(ctx, ActivityMain.class); 
notificationIntent.putExtra(INTENT_EXTRA_NOTIFICATION, true); 
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
//see rest of code in my question to create the notification 

2)更新清單與singleTop的活動

<activity 
    android:name="com.cool.app.ActivityMain" 
    android:label="@string/app_name" 
    android:launchMode="singleTop" > 
</activity> 

3)在ActivityMain(或任何你活性)重寫onNewIntent()

@Override 
protected void onNewIntent(Intent intent) 
{ 
    Log.i(TAG,"onNewIntent"); 
    super.onNewIntent(intent); 
    setIntent(intent);//needed so that getIntent() doesn't return null inside onResume() 
} 

4)在ActivityMain(或任何你活性)覆蓋的onResume()和包括邏輯到切換標籤

@Override 
protected void onResume() 
{ 
    super.onResume(); 

    // Test if a Notification caused this activity to launch - if so then select the first tab 
    // NOTE: we get here from a notification by using Intent.FLAG_ACTIVITY_NEW_TASK 
    Bundle extras = getIntent().getExtras(); 
    if (extras != null) 
    { 
     final boolean bIsLaunchedFromNotification = extras.getBoolean(INTENT_EXTRA_NOTIFICATION); 
     if (bIsLaunchedFromNotification) 
     { 
      final ActionBar actionBar = getActionBar(); 
      actionBar.setSelectedNavigationItem(0); 
     } 
    } 
} 

5)良好形式 - 在ActivityMain(或應用程序)中,定義傳遞額外的常量

final public static String INTENT_EXTRA_NOTIFICATION = "fromNotification"; 
相關問題