2012-03-20 62 views
0

我已經編寫了註冊ID爲c2dm的應用程序。但我得到異常,因爲無法在startService(intent)行啓動活動組件信息;錯誤: - 獲取c2dm的註冊ID時出現錯誤

我的主類是

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", "[email protected]"); 
     startService(intent); 


    } 
} 

我的收款人類是

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> 
+0

什麼錯誤,你得到了什麼?添加異常的堆棧跟蹤 – Houcine 2012-03-20 14:51:18

+0

03-20 20:01:54.703:E/AndroidRuntime(1881):java.lang.RuntimeException:無法啓動活動ComponentInfo {com.IdTest/com.IdTest.IdTestActivity}:java.lang .SecurityException:不允許啓動服務Intent {act = com.google.android.c2dm.intent.REGISTER(has extras)}未經許可com.google.android.c2dm.permission.RECEIVE 03-20 20:01:54.703 :E/AndroidRuntime(1881):\t at com.IdTest.IdTestActivity.onCreate(IdTestActivity.java:32) – user1196969 2012-03-20 14:53:49

回答

0

您應該添加到reveive您清單從C2DM服務器這樣的消息的權限:

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

我想你應該添加註冊意向過濾器到您的服務,由此你可以得到註冊ID

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

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

你也可以得到即將到來的regi共探ID使用

  intent.getStringExtra("registration_id"); 

,希望能幫助你