android - 无法启动服务 Intent { act=com.google.android.c2dm.intent.REGISTRATION(有附加功能)} U=0 : not found

标签 android google-cloud-messaging

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

我试过下面的代码,它给了我以下错误:

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

list 文件

<?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{

        }
    }

}

主要 Activity

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 级别 18 安装了 Google API。问题的原因可能是什么?

最佳答案

将此行添加到 list 中的 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>

关于android - 无法启动服务 Intent { act=com.google.android.c2dm.intent.REGISTRATION(有附加功能)} U=0 : not found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19722448/

相关文章:

android - Harmony OS 中禁止了哪些谷歌服务(这是我在我的 android 应用程序中使用的列表。)

android - 启动时结构崩溃

android-studio - 错误:Conflict with dependency 'com.google.code.findbugs:jsr305'

android - 如何在 Android 的应用程序中打开/关闭 GCM 推送通知的设置功能?

android - 卸载应用程序时在 Azure 通知中心进行检测

android - 在 Android 上创建照片共享应用程序

android - 在 flutter 测试中比较两个 PinCode 文本字段

android - adapter 的内容发生了变化但是 ListView 没有收到通知,后台没有更新

android - 从 Android 应用程序控制 DOM 元素

Android Espresso 2.0 与 Multidex 应用程序空测试套件问题