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

公共类 C2DMBroadcastReceiver 扩展 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"

您的文件夹结构是 com/google/android/c2dm/ 其中包含 C2DMBroadcastReceiver 类吗?

于 2012-06-13T11:48:00.957 回答