0

我在我的新 Android 应用程序上使用 GCM 服务,我的代码做得很好,但是当我在我的手机上运行项目时,我遇到了项目意外停止的异常,但是当我评论这行代码时

 GCMRegistrar.checkDevice(this);
    GCMRegistrar.checkManifest(this); 

错误停止了,但是当我运行项目时发生错误,当我检查我的谷歌 API 以检查错误是否发送到我的谷歌 API 但我没有找到任何报告为什么???

GCMIntenetService 类

package com.example.elarabygroup;

import com.google.android.gcm.GCMBaseIntentService;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class GCMIntenetService extends GCMBaseIntentService {
    public static String TAG = "GCMIntentService";

    public GCMIntenetService(String senderId) {
        super(senderId);
        Log.d("GCMIntentService", senderId);
    }

    @Override
    protected void onError(Context arg0, String arg1) {
        Log.d("onError", arg1);
    }

    @Override
    protected boolean onRecoverableError(Context context, String errorId) {
        Log.d("onRecoverableError", errorId);
        return false;
    }

    @Override
    /*
    protected void onMessage(Context arg0, Intent arg1) {
        Log.d("onMessage", String.valueOf(arg1));
    }
    */
    protected void onMessage(Context arg0, Intent arg1) {

        Log.d("GCM", "RECIEVED A MESSAGE");
        // Get the data from intent and send to notificaion bar
        generateNotification(arg0, arg1.getStringExtra("**notificaion**"));
    }

    private void generateNotification(Context arg0, String stringExtra) {
        // TODO Auto-generated method stub

    }

    @Override
    protected void onRegistered(Context arg0, String arg1) {
        Log.d("onRegistered", arg1);
    }

    @Override
    protected void onUnregistered(Context arg0, String arg1) {
        Log.d("onUnregistered", arg1);
    }
}

显现

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.elarabygroup"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <permission
        android:name="com.example.elarabygroup.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.example.elarabygroup.permission.C2D_MESSAGE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <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:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".ElarabyGroup"
            android:label="@string/title_activity_elaraby_group" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

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

                <category android:name="com.example.elarabygroup" />
            </intent-filter>
        </receiver>

        <service android:name=".GCMIntentService" />
    </application>

</manifest>

活动

package com.example.elarabygroup;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;
import com.google.android.gcm.GCMRegistrar;

public class ElarabyGroup extends Activity {
    private String TAG;
    private String SENDER_ID = "xxxxxxxx";
    private WebView webView;

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

        GCMRegistrar.checkDevice(this);
        GCMRegistrar.checkManifest(this);

        final String regId = GCMRegistrar.getRegistrationId(this);
        if (regId.equals("")) {
            GCMRegistrar.register(this, "xxxxxxxx");
        } else {
            Log.v(TAG, "Already registered");
        }

        try {

            ConnectivityManager con = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

            if (con.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED
                    && con.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED) {
                AlertDialog.Builder builder = new AlertDialog.Builder(this);

                builder.setMessage("No Internet connection");
                AlertDialog alert = builder.create();
                alert.show();

            } else

            {

                webView = (WebView) findViewById(R.id.webView1);
                webView.getSettings().setJavaScriptEnabled(true);
                webView.loadUrl("http://m.elarabygroup.com");
            }

        } catch (Exception e) {

            AlertDialog.Builder builder = new AlertDialog.Builder(this);

            builder.setMessage(e.getMessage().toString());

            AlertDialog alert = builder.create();

            String url = "http://m.elarabygroup.com/";

            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse(url));
            startActivity(i);

        }

    }

}
/*
 * @Override public boolean onCreateOptionsMenu(Menu menu) {
 * getMenuInflater().inflate(R.menu.activity_elaraby_group, menu); return true;
 * } }
 */
4

1 回答 1

1

如果日志可用我们可以找到问题,因此尝试通过在您的手机中安装一些应用程序(如aLogCat)来获取日志。

如果你不能得到那个,参考这个教程

http://androidv5.wordpress.com/2012/08/15/how-to-implement-google-cloud-messaging/

于 2012-09-10T08:55:24.893 回答