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);
}
}
谢谢!你的意思是,尽管我调用finish()来关闭活动,但系统只会将活动推到后台并在将来发布它? – HelloCW
还有更多,是否有更好的方式来定义活动打开的方式? – HelloCW
我认为你可以在你的Intent中使用action属性的不同值。 – ivan