2016-06-26 6 views
0

서비스를 실행하는 데 문제가 있습니다. 로그에는 서비스가 실행 중인지 아무 것도 없습니다. 그래서 나는 그녀가 일한다는 것을 모른다. 토스트 또한 MainActivity에 표시되지 않습니다. 나는 많은 게시물을 읽고 아무도 작동하지 않습니다. 그것을 고치는 방법?부팅 후 서비스가 시작되지 않음

서비스

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

public class AutoStartUp extends Service { 
    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show(); 
     // do something when the service is created 
    } 

} 

브로드 캐스트 리시버

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

public class BootComplete extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 

     if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) { 
      Intent serviceIntent = new Intent(context, AutoStartUp.class); 
      context.startService(serviceIntent); 
     } 

    } 
} 

Manifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="kamiszczu.ovh.servicetest3"> 
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" > 
    </uses-permission> 
    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <receiver 
      android:name="kamiszczu.ovh.servicetest3.BootComplete" 
      android:enabled="true" 
      android:exported="false" > 
      <intent-filter> 
       <action android:name="android.intent.action.BOOT_COMPLETED" /> 
      </intent-filter> 
     </receiver> 
     <service 
      android:name="kamiszczu.ovh.servicetest3.AutoStartUp" 
      android:enabled="true"> 
     </service> 
     <activity 
      android:name=".MainActivity" 
      android:label="@string/app_name" 
      android:theme="@style/AppTheme.NoActionBar"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 
</manifest> 

답변

1

당신은 필요 없어 수신자가 하나의 의도 유형 (Intent.ACTION_BOOT_COMPLETED)으로 등록되어 있으므로 의도 유형을 확인하십시오. 따라서 의도 동작이 Intent.ACTION_BOOT_COMPLETED 인 경우 수신자를 체크인 할 필요가 없습니다. 나는 수신기의 상태가 사실이 아니므로 서비스를 시작하는 코드가 실행되지 않는다고 생각한다.

+0

더 명확하게 또는 예를 쓸 수 있습니까? – kamiszczu

+0

내 대답을 편집했습니다 – user3215142

+0

맞습니다. ACTION_POWER_CONNECTED (으)로 변경하고 Toast를 (를) 보여줍니다. – kamiszczu

관련 문제