ComponentName - 无法启动服务Intent - GCM

时间:2015-09-06 09:25:24

标签: android android-intent android-service

我遇到问题,更改包名后我无法通过WakefulBroadcastReceiver启动服务。我正在选择这个问题并且无法解决它。

我的应用程序包名称与我拥有源文件的包不同。推送到应用程序,但当我尝试startWakefulService时,问题出现在这些行中:

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent)
    {       
        ComponentName comp = new ComponentName( context.getPackageName(), GcmIntentService.class.getName());        
        LogShower.printLogs("GcmBroadcastReceiver"+ "onReceive");       
        startWakefulService(context, (intent.setComponent(comp)));

        setResultCode(Activity.RESULT_OK);
    }   
}

我的应用程序包的名称,然后是我的服务名称,所以应该是一切。

这是logcat中出现的错误:

    09-05 18:01:12.770: W/ActivityManager(2246): 
Unable to start service Intent { act=com.google.android.c2dm.intent.RECEIVE flg=0x10
 pkg=com.jun cmp=com.jun/com.syr.juni.pushes.GcmIntentService (has extras) } U=0: not found

Manifest看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.jun"
    android:versionCode="4"
    android:versionName="1.0" >

    <supports-screens
        android:largeScreens="true"
        android:normalScreens="false"
        android:requiresSmallestWidthDp="600"
        android:smallScreens="false"
        android:xlargeScreens="true" />

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

    <uses-sdk
        android:minSdkVersion="17"
        android:targetSdkVersion="22" />

    <uses-permission android:name="android.permission.INTERNET" />
    <!-- GCM requires a Google account. -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />

    <!-- Keeps the processor from sleeping when a message is received. -->
    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <!-- Creates a custom permission so only this app can receive its messages. -->
    <permission
        android:name="com.jun.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.jun.permission.C2D_MESSAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <!-- This app has permission to register and receive data message. -->
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

    <!-- Network State Permissions to detect Internet status -->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <!-- Permission to vibrate -->
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.MANAGE_DOCUMENTS" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <receiver
            android:name="com.syr.juni.pushes.GcmBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>

                <!-- Receives the actual messages. -->
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />

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

        <service android:name="com.syr.juni.pushes.GcmIntentServiceService" />

        <activity
            android:name="com.syr.juni.viewbackend.ChooseScreen"
            android:screenOrientation="landscape"
            android:windowSoftInputMode="adjustResize" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.syr.juni.viewbackend.MainScreen"
            android:exported="true"
            android:screenOrientation="landscape" >
        </activity>
    </application>

</manifest>

我看过类似的帖子,但是已经知道答案已经知道我的包装有问题了。

1 个答案:

答案 0 :(得分:0)

我认为你应该创建自己的Intent,而不是从参数中重用Intent。

如果您想将extras传递给该服务,可以将其克隆。

@Override
public void onReceive(Context context, Intent intent)
{       
    Intent service = new Intent(context, GcmIntentService.class);
    service.putExtras(new Bundle(intent.getExtras())); //clone the extras
    LogShower.printLogs("GcmBroadcastReceiver"+ "onReceive");       
    startWakefulService(context, service);

    setResultCode(Activity.RESULT_OK);
}