Регистрация Android C2DM, BroadCastReceiver никогда не вызывается - PullRequest
0 голосов
/ 06 ноября 2011

У меня проблемы с реализацией регистрации C2DM.Мой BroadcastReceiver никогда не вызывается.

Я работаю с эмулятором (Google API (Google Inc.) - API уровня 8) с настроенной учетной записью Gmail.Кажется, это работает, потому что BroadcastReceiver из примера Thomas Malmsten отлично работает на моем эмуляторе ( link ).

Here is my manifest :

<permission android:name="my.package.permission.C2D_MESSAGE" android:protectionLevel="signature"></permission>
<uses-permission android:name="my.package.permission.C2D_MESSAGE" />
<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.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<uses-sdk android:minSdkVersion="8" />

<application android:name=".utils.MyApp" ...>

   <receiver android:name=".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="my.package" />
      </intent-filter>
      <!-- Receive the registration id -->
      <intent-filter>
          <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
          <catemgory android:name="my.package" />
      </intent-filter>
  </receiver>

    <activity android:name=".views.LoginScreen" android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <!-- lots of activities -->
    <!-- lots of services -->

</application>

Пока я просто хочунапишите что-нибудь в logcat.

My Broadcast Receiver
package my.package;

import ...;

public class C2DMBroadCastReceiver extends BroadcastReceiver {

    @Override
    public final void onReceive(Context context, Intent intent) {
        Log.d("C2DMBroadCastReceiver ", "Received something from Google.");
        if (intent.getAction().equals("com.google.android.c2dm.intent.REGISTRATION")) {
            Log.d("C2DMBroadCastReceiver ", "Received a registration ID from Google.");
        } else if (intent.getAction().equals("com.google.android.c2dm.intent.RECEIVE")) {
            Log.d("C2DMBroadCastReceiver ", "Received a C2DM message from Google.");
        }
    }
}

Вот моя регистрационная функция:

Context context = MyApp.getAppContext();
            Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER");
            registrationIntent.putExtra("app", PendingIntent.getBroadcast(context, 0, new Intent(), 0)); 
            registrationIntent.putExtra("sender", "myC2DMaddress@gmail.com");
            Log.w("Register", "Intent to register");
            context.startService(registrationIntent);

Наконец, logcat:

W/Register(  344): Intent to register
I/ActivityManager(   58): Starting activity: Intent { cmp=my.package/.views.Conver
sationList }
D/GoogleLoginService(  173): onBind: Intent { act=android.accounts.AccountAuthen
ticator cmp=com.google.android.gsf/.loginservice.GoogleLoginService }
D/dalvikvm(  173): GC_FOR_MALLOC freed 4630 objects / 378200 bytes in 133ms
D/dalvikvm(  344): GC_FOR_MALLOC freed 1799 objects / 279192 bytes in 95ms
D/NativeCrypto(  344): Freeing OpenSSL session
I/ActivityManager(   58): Displayed activity my.package/.views.ConversationList: 1
352 ms (total 1352 ms)
D/dalvikvm(  344): GC_EXTERNAL_ALLOC freed 2170 objects / 476328 bytes in 90ms
D/dalvikvm(  344): GC_FOR_MALLOC freed 6985 objects / 461360 bytes in 151ms
D/dalvikvm(  344): GC_FOR_MALLOC freed 612 objects / 32376 bytes in 153ms
I/dalvikvm-heap(  344): Grow heap (frag case) to 3.239MB for 87396-byte allocati
on
D/dalvikvm(  344): GC_FOR_MALLOC freed 24 objects / 1736 bytes in 51ms
D/dalvikvm(  344): GC_FOR_MALLOC freed 0 objects / 0 bytes in 146ms
I/dalvikvm-heap(  344): Grow heap (frag case) to 3.321MB for 87396-byte allocati
on
D/dalvikvm(  344): GC_FOR_MALLOC freed 0 objects / 0 bytes in 53ms
D/NativeCrypto(  344): Freeing OpenSSL session
D/dalvikvm(  344): GC_FOR_MALLOC freed 1080 objects / 244032 bytes in 122ms
I/global  (  344): Default buffer size used in BufferedReader constructor. It wo
uld be better to be explicit if an 8k-char buffer is required.
D/dalvikvm(  344): GC_FOR_MALLOC freed 1077 objects / 120272 bytes in 53ms
I/dalvikvm-heap(  344): Grow heap (frag case) to 3.506MB for 87396-byte allocati
on
D/dalvikvm(  344): GC_FOR_MALLOC freed 375 objects / 24096 bytes in 52ms
D/dalvikvm(  344): GC_FOR_MALLOC freed 3 objects / 104 bytes in 49ms
I/dalvikvm-heap(  344): Grow heap (frag case) to 3.567MB for 87396-byte allocati
on
D/dalvikvm(  344): GC_FOR_MALLOC freed 0 objects / 0 bytes in 146ms
D/dalvikvm(  344): GC_FOR_MALLOC freed 1 objects / 16 bytes in 49ms
I/dalvikvm-heap(  344): Grow heap (frag case) to 3.733MB for 87396-byte allocati
on
D/dalvikvm(  344): GC_FOR_MALLOC freed 0 objects / 0 bytes in 146ms
I/global  (  344): Default buffer size used in BufferedReader constructor. It wo
uld be better to be explicit if an 8k-char buffer is required.

Извините за весь код,но я действительно не знаю, что происходит!

Спасибо, Матье

ps: Это не работает и с Nexus One.

1 Ответ

0 голосов
/ 13 ноября 2011

Действительно глупая ошибка: catemgory вместо категории в манифесте.

...