2015-11-06 80 views
1

UIMain可以通過點擊應用程序圖標或通知圖標打開,我希望定義打開活動的方式。如何通過單擊通知圖標知道活動已打開?

所以我使用notificationIntent.putExtra("IsStartFromIcon",true)設置標籤,但Utility.LogError(""+isStartFromIcon)總是顯示false,我的代碼有什麼問題?

還有其他更好的方法嗎?謝謝!

public class UIMain extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.layout_main); 

     findViewById(R.id.buttonClose).setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       PublicParFun.DisplayIconByContent(mContext, "This is OK"); 
       finish(); 
      } 
     }); 

     boolean isStartFromIcon=this.getIntent().getBooleanExtra("IsStartFromIcon",false); 
     Utility.LogError(""+isStartFromIcon); 

    } 

} 

PublicParFun.cs

public class PublicParFun { 
    public static void DisplayIconByContent(Context myContext,String myContentText) { 

     CharSequence contentTitle= myContext.getResources().getString(R.string.NotificationTitle); 

     Intent notificationIntent = new Intent(myContext, ui.UIMain.class); 

     notificationIntent.putExtra("IsStartFromIcon",true); 

     PendingIntent contentItent = PendingIntent.getActivity(myContext, 0,notificationIntent, 0); 



     Notification.Builder builder = new Notification.Builder(myContext) 
       .setSmallIcon(R.drawable.icon) 
       .setContentTitle(contentTitle) 
       .setContentText(myContentText) 
       .setContentIntent(contentItent); 
     Notification notification = builder.build(); 
     notification.flags |= Notification.FLAG_ONGOING_EVENT; 
     notification.flags |= Notification.FLAG_NO_CLEAR; 

     NotificationManager notificationManager = (NotificationManager) myContext.getSystemService(android.content.Context.NOTIFICATION_SERVICE); 
     notificationManager.notify(myContext.getResources().getInteger(R.integer.NotificationID), notification); 
    } 
} 

回答

0

因爲你活動運行的背景下,當您單擊notifacation活動將顯示,但不打算轉移到活動,所以你不能得到的價值,你可以使用這種方式:添加代碼intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)這將清除活動的背景

+0

謝謝!你的意思是,儘管我調用finish()來關閉活動,但系統只會將活動推到後臺並在將來發布它? – HelloCW

+0

還有更多,是否有更好的方式來定義活動打開的方式? – HelloCW

+0

我認爲你可以在你的Intent中使用action屬性的不同值。 – ivan

2

希望這會幫助你

private void generateNotification(Context context, String message) { 

     int icon = R.drawable.icon; 

     long when = System.currentTimeMillis(); 

     NotificationManager notificationManager = (NotificationManager) 

       context.getSystemService(Context.NOTIFICATION_SERVICE); 

     Notification notification = new Notification(icon, message); 

     String title = context.getString(R.string.app_name); 

     Intent notificationIntent;  
     notificationIntent = new Intent(context, ActivityName.class); 
     notificationIntent.putExtra("From", "notifyFrag"); 

     notificationIntent .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | 
       Intent.FLAG_ACTIVITY_SINGLE_TOP); 
     PendingIntent intent = 
       PendingIntent.getActivity(context, 0, notificationIntent, 0); 
     notificationIntent .setLatestEventInfo(context, title, message, intent); 
     notification.flags |= Notification.FLAG_AUTO_CANCEL; 
     notification.defaults |= Notification.DEFAULT_VIBRATE; 
     notificationManager.notify(0, notification); 

}

在活動類

可以檢查活動的通知圖標打開點擊或在裏面你oncreat您可以用檢查活動是由通知開不使用下面的代碼

String type = getIntent().getStringExtra("From"); 
if (type != null) { 
    switch (type) { 
     case "notifyFrag": 

     // your activity called from notification 

      break; 
    } 
} 
+0

謝謝,但你的方式與我的方式相同。 – HelloCW

+0

讓我知道你的結果 – saeed