2015-09-06 100 views
0

我改變軟件包名稱後出現問題,我無法通過WakefulBroadcastReceiver啓動服務。我找到了這個問題,無法解決。ComponentName - 無法啓動服務意圖 - GCM

我的應用程序包名稱不同於那些包含我的源文件的包。該推來申請,但問題出現在這些行,當我嘗試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 

清單如下:

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

我看了類似的帖子,但也有其已經知道什麼是錯的答案與我的包裹。

回答

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); 
} 
相關問題