2015-11-06 84 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