无法启动服务 Intent { act=com.google.android.c2dm.intent.REGISTRATION (has extras) } U=0:未找到。

10

我正在尝试获取设备的注册ID(使用模拟器进行测试),以便使用GCM设置推送通知。

我已经尝试了以下代码,但是会出现以下错误:

 Unable to start service Intent { act=com.google.android.c2dm.intent.REGISTRATION (has extras) } U=0: not found

清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.me.pushnotifications"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.GET_ACCOUNTS"/>
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>

    <permission android:name="com.me.pushnotifications.permission.C2D_MESSAGE" android:protectionLevel="signature" />
    <uses-permission android:name="com.me.pushnotifications.permission.C2D_MESSAGE"  />


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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <receiver android:name="com.me.pushnotifications.MyBroadcastReceiver"
        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.me.pushnotifications"/>
        </intent-filter>

        </receiver>


        <activity
            android:name="com.me.pushnotifications.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>
    </application>

</manifest>

我正在使用BroadcastReceiver实现这个目的:

package com.me.pushnotifications;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class MyBroadcastReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context arg0, Intent intent) {
        // TODO Auto-generated method stub
        String action = intent.getAction();
        Log.i("action::",action);
        try{
            if(action.equals("com.google.android.c2dm.intent.REGISTRATION")){
                String regId = intent.getStringExtra("registration_id");
                String error = intent.getStringExtra("error");
                String unregistered = intent.getStringExtra("unregistered");
                Log.i("regId::",regId);
            }
            else if(action.equals("com.google.android.c2dm.intent.RECEIVE")){
                String data = intent.getStringExtra("data");
                Log.i("data::",data);
            }
        }
        finally{

        }
    }

}

主活动

package com.me.pushnotifications;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Context context = getApplicationContext();

        final String appId = getResources().getString(R.string.appId) ;
        Log.i("appName::",appId);
        Button activateButton,deactivateButton;
        activateButton = (Button)findViewById(R.id.activateButton);
        deactivateButton = (Button)findViewById(R.id.deactivateButton);

        activateButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Log.i("activate::","clicked");
                Intent regIntent = new Intent("com.google.android.c2dm.intent.REGISTRATION");
                Log.i("intent::","created");
                regIntent.putExtra("app", PendingIntent.getBroadcast(context, 0,
                        new Intent(), 0));
regIntent.putExtra("sender", "registration_id");
                startService(regIntent);
            }
        });

        deactivateButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Log.i("deactivate::","clicked");
                Intent unregIntent = new Intent("com.google.android.c2dm.intent.UNREGISTER");
                Log.i("intent::","created");
                unregIntent.putExtra("app", PendingIntent.getBroadcast(context, 0,
                        new Intent(), 0));
                startService(unregIntent);
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

我已经安装了API Level 18的Google API,问题可能出在哪里?


你在清单文件中声明了 .GCMIntentService 吗? - Siddharth_Vyas
我正在使用一个 BroadcastReceiver 而不是 GCMIntentService。 - Gaurav Sood
请查看以下链接:http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/ - Siddharth_Vyas
你在什么时候遇到了那个错误?能否将logcat中更多的代码行包含进来? - Eran
尝试在您的设备上更新Google服务 @Gaurav Sood - Vikash Bijarniya
2个回答

1

Use like this:

<receiver
    android:name="com.example.gcmclient.GcmBroadcastReceiver"
    android:permission="com.google.android.c2dm.permission.SEND" >
    <intent-filter>
        <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        <!-- Receives the registration id. -->
        <action android:name="com.google.android.c2dm.intent.REGISTER" />

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

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

<service android:name=".GcmIntentService" />

1

在清单文件的intent-filter标签中添加此行

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

像这样
<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.androidhive.pushnotifications" />
            </intent-filter>

@user2739737,你是否从ADT的extras文件夹中导入了gcm.jar?如果没有,请先安装Google Play服务,进入Android SDK管理器->extras->Google Play服务。然后将gcm.jar文件放到您的lib文件夹中(/Users/SS/Desktop/Android/adt-bundle-mac-x86_64-20130717/sdk/extras/google/gcm/gcm-client/dist),gcm.jar路径。清理您的项目,然后运行。 - Gajendra Rawat
请查看此链接:http://stackoverflow.com/questions/19678787/cant-import-import-com-google-android-gcm-gcmbaseintentservice/19678841#19678841 - Gajendra Rawat
我已经将GCM JAR文件导入到libs中,但似乎仍然无法正常工作。 - Gaurav Sood
2
@user2739737 将 com.google.android.c2dm.intent.REGISTER 替换为 com.google.android.c2dm.intent.REGISTRATION。 - Gajendra Rawat

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接