java-注册推送通知期间Android崩溃
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-注册推送通知期间Android崩溃,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5890字,纯文字阅读大概需要9分钟。
内容图文
![java-注册推送通知期间Android崩溃](/upload/InfoBanner/zyjiaocheng/662/9d60cfcaa6394e55beb63dba80aafae1.jpg)
我正在使用gcm.jar进行推送通知.当我启动我的应用程序时,我试图注册“推送”通知,但始终崩溃.
06-04 10:08:32.479: E/BroadcastReceiver(26774): BroadcastReceiver trying to return result during a non-ordered broadcast
06-04 10:08:32.479: E/BroadcastReceiver(26774): java.lang.RuntimeException: BroadcastReceiver trying to return result during a non-ordered broadcast
06-04 10:08:32.479: E/BroadcastReceiver(26774): at android.content.BroadcastReceiver.checkSynchronousHint(BroadcastReceiver.java:783)
06-04 10:08:32.479: E/BroadcastReceiver(26774): at android.content.BroadcastReceiver.setResult(BroadcastReceiver.java:658)
06-04 10:08:32.479: E/BroadcastReceiver(26774): at com.google.android.gcm.GCMBroadcastReceiver.onReceive(GCMBroadcastReceiver.java:56)
06-04 10:08:32.479: E/BroadcastReceiver(26774): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2394)
06-04 10:08:32.479: E/BroadcastReceiver(26774): at android.app.ActivityThread.access$1500(ActivityThread.java:145)
06-04 10:08:32.479: E/BroadcastReceiver(26774): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1322)
06-04 10:08:32.479: E/BroadcastReceiver(26774): at android.os.Handler.dispatchMessage(Handler.java:99)
06-04 10:08:32.479: E/BroadcastReceiver(26774): at android.os.Looper.loop(Looper.java:137)
06-04 10:08:32.479: E/BroadcastReceiver(26774): at android.app.ActivityThread.main(ActivityThread.java:5095)
06-04 10:08:32.479: E/BroadcastReceiver(26774): at java.lang.reflect.Method.invokeNative(Native Method)
06-04 10:08:32.479: E/BroadcastReceiver(26774): at java.lang.reflect.Method.invoke(Method.java:511)
06-04 10:08:32.479: E/BroadcastReceiver(26774): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
06-04 10:08:32.479: E/BroadcastReceiver(26774): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
06-04 10:08:32.479: E/BroadcastReceiver(26774): at dalvik.system.NativeStart.main(Native Method)
我的Menifest.xml是
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="com.app.example.permission.C2D_MESSAGE" />
<permission
android:name="com.app.example.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<receiver
android:name="com.google.android.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<!-- Receives the registration id. -->
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<!-- Receives the actual messages. -->
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.app.example" />
</intent-filter>
</receiver>
<service android:name="com.app.example.GCMIntentService" />
我的BroadcastReciever代码如下
public class GCMIntentService extends GCMBaseIntentService {
@SuppressWarnings("hiding")
private static final String TAG = "GCMIntentService ";
public GCMIntentService() {
super(SENDER_ID);
}
@Override
protected void onRegistered(Context context, String registrationId) {
PreferenceManager.getDefaultSharedPreferences(context).edit()
.putString("REGISTRATION_KEY", registrationId).commit();
}
@Override
protected void onUnregistered(Context context, String registrationId) {
// Debuger.e(TAG, "Device unregistered");
}
@Override
protected void onMessage(Context context, Intent intent) {
handleMessage(context, intent);
}
@Override
protected void onDeletedMessages(Context context, int total) {
}
@Override
public void one rror(Context context, String errorId) {
}
@Override
protected boolean onRecoverableError(Context context, String errorId) {
// log message
return super.onRecoverableError(context, errorId);
}
private void handleMessage(Context context, Intent intent) {
// Do whatever you want with the message
String order_ID = intent.getStringExtra("order_id");
String status_ID = intent.getStringExtra("status_id");
String message = intent.getStringExtra("message");
if (!message.equals("")) {
if (order_ID != null && status_ID != null & !status_ID.equals("4")) {
Bundle bundle = new Bundle();
bundle.putInt("fromGetItem", 2);
mainIntent = new Intent(context.getApplicationContext(),
MainActivity.class);
mainIntent.putExtras(bundle);
mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(mainIntent);
}
generateNotification(context, message, mainIntent);
}
}
private static void generateNotification(Context context, String message,
Intent notificationIntent) {
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}
}
如何解决此崩溃?
解决方法:
我认为您正在获取并处理您的消息
@Override
protected void onMessage(Context context, Intent intent) {
handleMessage(context, intent);
}
当App向Google发送请求进行设备注册的请求时,我不太了解,但有时Google返回消息,因此,如果您正在解析消息和Intent,则不会收到您的Bundle,它会返回null,因此App崩溃.
您可以尝试使用以下代码,可能会有所帮助.
@Override
protected void onMessage(Context context, Intent intent) {
//Bundle[{CMD=RST_FULL, from=google.com/iid}]
Bundle bundle = intent.getExtras();
if (bundle != null) {
if (!bundle.containsKey("CMD")) {
handleMessage(context, intent);
}
}
}
内容总结
以上是互联网集市为您收集整理的java-注册推送通知期间Android崩溃全部内容,希望文章能够帮你解决java-注册推送通知期间Android崩溃所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。