java - 谷歌云消息开发者 Android 代码崩溃

标签 java android xml android-activity google-cloud-messaging

我正在尝试使用 Google Cloud Messaging 制作一个应用程序,并且我遵循了在开发人员页面上实现 GCMclient 的所有确切步骤,代码似乎在启动时崩溃了。 Activity main 是否有错误?或者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());
            }               
        }
    });
}`

最佳答案

像这样注册您的广播接收器。

  <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" />

关于java - 谷歌云消息开发者 Android 代码崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22110171/

相关文章:

java - 如何在不使用任何内置方法或函数的情况下在恒定时间 (O(1)) 内获取字母表 (1-26) 中字符的数值/位置?

android - 当我的设备在 Wi-Fi 中的特定范围内时如何限制我的移动应用程序。我想在 Android 中制作像 sonic wall 或防火墙这样的应用程序?

java - 为什么在成功 Firebase 登录之前调用另一个 Activity 中的方法?

android - Android Studio 3.1.3 Gradle同步失败

java - 如何在“with\”转义时避免转义 &

java - 如何在 javadoc 内联标签中转义大括号,例如 {@code} 标签

java - 如何使用模数和指数创建 RSA 公钥以用于 Sign In with Apple?

android - 以编程方式创建带圆角的图层列表

java - 在java中手动为opengl创建 View (lookat)矩阵

c# - 使用 IComparer 对 xml 中的节点进行排序/排序