0

コード スニペット:

C2DMessaging.java

public class C2DMessaging {
public static final String EXTRA_SENDER = "sender";
public static final String EXTRA_APPLICATION_PENDING_INTENT = "app";
public static final String REQUEST_UNREGISTRATION_INTENT = "com.google.android.c2dm.intent.UNREGISTER";
public static final String REQUEST_REGISTRATION_INTENT = "com.google.android.c2dm.intent.REGISTER";
public static final String LAST_REGISTRATION_CHANGE = "last_registration_change";
public static final String BACKOFF = "backoff";
public static final String GSF_PACKAGE = "com.google.android.gsf";
public static final String REGISTRATION_ID="registrationid";

// package
static final String PREFERENCE = "wea.com.google.android.c2dm";

private static final long DEFAULT_BACKOFF = 30000;

/**
 * Initiate c2d messaging registration for the current application
 */
public static void register(Context context,
        String senderId) {
    Intent registrationIntent = new Intent(REQUEST_REGISTRATION_INTENT);
    registrationIntent.setPackage(GSF_PACKAGE);
    registrationIntent.putExtra(EXTRA_APPLICATION_PENDING_INTENT,
            PendingIntent.getBroadcast(context, 0, new Intent(), 0));
    registrationIntent.putExtra(EXTRA_SENDER, senderId);
    context.startService(registrationIntent);
    // TODO: if intent not found, notification on need to have GSF
}

/**
 * Unregister the application. New messages will be blocked by server.
 */
public static void unregister(Context context) {
    Intent regIntent = new Intent(REQUEST_UNREGISTRATION_INTENT);
    regIntent.setPackage(GSF_PACKAGE);
    regIntent.putExtra(EXTRA_APPLICATION_PENDING_INTENT, PendingIntent.getBroadcast(context,
            0, new Intent(), 0));
    context.startService(regIntent);
}

C2DMBroadcastReceiver.java

public class C2DMBroadcastReceiver extends BroadcastReceiver {

@Override
public final void onReceive(Context context, Intent intent) {
    // To keep things in one place.
    C2DMBaseReceiver.runIntentInService(context, intent);
    setResult(Activity.RESULT_OK, null /* data */, null /* extra */);        
}

}

マニフェスト ファイル

 <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.buuuk.sgweatherlah"
 android:versionCode="1"
 android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="8" />

<supports-screens
    android:anyDensity="true"
    android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<!-- for push -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

<permission
    android:name="com.buuuk.sgweatherlah.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.buuuk.sgweatherlah.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

<application
    android:name=".MyApp"
    android:icon="@drawable/ic_launcher"
    android:installLocation="preferExternal"
    android:label="@string/app_name"
    android:theme="@style/full_screen" >
    <uses-library android:name="com.google.android.maps" />

    <activity
        android:name=".WeatherLahActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
        <!-- for push -->
        <service
            android:name=".C2DMReceiver"
            android:enabled="true" />

        <!--
      Only Google services can send messages to the app. If this permission
      weren't set any other app would be able to send messages to us.
        -->
        <receiver
            android:name="com.google.android.c2dm.C2DMBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >

            <!-- Receive actual messages -->
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />

                <category android:name="com.buuuk.sgweatherlah" />
            </intent-filter>
            <!-- Receive registration ids -->
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

                <category android:name="com.buuuk.sgweatherlah" />
            </intent-filter>
        </receiver>
    </service>
</application>

問題は :

私が電話した後

C2DMessaging.register(Context context,String senderId) 

アプリを登録する方法。

しかし、C2DMBroadcastReceiver の onReceive() で何も受信していません。

構造イメージです。ここに画像を投稿することはできません。ボックスにアップロードするだけです。

https://www.box.com/s/22782990e6f0bc34bbeb

助けてください。ありがとう

4

1 に答える 1

0

マニフェスト ファイルの問題

アプリケーションタグ内にサービスタグとレシーバータグを記述する必要があります

例えば:

<application android:icon="@drawable/icon"
        android:label="@string/app_name">

<service
            android:name=".C2DMReceiver"
            android:enabled="true" />

<!--SAME FOR RECIEVER-->

</application>

もう一つ..あなたは書いています

<receiver
            android:name="com.google.android.c2dm.C2DMBroadcastReceiver"

あなたのフォルダ構造は C2DMBroadcastReceiver クラスを含む com/google/android/c2dm/ ですか?

于 2012-06-13T11:48:00.957 に答える