我正在尝试运行此 GCMIntentService 代码,但我不断收到Unable to instantiate service
. 这是我的 GCMIntentService 代码:
package com.e;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences.Editor;
import android.util.Log;
import com.google.android.gcm.GCMBaseIntentService;
public class GCMIntentService extends GCMBaseIntentService {
public static String TAG = "GCMIntentService";
private static String KEY = "c2dmPref";
private static String REGISTRATION_KEY = "registrationKey";
public GCMIntentService(String senderId) {
super(senderId);
Log.d("GCMIntentService", senderId);
}
@Override
public void onRegistered(Context context, String regId){
Log.e("registration :","registration :"+regId);
if (regId != null) {
Log.d("c2dm", regId);
Editor editor = context.getSharedPreferences(KEY, Context.MODE_PRIVATE).edit();
editor.putString(REGISTRATION_KEY, regId);
editor.commit();
}
}
@Override
public void onMessage(Context context, Intent intent){
String message = intent.getExtras().getString("payload");
//String key = intent.getExtras().getString("collapse_key");
Log.e("","message : " +message);
Intent startActivity = new Intent();
startActivity.setClass(context, NotificationService.class);
startActivity.setAction(NotificationService.class.getName());
startActivity.setFlags(
Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
startActivity.putExtra("Title", "New Message");
startActivity.putExtra("Message", message);
context.startActivity(startActivity);
}
@Override
protected void onError(Context arg0, String arg1) {
Log.d("onError", arg1);
}
@Override
protected boolean onRecoverableError(Context context, String errorId){
Log.d("onRecoverableError", errorId);
return false;
}
@Override
protected void onUnregistered(Context arg0, String arg1) {
// TODO Auto-generated method stub
Log.d("onUnregistered", arg1);
}
}
这是我的清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.e"
android:versionCode="1"
android:versionName="1.0" >
<application android:icon="@drawable/mainlogo" android:label="@string/app_name">
<activity android:name=".SplashScreen" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".HomeScreen"></activity>
<activity android:name=".TabController" android:windowSoftInputMode="adjustPan"></activity>
<activity android:name=".Help" android:windowSoftInputMode="adjustPan"></activity>
<activity android:name=".Services"></activity>
<activity android:name=".About"></activity>
<activity android:name=".Inbox"></activity>
<activity android:name=".More"></activity>
<activity android:name=".Disclaimer"></activity>
<activity android:name=".Legal"></activity>
<activity android:name=".PrivacyPolicy"></activity>
<activity android:name=".Settings"></activity>
<activity android:name=".TermsOfUse"></activity>
<activity android:name=".WebPage"></activity>
<activity android:name=".DetailView"></activity>
<service android:name=".NotificationService"></service>
<service android:name=".GCMIntentService"/>
<receiver android:name="com.google.android.gcm.GCMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.e" />
</intent-filter>
</receiver>
</application>
<permission android:name="com.e.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.e.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-sdk android:minSdkVersion="10" />
</manifest>
日志猫
08-16 16:18:29.026: E/AndroidRuntime(651): FATAL EXCEPTION: main
08-16 16:18:29.026: E/AndroidRuntime(651): java.lang.RuntimeException: Unable to instantiate service com.e.GCMIntentService: java.lang.InstantiationException: com.e.GCMIntentService
08-16 16:18:29.026: E/AndroidRuntime(651): at android.app.ActivityThread.handleCreateService(ActivityThread.java:1929)
08-16 16:18:29.026: E/AndroidRuntime(651): at android.app.ActivityThread.access$2500(ActivityThread.java:117)
08-16 16:18:29.026: E/AndroidRuntime(651): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:985)
08-16 16:18:29.026: E/AndroidRuntime(651): at android.os.Handler.dispatchMessage(Handler.java:99)
08-16 16:18:29.026: E/AndroidRuntime(651): at android.os.Looper.loop(Looper.java:130)
08-16 16:18:29.026: E/AndroidRuntime(651): at android.app.ActivityThread.main(ActivityThread.java:3683)
08-16 16:18:29.026: E/AndroidRuntime(651): at java.lang.reflect.Method.invokeNative(Native Method)
08-16 16:18:29.026: E/AndroidRuntime(651): at java.lang.reflect.Method.invoke(Method.java:507)
08-16 16:18:29.026: E/AndroidRuntime(651): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-16 16:18:29.026: E/AndroidRuntime(651): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-16 16:18:29.026: E/AndroidRuntime(651): at dalvik.system.NativeStart.main(Native Method)
08-16 16:18:29.026: E/AndroidRuntime(651): Caused by: java.lang.InstantiationException: com.e.GCMIntentService
08-16 16:18:29.026: E/AndroidRuntime(651): at java.lang.Class.newInstanceImpl(Native Method)
08-16 16:18:29.026: E/AndroidRuntime(651): at java.lang.Class.newInstance(Class.java:1409)
08-16 16:18:29.026: E/AndroidRuntime(651): at android.app.ActivityThread.handleCreateService(ActivityThread.java:1926)
08-16 16:18:29.026: E/AndroidRuntime(651): ... 10 more