2012-02-10 3 views
-1

"android.intent.action.BOOT_COMPLETED"를 사용하는 브로드 캐스트 수신기에서 서비스를 시작하려고했습니다. 이클립스에서 응용 프로그램을 클릭하여 작성하고 실행하면 잘 실행됩니다. 하지만 이미 설치된 에뮬레이터에서 실행하면 응용 프로그램이 충돌합니다. 다음은 소스 코드입니다.브로드 캐스트 수신기를 사용할 때 응용 프로그램이 충돌 함

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

방송 리시버 사전에

*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 출력의 충돌 보고서는 무엇입니까? –

+0

사실 더 많은 문제가있었습니다. 내 배경 서비스는 인터넷에서 데이터를 가져오고 아마도 인터넷 연결은 방송이 수신 될 때까지 사용할 수 없었습니다 ... – Ahmad

답변

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> 

수신기의 자바 코드에서 다음과 같이 수정하십시오.

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

도움이 될 수 있기를 바랍니다.

+0

아주 좋고 깨끗합니다. 5 별 .. – Ahmad

+0

@Ahmad 무슨 일이 일어 났습니까? –

+0

부팅시 서비스가 시작되었습니다. 하지만 실제로는 인터넷에서 데이터를 가져 오는 것으로, 아마도 방송이 수신 될 때 연결이 불가능할 것입니다. 내 서비스가 인터넷에서 데이터를 얻기 전에 연결을 얻는 방법을 찾았습니다. – Ahmad

0

앱이 이미 에뮬레이터에 설치되어 있고 에뮬레이터에 앱을 다시 설치하려고하면 새로 설치하면 설치가 끝납니다. 새 설치가 재정의 된 빌드 인 깨끗한 빌드가 아니기 때문에이 절차에서는 앱 충돌과 같은 일부 문제가 발생할 수 있습니다. 항상 기억해 두십시오. 코드에서 큰 변화가있을 때마다 기존 앱을 먼저 제거한 다음 다시 설치하는 것이 좋습니다.

+0

그건 문제가 아닙니다. 응용 프로그램을 다시 설치하면 BOOT 이벤트가 설치 전에 이미 브로드 캐스트되고 수신자가 바이 패스되어 있기 때문에 실행됩니다. 하지만 에뮬레이터에서 실행하면 브로드 캐스트 이벤트를 포착하여 충돌이 발생합니다. 내 생각에 의도 및 방송 수신자에 문제가 있습니다. Logcat에 표시되지 않은 브로드 캐스트의 로그를 작성했습니다. 즉, broadsact 수신자의 코드가 실행되고 있지 않음을 의미합니다. – Ahmad

관련 문제