티스토리 뷰

반응형

Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.

Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.

 

아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.

Trying to start a service on boot on Android

부팅 시 서비스를 시작하기

 문제 내용 

I've been trying to start a service when a device boots up on android, but I cannot get it to work. I've looked at a number of links online but none of the code works. Am I forgetting something?

나는 안드로이드에서 기기가 부팅될 때 서비스를 시작하려고 했지만 작동하지 않는다. 나는 온라인에서 여러 링크를 살펴보았지만 어떤 코드도 작동하지 않습니다. 내가 뭘 잊은 거야?

 

AndroidManifest.xml

Android Manifest.xml
<receiver
    android:name=".StartServiceAtBootReceiver"
    android:enabled="true"
    android:exported="false"
    android:label="StartServiceAtBootReceiver" >
    <intent-filter>
        <action android:name="android.intent.action._BOOT_COMPLETED" />
    </intent-filter>
</receiver>

<service
    android:name="com.test.RunService"
    android:enabled="true" />

 

BroadcastReceiver

브로드캐스트리시버
public void onReceive(Context context, Intent intent) {
    if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
        Intent serviceLauncher = new Intent(context, RunService.class);
        context.startService(serviceLauncher);
        Log.v("TEST", "Service loaded at start");
    }
}

 

 

 높은 점수를 받은 Solution 

The other answers look good, but I thought I'd wrap everything up into one complete answer.

다른 답들은 좋아 보이지만, 나는 모든 것을 하나의 완전한 답으로 마무리할 것이라고 생각했다.

 

You need the following in your AndroidManifest.xml file:

AndroidManifest.xml 파일에는 다음이 필요합니다.

 

  1. In your <manifest> element:
  2. <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
  3. In your <application> element (be sure to use a fully-qualified [or relative] class name for your BroadcastReceiver):(you don't need the android:enabled, exported, etc., attributes: the Android defaults are correct)
    package com.example;  public class MyBroadcastReceiver extends BroadcastReceiver {     @Override     public void onReceive(Context context, Intent intent) {         Intent startServiceIntent = new Intent(context, MyService.class);         context.startService(startServiceIntent);     } } 
  4. In MyBroadcastReceiver.java:
  5. <receiver android:name="com.example.MyBroadcastReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver>

 

From the original question:

원래 질문에서:

 

  • it's not clear if the <receiver> element was in the <application> element
  • it's not clear if the correct fully-qualified (or relative) class name for the BroadcastReceiver was specified
  • there was a typo in the <intent-filter>
<어플리케이션> 요소에 <리시버> 요소가 있었는지는 명확하지 않습니다.
BroadcastReceiver에 대해 올바른 정규화된(또는 상대적) 클래스 이름이 지정되었는지 여부는 명확하지 않습니다.
<intent-filter>에 오타가 있었습니다.

 

 

 

 가장 최근 달린 Solution 

This is what I did

이것이 내가 한 일이다.

 

1. I made the Receiver class

1. 나는 리시버 클래스를 만들었다.
public class BootReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        //whatever you want to do on boot
       Intent serviceIntent = new Intent(context, YourService.class);
       context.startService(serviceIntent);
    }
}

 

2.in the manifest

2. 매니페스트에..
<manifest...>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <application...>
        <receiver android:name=".BootReceiver" android:enabled="true" android:exported="false">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
    ...

 

3.and after ALL you NEED to "set" the receiver in your MainActivity, it may be inside the onCreate

3.메인 액티비티에서 리시버를 "설정"하기 위해 필요한 경우, 리시버는 onCreate 내부에 있을 수 있습니다.
...
 final ComponentName onBootReceiver = new ComponentName(getApplication().getPackageName(), BootReceiver.class.getName());
        if(getPackageManager().getComponentEnabledSetting(onBootReceiver) != PackageManager.COMPONENT_ENABLED_STATE_ENABLED)
        getPackageManager().setComponentEnabledSetting(onBootReceiver,PackageManager.COMPONENT_ENABLED_STATE_ENABLED,PackageManager.DONT_KILL_APP);
...

 

the final steap I have learned from ApiDemos

ApiDemos에서 배운 마지막 단계

 

 

 

출처 : https://stackoverflow.com/questions/2784441/trying-to-start-a-service-on-boot-on-android

반응형
댓글
공지사항
최근에 올라온 글