2017-12-20 1 views
0

배터리 관련 앱을 만들고 있습니다. 나는 방송 수신기가 매니페스트에 선언 만들었습니다브로드 캐스트 수신기에서 포 그라운드 서비스를 시작하는 방법은 무엇입니까?

매니페스트 :

<receiver android:name=".PowerConnectionReceiver"> 
     <intent-filter> 
      <action android:name="android.intent.action.ACTION_POWER_CONNECTED" /> 
      <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" /> 
     </intent-filter> 
</receiver> 

수신기 :

public class PowerConnectionReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     if (intent.getAction().equals(Intent.ACTION_POWER_CONNECTED)) { 
      Toast.makeText(context, "The device is charging", Toast.LENGTH_SHORT).show(); 
     } else { 
      intent.getAction().equals(Intent.ACTION_POWER_DISCONNECTED); 
      Toast.makeText(context, "The device is not charging", Toast.LENGTH_SHORT).show(); 
     } 
    } 
} 
내가 전경 통지 (같은 this 일) 를 추가 할

하지만 방법은 내가해야 방송 수신기에서 추가 하시겠습니까? (즉, 어떻게 방송 수신기에서 서비스를 시작할 수 있습니까?) onReceive에서

답변

0

() 서비스를 시작합니다

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { 
    startForeground(1, new Notification()); 
} 

: 그런 일을) (

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { 
    context.startForegroundService(intent); 
} else { 
    context.startService(intent); 
} 

그리고 서비스에서 onCreate 일부 제조업체에서는 Android N에 백 포트가있어 이러한 새로운 배경 제한 때문에이 API도 지원할 수 있습니다.

관련 문제