错误: - 获取c2dm的注册ID时出错

时间:2012-03-20 14:42:14

标签: android android-c2dm

我已经编写了获取c2dm注册ID的应用程序。但我得到的异常是因为无法在startService(intent);

行启动活动组件信息

我的主要课程是

 public class IdTestActivity extends Activity {
    static TextView mytext = null;
    Context context = null;
    Intent intent = null;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Intent intent = new Intent("com.google.android.c2dm.intent.REGISTER");
        intent.putExtra("app",PendingIntent.getBroadcast(this, 0, new Intent(), 0));
        intent.putExtra("sender", "dvimayandroid@gmail.com");
        startService(intent);


    }
}   

我的Receiver课程是

    public class c2dmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        Log.w("C2DM", "Registration Receiver called");
        if ("com.google.android.c2dm.intent.REGISTRATION".equals(action)) {
            Log.w("C2DM", "Received registration ID");
            final String registrationId = intent
                    .getStringExtra("sender");
            String error = intent.getStringExtra("error");

            Log.d("C2DM", "dmControl: registrationId = " + registrationId
                    + ", error = " + error);
            // TODO Send this to my application server
        }
    }

}

我的清单文件是

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

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

    <application>
        <permission
            android:name="com.IdTest.permission.C2D_MESSAGE"
            android:protectionLevel="signature" />

        <uses-permission android:name="com.IdTest.permission.C2D_MESSAGE" />

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver
            android:name=".c2dmReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter >
                <action android:name="com.google.android.c2dm.intent.RECEIVE" >
                </action>

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

</manifest>

3 个答案:

答案 0 :(得分:0)

您似乎需要为清单添加权限。检查这里需要什么,并确保您的应用程序包含所有内容:

http://code.google.com/android/c2dm/#manifest

答案 1 :(得分:0)

您应该在清单中添加从C2DM服务器接收邮件的权限:

 <!-- This app has permission to register and receive message -->
   <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

答案 2 :(得分:0)

我认为您应该将注册意图过滤器添加到您的服务中,您可以获得注册ID

        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

            <category android:name="your pacakge" />
        </intent-filter>

您也可以获得即将使用的注册ID

         intent.getStringExtra("registration_id");

希望能帮到你