ant*_*014 36 android bundle android-intent android-notifications android-pendingintent
我试图从通知发送信息到被调用的活动,而从我的活动我得到null.
通知代码是:
private void showNotification() {
Intent resultIntent = new Intent(this, MainActivity.class);
if (D)
Log.d(TAG, "Id: " + Id);
resultIntent.putExtra("ineedid", deviceId);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MeterActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);
// Bundle tmp = resultIntent.getExtras();
// if (tmp == null) {
// Log.d(TAG, "tmp bundle is null");
// } else {
// long id = tmp.getLong("ineedid", -1);
// Log.d(TAG, "tmp id : " + id);
// }
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
BLEMessengerService.this)
.setSmallIcon(R.drawable.ic_action_search)
.setContentTitle("Event tracker")
.setContentText("Events received").setOngoing(true)
.setContentIntent(resultPendingIntent)
.setWhen(System.currentTimeMillis());
int mId = R.string.service_notification_start_service;
mNM.notify(mId, mBuilder.getNotification());
}
Run Code Online (Sandbox Code Playgroud)
从主要活动中获取信息的代码;
Bundle extras = getIntent().getExtras();
if (extras != null) {
long deviceID = getIntent().getLongExtra("ineedid",
-1);
if (ID == -1) {
if (D)
Log.i(TAG_D, "Wrong Id received.");
finish();
} else {
device = dataSource.getDeviceByID(deviceID);
if (D)
Log.i(TAG_D, "Get the id.");
}
} else {
if (D)
Log.d(TAG_D, "Bundle is null");
finish();
}
Run Code Online (Sandbox Code Playgroud)
我在通知收到通知之前已经验证,bundle不是null,并且它在附加内容中有id.然而,当我试图从意图中获取它时,它已经消失了.救命.
Oub*_*aan 37
在PendingIntent中使用此标志PendingIntent.FLAG_UPDATE_CURRENT
它对我有用
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Run Code Online (Sandbox Code Playgroud)
Gen*_* Bo 27
对我来说,除了设置Intent.FLAG_ACTIVITY_SINGLE_TOP之外,我还必须为意图添加一个独特的动作:
Intent resultIntent = new Intent(caller, NotificationActivity.class);
String xId = getNextUniqueId();
resultIntent.putExtra("x_id", xId);
resultIntent.setAction(xId);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
Run Code Online (Sandbox Code Playgroud)
..没有setAction(..),Extras在我的NotificationActivity收到的Intent上为null.
这篇文章有助于解释它:https://stackoverflow.com/a/3128271/2162226
ant*_*014 12
我刚刚得到答案,补充一句: resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
注意:如果添加它,resultIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
它将无法正常工作.
我还尝试了其他标志,例如"FLAG_ACTIVITY_NEW_TASK"和"FLAG_ACTIVITY_RESET_TASK_IF_NEEDED".都不在这里工作.
当一个活动启动时(第一次启动时)或重新启动时(将其移到栈顶时)getIntent().getExtra()
不会给出null
。但是,如果该活动位于堆栈的顶部,则使用来PendingIntent
启动该活动不会重新启动该活动(onCreate()
不会被调用),而是onResume()
会被调用。并且getIntent().getExtra()
将返回与启动活动的意图相关的值(即null
)。
为了更新意图,请执行以下步骤:
1)。设置FLAG_ACTIVITY_SINGLE_TOP
并FLAG_ACTIVITY_CLEAR_TOP
标记您的意图。
resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
2)。覆盖onNewIntent()
活动在哪里getIntent().getExtra()
被调用。该方法称为FLAG_ACTIVITY_SINGLE_TOP
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
}
Run Code Online (Sandbox Code Playgroud)
有关更多信息:请参阅此文档
归档时间: |
|
查看次数: |
17184 次 |
最近记录: |