Android BroadcastReceiver试图在非有序广播错误期间返回结果
即使我没有发送推送通知,但在第一次启动应用程序时仍会发生此错误:Android BroadcastReceiver试图在非有序广播错误期间返回结果,android,broadcastreceiver,Android,Broadcastreceiver,即使我没有发送推送通知,但在第一次启动应用程序时仍会发生此错误: BroadcastReceiver trying to return result during a non-ordered broadcast java.lang.RuntimeException: BroadcastReceiver trying to return result during a non-ordered broadcast at android.content
BroadcastReceiver trying to return result during a non-ordered broadcast
java.lang.RuntimeException: BroadcastReceiver trying to return result during a non-ordered broadcast
at android.content.BroadcastReceiver.checkSynchronousHint(BroadcastReceiver.java:799)
at android.content.BroadcastReceiver.setResultCode(BroadcastReceiver.java:565)
at com.pushnotification.GcmBroadcastReceiver.onReceive(GcmBroadcastReceiver.java:17)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2712)
at android.app.ActivityThread.access$1700(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1449)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5696)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
广播接收器的代码:
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Explicitly specify that GcmIntentService will handle the intent.
ComponentName comp = new ComponentName(context.getPackageName(),
GcmIntentService.class.getName());
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
/* the crash is pointing to this line */
setResultCode(Activity.RESULT_OK);
}
}
在我在IntentService中实现以下代码后,错误开始出现(在应用程序启动时也不会被调用)。但也不是每次,即从Android Studio卸载并运行应用程序后,有时会出现错误,有时不会
BroadcastReceiver receiver;
public void DownloadListener(final String ZipFile) {
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(downloadReference);
Cursor c = downloadManager.query(query);
if (c.moveToFirst()) {
int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {
DismissProgressDialog();
ShowProgress("Almost Done", "Unzipping And Installing Database", pd.STYLE_SPINNER);
}
}
}
}
};
context.registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
在舱单中:
<receiver
android:name="com.pushnotification.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.geovision.ffmsnativeprototype" />
</intent-filter>
</receiver>
<service android:name=".WebServiceCommunication.SystemDatabaseService" />
根据答案,我的情况与相同
这是一个与谷歌相关的功能,不是什么大问题,可以通过编程进行过滤
@Override
protected void onHandleIntent(Intent intent) //RECEIVE THE PUSHNOTIFICATION
{
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
// The getMessageType() intent parameter must be the intent you received
// in your BroadcastReceiver.
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) { // has effect of unparcelling Bundle
/*
* Filter messages based on message type. Since it is likely that GCM
* will be extended in the future with new message types, just ignore
* any message types you're not interested in, or that you don't
* recognize.
*/
if (GoogleCloudMessaging.
MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
// sendNotification("Send error: " + extras.toString());
} else if (GoogleCloudMessaging.
MESSAGE_TYPE_DELETED.equals(messageType)) {
// sendNotification("Deleted messages on server: " +
// extras.toString());
// If it's a regular GCM message, do some work.
} else if (GoogleCloudMessaging.
MESSAGE_TYPE_MESSAGE.equals(messageType)) {
Log.i(TAG, "PUSHNOTIFICATION RECEIVED @ " + SystemClock.elapsedRealtime());
extraData=extras.getString("extraData");
from=extras.getString("from");
title=extras.getString("title");
message=extras.getString("message");
Log.i(TAG, "Received: " + extras.toString()+" M"+messageType);
if(title!=null) {
这里的根本问题是GCM API已经更改。正如OP所指出的,您可以简单地过滤掉这个新的意图,但是这样做您将错过令牌刷新通知 从更新的GCM AndroidManifest.xml
<service
android:name=".MyInstanceIdListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID"/>
</intent-filter>
</service>
解决此问题的正确方法是按照更新的API中的详细说明更新新API的实现
对于扩展WakefulBroadcastReceiver的现有应用程序,Google
建议迁移到GCMReceiver和GcmListenerService。到
迁移:
在应用程序清单中,将GcmBroadcastReceiver替换为
“com.google.android.gms.gcm.GcmReceiver”,并替换当前
将IntentService扩展到新服务的服务声明
GcmListenerService
从客户端代码中删除BroadcastReceiver实现
重构当前IntentService服务实现以使用
GcmListenerService
有关详细信息,请参阅本页中的示例清单和代码示例
我发布了一个相关问题的答案,该问题从旧实现中提取了更新所需的所有相关代码。我看到的答案可能重复,但没有帮助您是否尝试删除setResultCode()?是的,我尝试过。请参阅我的编辑如果你想让我发布pushnotification意图服务的OnHandleContent代码(从GcmBroadcastReceiver调用),我也会面临这个错误。虽然这看起来无害,但你能告诉我如何防止这个错误突然出现吗?谢谢你的回答。这是正确的处理方法吗?@JithuP.S不,我不相信这是正确的处理方法。请看我的答案。
if(from.equals("google.com/iid"))
{
//related to google ... DO NOT PERFORM ANY ACTION
}
else {
//HANDLE THE RECEIVED NOTIFICATION
}
<service
android:name=".MyInstanceIdListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID"/>
</intent-filter>
</service>
public class MyInstanceIDListenerService extends InstanceIDListenerService {
private static final String TAG = "MyInstanceIDLS";
/**
* Called if InstanceID token is updated. This may occur if the security of
* the previous token had been compromised. This call is initiated by the
* InstanceID provider.
*/
// [START refresh_token]
@Override
public void onTokenRefresh() {
// Fetch updated Instance ID token and notify our app's server of any changes (if applicable).
Intent intent = new Intent(this, RegistrationIntentService.class);
startService(intent);
}
// [END refresh_token]
}