可以通过单击应用程序图标或通知图标打开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 :(得分: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;
}
}
答案 1 :(得分:0)
因为您的活动正在运行后台,当您单击notifacation活动时会显示但意图没有转移到活动,因此您无法获取该值,您可以使用这种方式:添加代码intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
这将清除活动背景