Android GCM:无法启动意向服务(有附加功能)

时间:2014-02-28 12:37:40

标签: android android-intent google-cloud-messaging android-intentservice

Log-cat 错误

> 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() {
}

提前致谢!

2 个答案:

答案 0 :(得分:1)

它认为问题位于 GCMServiceHandler onCreate()方法中。 如果重写onCreate()方法,则不能将其留空。你应该写 super.onCreate();

所以看起来应该是这样的:

    @Override
    public void onCreate() {
        super.onCreate();
    }

David Wasser解释了here解释为什么你不能把这个留空的原因:

  实例化Service对象时(即:创建服务时),将调用

onCreate()。你应该在这个方法中做一些你只需要做一次的事情(即:初始化一些变量等)。


当然,您也可以删除onCreate方法。

对于迟到的回答感到抱歉,但我希望它对你有帮助。

答案 1 :(得分:0)

额外变量在onHandleIntent方法中变为null检查您是否正在发送正确的extrats