2014-03-01 49 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