【发布时间】:2018-02-13 12:42:50
【问题描述】:
我整天都在阅读有关此内容的各种不同解决方案,但到目前为止都没有奏效。
我有一个向 ResultReceiver 发送更新的上传服务 (IntentService)。接收者创建和管理通知。
单击通知(错误或成功)后,将加载 MainActivity。但捆绑始终为空。如何更改我的代码以访问/获取捆绑包?
如果我在另一个 Activity 中,如果应用程序在后台并且应用程序已停止,则会发生这种情况。
CustomResultReceiver:
private Context mContext;
private NotificationManager mNotificationManager;
private NotificationCompat.Builder mBuilder;
public static final String RETURN_MESSAGE = "RETURN_MESSAGE";
public static final String RETURN_STATUS = "RETURN_STATUS";
private final int id = 1;
public CustomResultReceiver(Handler handler, Context context) {
super(handler);
mContext = context;
mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder = new NotificationCompat.Builder(context);
mBuilder.setContentTitle("Upload data")
.setContentText("uploading...")
.setSmallIcon(R.mipmap.ic_launcher);
}
创建通知。在 onReceiveResult 我通知通知,如果发生错误或上传成功,我添加一个 ContentIntent。
protected void onReceiveResult(int resultCode, Bundle resultData) {
super.onReceiveResult(resultCode, resultData);
String message;
boolean success;
switch (resultCode) {
case RESULT_ERROR:
message = resultData.getString(BROADCAST_MESSAGE, null);
boolean failed = resultData.getBoolean(EXTENDED_ACTION_FAILED, false);
if (failed) {
mBuilder.setProgress(0, 0, false);
mBuilder.setContentText("Aborted! Error while uploading.");
mBuilder.setContentIntent(createContentIntent(message, false));
mNotificationManager.notify(id, mBuilder.build());
}
break;
case RESULT_FINISHED:
message = resultData.getString(UPLOAD_MESSAGE, null);
success = resultData.getBoolean(UPLOAD_STATUS, false);
if (success) {
mBuilder.setProgress(0, 0, false);
mBuilder.setContentText("Upload successful");
mBuilder.setContentIntent(createContentIntent(message, true));
mNotificationManager.notify(id, mBuilder.build());
}
break;
}
}
createContentIntent() 提供带有结果消息和成功布尔值的 PendingIntent:
private PendingIntent createContentIntent(String message, boolean success) {
Intent intent = new Intent(mContext, MainActivity.class);
intent.putExtra(RETURN_MESSAGE, message);
intent.putExtra(RETURN_STATUS, success);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.setAction("NOTIFIY RESPONSE");
return PendingIntent.getActivity(mContext, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
在我正在使用的接收器中:
android.app.NotificationManager;
android.app.PendingIntent;
android.content.Context;
android.content.Intent;
android.os.Bundle;
android.os.Handler;
android.os.ResultReceiver;
android.support.v4.app.NotificationCompat;
MainActivity 是一个 android.support.v7.app.AppCompatActivity。
为了创建 PendingIntent,我已经尝试了几种不同的标志组合。
以下是清单中的一些 sn-ps:
我的权限:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-feature android:name="android.hardware.camera"/>
我通过通知打开的活动:
<activity
android:name=".views.activities.MainActivity"
android:label="@string/title_activity_main"
android:screenOrientation="landscape"
android:theme="@style/AppTheme.NoActionBar"
android:windowSoftInputMode="stateHidden">
<intent-filter>
<action android:name="android.intent.action.RUN"/>
</intent-filter>
</activity>
这里的服务:
<service android:name=".services.UploadService"/>
当我从 ResultReceiver 启动活动时,会发生以下情况:
活动开始并调用 onCreate 我检查包的地方是这样的:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_main);
...
Intent old = getIntent();
if (old != null) {
Bundle extras = old.getExtras();
if (extras != null) {
Log.d(MainActivity.class.getSimpleName(), "Message: " + extras.getString(CustomResultReceiver.RETURN_MESSAGE, "empty"));
Log.d(MainActivity.class.getSimpleName(), "Status: " + extras.getBoolean(CustomResultReceiver.RETURN_STATUS, false));
}
}
...
}
【问题讨论】:
-
请发布您的清单
-
并将代码发布到您希望在 extras Bundle 中找到内容的位置。
-
@DavidWasser 我添加了(可能)相关的清单 sn-ps 和 MainActivity 的 onCreate。我还尝试在未调用的 onNewIntent 方法中获取捆绑包(其他一些线程建议这样做)。我还尝试更改没有帮助的请求代码。
-
是的,我尝试了另一个代码,但没有任何改变。但现在我尝试了其他标志。现在的工作是:对于意图 ->
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);和对于 PendingIntent.getActivity() ->PendingIntent.FLAG_CANCEL_CURRENT -
如果您对我为什么不工作感兴趣,我添加了一个答案。
标签: android android-notifications android-pendingintent