Android的GCM无法启动服务意向 [英] Android GCM Unable to start service Intent

查看:192
本文介绍了Android的GCM无法启动服务意向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我想作一个简单的通知应用程序。
我使用这个( http://www.youtube.com/watch?v=rmzv716SYkQ)教程。
这里是我的codeS。

Hello I want to make a simple notification application. I am using this (http://www.youtube.com/watch?v=rmzv716SYkQ) tutorial. here is my codes.

清单文件

 <uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />
 <uses-permission android:name="android.permission.INTERNET" />
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 <uses-permission android:name="android.permission.GET_ACCOUNTS"/>
 <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

<permission
android:name="com.myapp.ntfapp.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.turk.bakistik.permission.C2D_MESSAGE" /> 
<uses-permission android:name="android.permission.WAKE_LOCK"/>

在应用程序块的清单文件

 <receiver
                android:name=".MyNotificationService"
                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.myapp.ntfapp" />
                </intent-filter>
            </receiver> 

MyNotificationService类

public class MyNotificationService extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    try {
        String action = intent.getAction();
        if (action.equals("com.google.android.c2dm.intent.REGISTRATION")) {
            String registrationId = intent.getStringExtra("registration_id");
            Log.i("ui",registrationId);
            String error = intent.getStringExtra("error");
            String unregistered = intent.getStringExtra("unregistered"); 
            }
        else if (action.equals("com.google.android.c2dm.intent.RECEIVE")) {
            String data1 = intent.getStringExtra("data1");
            String data2 = intent.getStringExtra("data2");
        }
    } finally { }}

启用及停用的主要活动按钮

button2=(Button)findViewById(R.id.button2); 
button3=(Button)findViewById(R.id.button3); 
    button2.setOnClickListener(new View.OnClickListener() 
    {   
        public void onClick(View v) 
        {   
            Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER");
            registrationIntent.putExtra("app", PendingIntent.getBroadcast(v.getContext(), 0, new Intent(), 0));
            registrationIntent.putExtra("sender", "123456789101112");
            startService(registrationIntent);   
        }
    });

    button3.setOnClickListener(new View.OnClickListener() 
    {   
        public void onClick(View v) 
        {   
            Intent unregIntent = new Intent("com.google.android.c2dm.intent.UNREGISTER");
            unregIntent.putExtra("app", PendingIntent.getBroadcast(v.getContext(), 0, new Intent(), 0));
            startService(unregIntent);  
        }}); }

当我点击激活按钮
W / ActivityManager(70):无法启动服务意向{行动= com.google.android.c2dm.intent.REGISTRATION(有演员)}:找不到

When I click activate button W/ActivityManager(70): Unable to start service Intent { act=com.google.android.c2dm.intent.REGISTRATION (has extras) }: not found

我怎样才能解决这个问题呢?

How can I fix this problem?

推荐答案

我看到的不一致在你的清单:

I see an inconsistency in your manifest :

<permission
android:name="com.myapp.ntfapp.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.turk.bakistik.permission.C2D_MESSAGE" />

您应该使用应用的包名都(无论是 com.myapp.ntfapp <​​/ code>
com.turk.bakistik )。

You should use your app's package name in both (either com.myapp.ntfapp or com.turk.bakistik).

此外,您使用的是旧的和过时的方式注册到GCM。您应该使用GoogleCloudMessaging.register方法​​,它是谷歌Play服务库的一部分。

In addition, you are using an old and obsolete way to register to GCM. You should use the GoogleCloudMessaging.register method which is part of the Google Play Services library.

这篇关于Android的GCM无法启动服务意向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆