2014-03-01 88 views
0

林試圖與谷歌雲端通訊的應用程序,我followd所有開發者頁面上實現GCMclient的具體步驟,代碼只是似乎崩潰上推出。活動主體有沒有錯誤?或者android如何在創建時設置其應用程序的一些邏輯缺陷?即時通訊新的android編程。谷歌雲端通訊開發的Android代碼崩潰

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

<meta-data android:name="com.google.android.gms.version" 
     android:value="@integer/google_play_services_version" /> 

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

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.GET_ACCOUNTS" /> 
<uses-permission android:name="android.permission.WAKE_LOCK" /> 
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> 


<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name="com.iotproj.clandestine.MainActivity" 
     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="GcmBroadcastReceiver" 
     android:permission="com.google.android.c2dm.permission.SEND" > 
     <intent-filter> 
      <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
      <category android:name="com.iotproj.clandestine" /> 
     </intent-filter> 
    </receiver> 
    <service android:name="GcmIntentService" /> 

    </application> 


    </manifest> 



      public class MainActivity extends Activity { 

private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;TextView mDisplay; 
Button mButton; 

GoogleCloudMessaging gcm; 
Context appContext = getApplicationContext(); 

// this is quite important dude 
String registrationId = null; 


String SENDER_ID = "xxxxxxxxx"; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    mDisplay = (TextView) findViewById(R.id.pool); 
    mButton = (Button) findViewById(R.id.getid); 

    // Check device for Play Services APK. 
    if (!checkPlayServices()) { 

     mDisplay.setText("device not supproted !"); button.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      gcm = GoogleCloudMessaging.getInstance(appContext); 
      try{ 
       registrationId = gcm.register(SENDER_ID); 
      } 
      catch(IOException e){ 
       mDisplay.setText(e.getMessage()); 
      }    
     } 
    }); 
}` 
+0

你介意分享關於異常你會得到更多的細節? – 3yanlis1bos

+0

即時在我的手機上運行它,所以只要它部署它只是說不幸的應用程序已停止工作。你想要我分享mainactiviy.java嗎? – br0k3nc0d3

+0

雅需... – Piyush

回答

1

以這種方式註冊您的廣播接收機。

<receiver 
     android:name="com.google.android.gcm.GCMBroadcastReceiver" 
     android:permission="com.google.android.c2dm.permission.SEND" > 
     <intent-filter> 

      <!-- Receives the actual messages. --> 
      <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
      <!-- Receives the registration id. --> 
      <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> 

      <category android:name="com.iotproj.clandestine" /> 
     </intent-filter> 
</receiver> 

的服務

<service android:name=".GCMIntentService" /> 
+0

爲Android:NAME = 「com.google.android.gcm.GCMBroadcastReceiver」 我把它改成機器人:名字= 「com.iotproj.clandestine」 .GCMBroadcastReceiver」,仍然沒有工作 – br0k3nc0d3

+0

是的,你必須.... – Piyush

+0

我的源目錄結構,我有一個包的應用程序和所有三個文件都在裏面只。GcmBroadcastReceiver,GcmIntentService和MainActivity一起 – br0k3nc0d3