2017-04-30 7 views
0

BOOT_COMPLETED 이벤트를 수신하지에 잘 살고있다 (started) true 그의 onCreate() 메서드에 의해하지만 내가 MainActivity에서 콘솔에 var을 인쇄 할 때 부울은 여전히 ​​false입니다.안드로이드 브로드 캐스트 리시버는 내 용 애플리케이션 BOOT_COMPLETED 이벤트 (<code>BootReceiver</code>)를 수신 한 후 서비스 (<code>NtService</code>)이 서비스를 시작 공공 정적 부울을 가지고 <strong>브로드 캐스트 리시버</strong>에서 만든

응용 프로그램은 내부 저장에 설치되어, 나는 ADB 쉘에서이 명령을 제출하여 안드로이드 스튜디오 에뮬레이터에서 디버깅을 해요 :

: 여기

am broadcast -a android.intent.action.BOOT_COMPLETED 

코드입니다

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 

public class BootReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
      Intent i = new Intent(context, NtService.class); 
      context.startActivity(i); 
    } 
} 

수 (Nt) BootReceiver 서비스 AndroidManifest를

import android.app.Service; 
    import android.content.Intent; 
    import android.os.IBinder; 

    public class NtService extends Service { 
    public static boolean started; 

    public NtService() { 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 

    public void onCreate(){ 
     started=true; 
    } 
    } 

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.primerdime.cloudchat"> 
    <!-- PERMISSION --> 
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 
    <application 
     android:allowBackup="false" 
     android:icon="@drawable/icon" 
     android:installLocation="internalOnly" 
     android:label="@string/app_name" 
     android:roundIcon="@mipmap/ic_launcher_round" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
      <activity 
       android:name=".MainActivity" 
       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=".registration" /> 
      <!-- SERVICE AND RECEIVER --> 
      <service 
       android:name=".NtService" 
       android:enabled="true" 
       android:exported="false" /> 

      <receiver 
       android:name=".BootReceiver" 
       android:enabled="true" 
       android:exported="false"> 
       <intent-filter> 
        <action android:name="android.intent.action._BOOT_COMPLETED" /> 
       </intent-filter> 
      </receiver> 

    </application> 
</manifest> 

답변

1

<receiver> 요소에서 android:exported="false"를 제거합니다. 그대로, 앱 외부에서 브로드 캐스트를 수신 할 수 없습니다.

또한 <action> 요소에 오타가 있습니다. <action android:name="android.intent.action.BOOT_COMPLETED" />이어야합니다.

마지막으로 서비스가 아닌 활동을 시작하려고 할 때 context.startActivity(i);context.startService(i);으로 바꿉니다.

+0

'android : exported = "false"'를 제거했지만 ''에 오타가 수정되었지만 여전히 작동하지 않습니다. – Helio

+0

@Helio : 업데이트 된 답변보기. – CommonsWare

+0

완벽한 덕분에 감사합니다. – Helio