我正在尝试将C2DM客户端用于本教程:http://www.vogella.de/articles/AndroidCloudToDeviceMessaging/article.html
我正在为客户端使用此代码(与教程代码略有不同):http://www.vogella.de/code/de.vogella.android.c2dm/codestartpage.html
我只讨论了包名,我将两个包合并为一个com.DemoC2DM
,并且我在清单中使用了一些行来获取使用新的唯一包的代码,但可能我正在做的事情清单changued行错了,因为我在尝试获取客户端的注册ID时遇到此错误:
01-16 12:45:52.133: ERROR/Super(1517): Starting registration
01-16 12:45:52.375: DEBUG/GoogleLoginService(1226): onBind: Intent { act=android.accounts.AccountAuthenticator cmp=com.google.android.gsf/.loginservice.GoogleLoginService }
01-16 12:45:53.640: DEBUG/dalvikvm(1226): GC_FOR_MALLOC freed 7811 objects / 420368 bytes in 53ms
01-16 12:45:53.640: DEBUG/NativeCrypto(1226): Freeing OpenSSL session
01-16 12:45:53.945: WARN/DefaultRequestDirector(1226): Authentication error: Unable to respond to any of these challenges: {}
01-16 12:45:53.945: DEBUG/C2DMRegistrar(1226): [C2DMRegistrar.25] register: http error 401
01-16 12:45:53.945: ERROR/C2DMRegistrar(1226): [C2DMReg] handleRequest caught org.apache.http.auth.AuthenticationException
这是我清单的代码:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.DemoC2DM" android:versionCode="1"
android:versionName="1.0">
<permission android:name="com.DemoC2DM.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.DemoC2DM.permission.C2D_MESSAGE" />
<!-- Permissions -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name="RegisterActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".C2DMReceiver" />
<!-- Only C2DM servers can send messages for the app. If permission is
not set - any other app can generate it -->
<receiver android:name="com.google.android.c2dm.C2DMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<!-- Receive the actual message -->
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="de.vogella.android.c2dm" />
</intent-filter>
<!-- Receive the registration id -->
<intent-filter>
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="de.vogella.android.c2dm" />
</intent-filter>
</receiver>
</application>
</manifest>
这是原始清单的代码,没有我的包更改:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.vogella.android.c2dm" android:versionCode="1"
android:versionName="1.0">
<permission android:name="de.vogella.android.c2dm.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="de.vogella.android.c2dm.permission.C2D_MESSAGE" />
<!-- Permissions -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name="RegisterActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".C2DMReceiver" />
<!-- Only C2DM servers can send messages for the app. If permission is
not set - any other app can generate it -->
<receiver android:name="com.google.android.c2dm.C2DMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<!-- Receive the actual message -->
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="de.vogella.android.c2dm" />
</intent-filter>
<!-- Receive the registration id -->
<intent-filter>
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="de.vogella.android.c2dm" />
</intent-filter>
</receiver>
</application>
</manifest>
编辑:在Sumant更改后添加例外:
01-16 13:16:39.187: DEBUG/C2DMRegistrar(1226): [C2DMRegistrar.25] register: http error 401
01-16 13:16:39.187: ERROR/C2DMRegistrar(1226): [C2DMReg] handleRequest caught org.apache.http.auth.AuthenticationException
01-16 13:16:39.218: DEBUG/GoogleLoginService(1226): onBind: Intent { act=android.accounts.AccountAuthenticator cmp=com.google.android.gsf/.loginservice.GoogleLoginService }
01-16 13:16:39.289: WARN/ActivityManager(1086): Unable to start service Intent { act=com.motorola.motosync.service.intent.action.CHECK_PING flg=0x4 cmp=com.motorola.motosync/.service.EasService (has extras) }: not found
01-16 13:16:39.304: DEBUG/GoogleLoginService(1226): onBind: Intent { act=android.accounts.AccountAuthenticator cmp=com.google.android.gsf/.loginservice.GoogleLoginService }
01-16 13:16:40.218: DEBUG/dalvikvm(1226): GC_FOR_MALLOC freed 8422 objects / 453352 bytes in 71ms
01-16 13:16:40.523: DEBUG/AndroidRuntime(1535): Shutting down VM
01-16 13:16:40.523: WARN/dalvikvm(1535): threadid=1: thread exiting with uncaught exception (group=0x4001d7e0)
01-16 13:16:40.539: ERROR/AndroidRuntime(1535): FATAL EXCEPTION: main
01-16 13:16:40.539: ERROR/AndroidRuntime(1535): java.lang.RuntimeException: Unable to instantiate receiver com.google.android.c2dm.C2DMBroadcastReceiver: java.lang.ClassNotFoundException: com.google.android.c2dm.C2DMBroadcastReceiver in loader dalvik.system.PathClassLoader[/data/app/com.DemoC2DM-2.apk]
01-16 13:16:40.539: ERROR/AndroidRuntime(1535): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2789)
01-16 13:16:40.539: ERROR/AndroidRuntime(1535): at android.app.ActivityThread.access$3200(ActivityThread.java:125)
01-16 13:16:40.539: ERROR/AndroidRuntime(1535): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2083)
01-16 13:16:40.539: ERROR/AndroidRuntime(1535): at android.os.Handler.dispatchMessage(Handler.java:99)
01-16 13:16:40.539: ERROR/AndroidRuntime(1535): at android.os.Looper.loop(Looper.java:123)
01-16 13:16:40.539: ERROR/AndroidRuntime(1535): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-16 13:16:40.539: ERROR/AndroidRuntime(1535): at java.lang.reflect.Method.invokeNative(Native Method)
01-16 13:16:40.539: ERROR/AndroidRuntime(1535): at java.lang.reflect.Method.invoke(Method.java:521)
01-16 13:16:40.539: ERROR/AndroidRuntime(1535): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
01-16 13:16:40.539: ERROR/AndroidRuntime(1535): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
01-16 13:16:40.539: ERROR/AndroidRuntime(1535): at dalvik.system.NativeStart.main(Native Method)
01-16 13:16:40.539: ERROR/AndroidRuntime(1535): Caused by: java.lang.ClassNotFoundException: com.google.android.c2dm.C2DMBroadcastReceiver in loader dalvik.system.PathClassLoader[/data/app/com.DemoC2DM-2.apk]
01-16 13:16:40.539: ERROR/AndroidRuntime(1535): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
01-16 13:16:40.539: ERROR/AndroidRuntime(1535): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
01-16 13:16:40.539: ERROR/AndroidRuntime(1535): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
01-16 13:16:40.539: ERROR/AndroidRuntime(1535): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2780)
01-16 13:16:40.539: ERROR/AndroidRuntime(1535): ... 10 more
01-16 13:16:40.562: WARN/ActivityManager(1086): Force finishing activity com.DemoC2DM/.RegisterActivity
答案 0 :(得分:0)
您只需在清单文件中进行以下更改即可。然后尝试使用。
<!-- Only C2DM servers can send messages for the app. If permission is
not set - any other app can generate it -->
<receiver android:name="com.google.android.c2dm.C2DMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<!-- Receive the actual message -->
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.DemoC2DM" />
</intent-filter>
<!-- Receive the registration id -->
<intent-filter>
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.DemoC2DM" />
</intent-filter>
</receiver>