我正在使用一些EditTexts和正在进行的通知进行活动。
填写完EditTexts后,按Home键返回主屏幕(我的应用程序在后台运行)。我想要的是在点击正在进行的通知时,用填充的EditTexts (不创建一个新的)返回我的活动。
我试过这个
How should i do from notification back to activity without new intent
这个
Notification click: activity already open
他们根本不工作!!!
以下是我的代码段
ProtocolMonitorActivity.java
Intent resultIntent = new Intent(this, ProtocolMonitorActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(ProtocolMonitorActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notiBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.noti_icon)
.setContentTitle("Protocol Monitor App")
.setContentText("Service is running")
.setOngoing(true);
notiBuilder.setContentIntent(resultPendingIntent);
notiManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notiManager.notify(notiId, notiBuilder.build());
清单
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".ProtocolMonitorActivity"
android:label="@string/app_name"
android:process="com.android.phone"
android:launchMode="singleTop"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.DEVELOPMENT_PREFERENCE" />
</intent-filter>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".ProtocolMonitorActivity" />
</activity>
</application>
有人对此有任何想法吗?
非常感谢你!
答案 0 :(得分:3)
您无法使用TaskStackBuilder
执行此操作。 TaskStackBuilder
的行为是它总是清除任务(重新创建任何活动)。
你只需要一个&#34;启动Intent&#34;将你的任务带到前台,无论它碰巧处于什么状态。有两种方法可以做到这一点:
变化1:
final Intent notificationIntent = new Intent(this, ProtocolMonitorActivity.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
变式2:
final Intent notificationIntent =
PackageManager.getLaunchIntentForPackage(getPackageName());
然后做:
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notiBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.noti_icon)
.setContentTitle("Protocol Monitor App")
.setContentText("Service is running")
.setOngoing(true);
notiBuilder.setContentIntent(resultPendingIntent);
notiManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notiManager.notify(notiId, notiBuilder.build());
答案 1 :(得分:0)
只需插入此行
即可resultIntent.setAction(Intent.ACTION_MAIN);
答案 2 :(得分:-1)
在活动代码中的必需活动内的清单中设置android:launchMode =“ singleTop”。