日志猫错误
> FATAL EXCEPTION: main
> java.lang.RuntimeException: Unable to start service mobinet.imagine.GCMServiceHandler@405656c0 with Intent { > act=com.google.android.c2dm.intent.RECEIVE cat=[mobinet.imagine] cmp=mobinet.imagine/.GCMServiceHandler (has extras) }: > java.lang.NullPointerException at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2056) at > android.app.ActivityThread.access$2800(ActivityThread.java:117)
> at android.app.ActivityThread$H.handleMessage(ActivityThread.java:998)
> at android.os.Handler.dispatchMessage(Handler.java:99)
> at android.os.Looper.loop(Looper.java:130)
> at android.app.ActivityThread.main(ActivityThread.java:3687)
> at java.lang.reflect.Method.invokeNative(Native Method)
> at java.lang.reflect.Method.invoke(Method.java:507)
> at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
> at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
> at dalvik.system.NativeStart.main(Native Method)
> at android.app.IntentService.onStart(IntentService.java:110)
> at android.app.IntentService.onStartCommand(IntentService.java:118)
> at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2043)
我在 gcm 广播接收器的 OnReceive 上收到错误
应用清单文件:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="mobinet.imagine"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<uses-permission android:name="com.google.android.c2dm.intent.REGISTRATION" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<permission
android:name="mobinet.imagine.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="mobinet.imagine.permission.C2D_MESSAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="SampleActivity"
android:label="@string/title_activity_main"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="SearchResult"
android:label="@string/title_activity_main"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="ShowProduct"
android:label="@string/title_activity_main"
android:screenOrientation="portrait" >
</activity>
<receiver
android:name="mobinet.imagine.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" /> <category android:name="mobinet.imagine" />
</intent-filter>
</receiver>
<service android:name="mobinet.imagine.GCMServiceHandler" android:enabled="true" >
</service>
</application>
</manifest>
WakefulBroadcastReceiver的代码:
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
ComponentName comp = new ComponentName("mobinet.imagine",
GCMServiceHandler.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
意图服务代码如下:
public class GCMServiceHandler extends IntentService {
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
public GCMServiceHandler() {
super("GCMServiceHandler");
ApplicationDebug.LogInfo("Inside Constructor of service");
}
@Override
protected void onHandleIntent(Intent intent) {
ApplicationDebug.LogInfo("Inside onHandleIntent");
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
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());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE
.equals(messageType)) {
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
}
}
sendNotification("Received: " + extras.toString());
}
}
else{
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
private void sendNotification(String msg) {
mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.logo)
.setContentTitle("GCM Notification")
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
}
提前致谢!