UIMain可以通过点击App图标或者Notification图标来打开,我希望定义activity打开的方式。
所以我使用 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);
}
}
最佳答案
希望对你有帮助
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);
在您的 Activity 类中,您可以使用以下代码检查是否通过单击通知图标打开了 Activity
在你的 oncreat 中,你可以使用
检查 Activity 是否通过通知打开 String type = getIntent().getStringExtra("From");
if (type != null) {
switch (type) {
case "notifyFrag":
// your activity called from notification
break;
}
}
关于android - 我怎么知道点击通知图标打开了一个 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33561611/