2012-02-10 82 views
-1

我想從一個需要「android.intent.action.BOOT_COMPLETED」的廣播接收器啓動服務。當我在eclipse上點擊應用程序並啓動時,它運行良好。但是當我從它已經安裝的模擬器啓動它時,應用程序崩潰。以下是源代碼。當我使用廣播接收器時,應用程序崩潰

AndroidManifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.android.newsreader" 
android:versionCode="1" 
android:versionName="1.0" > 
<uses-sdk 
    android:minSdkVersion="13" 
    android:targetSdkVersion="15" /> 
<supports-screens 
    android:largeScreens="true" 
    android:normalScreens="true" 
    android:smallScreens="true" 
    android:xlargeScreens="true" /> 
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 
<application 
    android:icon="@drawable/icon" 
    android:label="@string/app_name" 
    android:logo="@drawable/logo" 
    android:theme="@style/NewsReaderStyle" > 

    <receiver 
     android:name=".StartupIntentReceiver" 
     android:enabled="true" 
     android:permission="android.permission.RECEIVE_BOOT_COMPLETED" > 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
    </receiver> 
    <service android:name=".LoadFeedsService" ></service> 

    <activity 
     android:name=".NewsReaderActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity 
     android:name=".ArticleActivity" 
     android:theme="@style/NewsReaderStyle_NoActionBar" /> 
</application> 
<uses-permission android:name="android.permission.INTERNET" /> 
</manifest>* 

和我廣播reciever事先

*package com.example.android.newsreader; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.util.Log; 
public class StartupIntentReceiver extends BroadcastReceiver{ 
@Override 
public void onReceive(Context context, Intent intent) { 
    Log.i("reciever", "recieved intent"+ intent); 
    Intent serviceIntent = new Intent(context, LoadFeedsService.class); 

    context.startService(serviceIntent); 
} 
}* 

感謝。

+1

崩潰? Logcat輸出中的崩潰報告是什麼? – 2012-02-10 05:27:35

+0

其實..有更多的問題在那裏。我的後臺服務是從互聯網獲取數據,可能互聯網連接直到收到廣播時纔可用... – Ahmad 2012-02-10 07:34:29

回答

2

修改您的接收器代碼如下並嘗試。

<receiver 
     android:name=".StartupIntentReceiver" 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED" /> 
      <category android:name="android.intent.category.HOME" /> 
     </intent-filter> 
    </receiver> 

如果上述修改不起作用修改服務也如下,並嘗試。

<service 
    android:name=".LoadFeedsService"> 
    <intent-filter > 
     <action android:name="testapp.BACKGROUND_SERVICE" />     
    </intent-filter>    
</service> 

在接收方的java代碼中修改如下。

Intent serviceIntent = new Intent("testapp.BACKGROUND_SERVICE"); 

我希望它可以幫助你。

+0

非常好,乾淨。 5星給你.. – Ahmad 2012-02-10 07:23:18

+0

@Ahmad發生了什麼人? – 2012-02-10 09:23:52

+0

服務在啓動時啓動。但實際上,Iw從互聯網獲取數據,並且在接收到廣播時可能無法連接。我正在想辦法在我的服務從互聯網獲取數據之前獲得連接.. – Ahmad 2012-02-10 11:15:36

0

當您的應用程序已經安裝在模擬器中,並且您嘗試再次在您的emaulator中安裝該應用程序時,安裝過程中全新的安裝過度。此過程有時確實會導致一些問題,如應用程序崩潰,因爲新安裝不是乾淨的構建,而是覆蓋的構建。所以一定要記住,只要你在代碼中做了一些大的改動,最好先卸載已經存在的應用程序,然後再安裝一次。

+0

這不是問題。請參閱當我重新安裝應用程序時,它會運行,因爲BOOT事件在安裝之前已經播出並且旁路器被旁路。但是當我從模擬器啓動它時,它捕捉到廣播事件並崩潰。我猜想有意向和廣播接收者存在一些問題。我在廣播中編寫了日誌,即使Logcat上沒有顯示日誌,也就是說,廣告接收者的代碼沒有被執行 – Ahmad 2012-02-10 06:32:54

相關問題