2014-06-25 80 views
2

我試圖在通知欄中按通知時打開一個片段。 我的應用程序的結構是:在android中按通知時打開一個片段

  • 與導航抽屜菜單
  • 和一些片段被從菜單

開了一個基本活動。當我按通知的活動重新開放,但不是在指示的片段,在LogCat中,來自指定片段的數據看起來是打開並加載的,但不是用戶界面。

通知碼:

 NotificationCompat.Builder mBuilder = 
    new NotificationCompat.Builder(this); 
     mBuilder.setContentTitle("Comanda Noua"); 
     mBuilder.setContentText("S-a introdus o comanda noua"); 
     mBuilder.setTicker("Comanda noua!"); 
     mBuilder.setSmallIcon(R.drawable.calculator_icon); 
    mBuilder.setAutoCancel(true);//inchide notificare dupa ce s-a dat click pe ea 

     //creste numar notificare 
     mBuilder.setNumber(++numMessages); 

     // cream un intent 
     Intent resultIntent = new Intent(this, MainActivity.class); 

     //am setat actiune pentru a deschide fragmentul corespunzator notificartii la apasare 
     resultIntent.setAction("Action1"); 
     TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); 
     stackBuilder.addParentStack(MainActivity.class); 


     // Adds the Intent that starts the Activity to the top of the stack 
    stackBuilder.addNextIntent(resultIntent); 
     PendingIntent resultPendingIntent = 
     stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 

     mBuilder.setContentIntent(resultPendingIntent); 

     mNotificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

     // notificationID allows you to update the notification later on. 
     mNotificationManager.notify(notificationID, mBuilder.build()); 

我在哪裏開的片段時,按下通知代碼:

Intent intent = getIntent(); 
    try{ 
     String action = intent.getAction(); 
     // Log.d("action:",action); 

     if(action.equals("Action1")){ 
      //Log.d("deschidem agenda:",action); 
      AgendaFragment fragment = new AgendaFragment(); 
     FragmentTransaction transaction = getFragmentManager() 
        .beginTransaction(); 
     transaction.replace(R.id.frame_container, fragment) 
      .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN) 
      .addToBackStack(null).commit(); 
     }else{ 
      Log.d("eroare", "Intent was null"); 
     } 
    }catch(Exception e){ 
     Log.e("eroare", "Problem consuming action from intent", e);    
    } 

爲什麼活動開放,但片段沒有?

+0

爲什麼您使用TaskStackBuilder創建掛起的意圖? –

+0

我是Android新手,通知代碼來自互聯網。我想我使用它來把活動放在stack.if我沒有使用通知什麼都不做 – user3481497

回答

0

嘗試下面的代碼,它是更簡單的方法來顯示通知:

Intent resultIntent = new Intent(this, MainActivity.class); 

resultIntent.setAction("Action1"); 

PendingIntent resultPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, resultIntent, PendingIntent.FLAG_CANCEL_CURRENT); 

mNotificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

Notification notification = new Notification(R.drawable.icon_notification, "Your title", System.currentTimeMillis()); 
notification.flags = Notification.FLAG_AUTO_CANCEL; 
notification.setLatestEventInfo(this, "Your title", "Your text", pendingIntent); 

mNotificationManager.notify(notificationID, notification); 
+0

我有這個錯誤:方法getActivity(上下文,int,意圖,int)是未定義的類型MainActivity – user3481497

+0

對不起,我錯了。我更新了我的帖子。 –

+0

通知出現,但當我點擊它的主要活動開始,但不是特定的片段(片段在LogCat中加載,但用戶界面沒有出現)。當我從通知中導航新的實例活動後,會出現另一個通知... – user3481497

0

與廣播

在通知類TRY你必須sendBroadcast。它的默認方法。

 `intent.setAction("one"); 
     sendBroadcast(intent);` 

在基本活動剛剛創建廣播reveiver

private BroadcastReceiver receiver = new BroadcastReceiver() { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     if(intent!=null && intent.getAction().equals("one")) 

     { 
      displayView(fragement_position); 
     } 
    } 
}; 

在上BaseAcitivty的創建resiger的broatcast接收機

IntentFilter filter = new IntentFilter(); 
    filter.addAction("one"); 
    registerReceiver(getList, filter); 

您必須添加的IntentFilter

,你必須取消註冊對基地活動的破壞

@Override protected void onDestroy() { unregisterReceiver(receiver); super.onDestroy(); }