我正在关注http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/上的 GCM 指南。
目前我被困在接收通知的部分。有时当我运行我的代码时,它会因以下错误而崩溃:
7.517:E/AndroidRuntime(8601):致命异常:主要 7.517:E/AndroidRuntime(8601):java.lang.RuntimeException:无法实例化接收器 com.google.android.gcm.GCMBroadcastReceiver:java.lang.ClassNotFoundException:没有' t 在路径上找到类“com.google.android.gcm.GCMBroadcastReceiver”:DexPathList[[zip file "/data/app/com.example.someactivity-1.apk"],nativeLibraryDirectories=[/data/app-lib/ com.example.someactivity-1, /vendor/lib, /system/lib]] 7.517: E/AndroidRuntime(8601): 在 android.app.ActivityThread.handleReceiver(ActivityThread.java:2544) 7.517: E/AndroidRuntime(8601) ): 在 android.app.ActivityThread.access$1500(ActivityThread.java:153) 7.517: E/AndroidRuntime(8601): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1345) 7.517: E/AndroidRuntime( 8601):在 android.os。Handler.dispatchMessage(Handler.java:99) 7.517: E/AndroidRuntime(8601): 在 android.os.Looper.loop(Looper.java:137) 7.517: E/AndroidRuntime(8601): 在 android.app.ActivityThread。 main(ActivityThread.java:5295) 7.517: E/AndroidRuntime(8601): 在 java.lang.reflect.Method.invokeNative(Native Method) 7.517: E/AndroidRuntime(8601): 在 java.lang.reflect.Method.invoke (Method.java:525) 7.517: E/AndroidRuntime(8601): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739) 7.517: E/AndroidRuntime(8601): 在 com.android .internal.os.ZygoteInit.main(ZygoteInit.java:555) 7.517: E/AndroidRuntime(8601): at dalvik.system.NativeStart.main(Native Method) 7.517: E/AndroidRuntime(8601): Caused by: java. lang.ClassNotFoundException:找不到类“com.google.android.gcm。GCMBroadcastReceiver" 路径:DexPathList[[zip file "/data/app/com.example.someactivity-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.someactivity-1, /vendor/lib , /system/lib]] 7.517: E/AndroidRuntime(8601): 在 dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53) 7.517: E/AndroidRuntime(8601): 在 java.lang.ClassLoader.loadClass(ClassLoader .java:501) 7.517: E/AndroidRuntime(8601): 在 java.lang.ClassLoader.loadClass(ClassLoader.java:461) 7.517: E/AndroidRuntime(8601): 在 android.app.ActivityThread.handleReceiver(ActivityThread.java :2539)system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53) 7.517: E/AndroidRuntime(8601): 在 java.lang.ClassLoader.loadClass(ClassLoader.java:501) 7.517: E/AndroidRuntime(8601): 在 java.lang。 ClassLoader.loadClass(ClassLoader.java:461) 7.517: E/AndroidRuntime(8601): 在 android.app.ActivityThread.handleReceiver(ActivityThread.java:2539)system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53) 7.517: E/AndroidRuntime(8601): 在 java.lang.ClassLoader.loadClass(ClassLoader.java:501) 7.517: E/AndroidRuntime(8601): 在 java.lang。 ClassLoader.loadClass(ClassLoader.java:461) 7.517: E/AndroidRuntime(8601): 在 android.app.ActivityThread.handleReceiver(ActivityThread.java:2539)
这是我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.someactivity"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<!-- GCM connects to Internet Services. -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- GCM requires a Google account. -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<!-- Creates a custom permission so only this app can receive its messages. -->
<permission
android:name="com.example.someactivity.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.someactivity.permission.C2D_MESSAGE" />
<!-- This app has permission to register and receive data message. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- Network State Permissions to detect Internet status -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- Permission to vibrate -->
<uses-permission android:name="android.permission.VIBRATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.Sherlock.Light" >
<activity
android:name="com.example.someactivity.Login"
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="com.example.someactivity.fragmentContainer" >
</activity>
<receiver
android:name="com.google.android.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<!-- Receives the actual messages. -->
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<!-- Receives the registration id. -->
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.example.someactivity" />
</intent-filter>
</receiver>
<service android:name=".GcmIntentService" />
</application>
</manifest>
还有我使用的接收器:
private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String newMessage = intent.getExtras().getString("Some Message");
// Waking up mobile if it is sleeping
WakeLocker.acquire(getApplicationContext());
/**
* Take appropriate action on this message
* depending upon your app requirement
* For now i am just displaying it on the screen
* */
// Showing received message
Toast.makeText(getApplicationContext(), "New Message: " + newMessage, Toast.LENGTH_LONG).show();
// Releasing wake lock
WakeLocker.release();
}
};
为什么我没有实例化接收器,我已经更改了包名称。