안드로이드 부팅 후 앱 자동실행_AndroidStudio

1. 자동실행

프로젝트 진행 도중 부팅 시 자동으로 앱을 실행시켜 주어야 하는 기능이 필요했습니다.

Linux 환경에서는 crontab에 등록하여 부팅 시 특정 응용프로그램이 자동 실행이 가능했습니다.

 

안드로이드에서는 AndroidManifest.xml 파일에 퍼미션 등록 이후

BroadcastReceiver를 이용하면 간단하게 apk파일 자체에

부팅 후 자동 실행이 되는 앱으로 등록이 가능합니다.

 

카카오톡과 같이 부팅 시 서비스로 실행이 자동으로 되어 백그라운드로 작동하는 것이 아닌

어플리케이션 자체의 자동 실행을 목표로 포스팅을 하였습니다.

 

 

 

2. AndroidManifest.xml 등록

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
        <receiver
            android:name=".AltaAutoRun"
            android:enabled="true"
            android:exported="false"
            android:label="STARTReceiver">

            <intent-filter >
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
        </receiver>

위의 두 코드를 AndroidManifeat.xml 파일에 넣어주면 됩니다.

receiver 속의 android:name=".AltaAutoRun" 경우

AltaAutiRun 대신에 BroadcastRecever를 상속받는 class 이름으로 변경해 주시면 됩니다.

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.sh.altpautoruntest">

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


        <receiver
            android:name=".AltaAutoRun"
            android:enabled="true"
            android:exported="false"
            android:label="STARTReceiver">

            <intent-filter >
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
        </receiver>

    </application>

</manifest>

위의 AndroidManifeast.xml을 참고하여 <uses-permission> 과 <receiver> 코드를 넣어 주시면 됩니다.

 

 

 

3. BroadcastReceover Class

퍼미션에 대한 설정을 완료하셨다면 BroadcastReceiver를 상속받는 Class를 새로 생성해 주어야 합니다.

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

public class AltaAutoRun extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
            Intent it = new Intent(context, MainActivity.class);
            it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(it);
        }
    }
}

 

제가 만든 Class의 이름은 AltaAutoRun 입니다.

Class 이름은 자유롭게 정해주셔도 되지만 AndroidManifeast.xml 속 receiver에 등록되는

android:name 의 class 이름과 동일해야 합니다.

 

안드로이드에서 앱을 자동실행시키기 위해서

MainActivity에 내용을 추가하거나 변경해야 하는 수고로움 없이

BroadcastReceiver를 상속받은 Class를 만들기만 하면 됩니다.

 

 

 

4. 기타

부팅 시 서비스 자동 실행의 경우 위의 AltaAutoRun를 밑의 코드로 변경하여 주시면 됩니다.

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

public class AltaAutoRun extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
            Intent it = new Intent(context, MainActivity.class);
            context.startService(it);
        }
    }
}

 

 

어플리케이션을 설치 이후 한번 이상 앱을 실행시켜 주셔야

부팅 시 정상적으로 앱이 실행됩니다.

 

 

문의 사항이나 오류&기타 질문은 댓글로 남겨 주시면 바로 반영하겠습니다.

 

 

X. 참고 사이트

https://cofs.tistory.com/172

https://kanzler.tistory.com/139

https://m.blog.naver.com/PostView.nhn?blogId=mandolle7&logNo=220011206857&proxyReferer=https%3A%2F%2Fwww.google.com%2F

https://link2me.tistory.com/1359

 

 

 

 

 

댓글

Designed by JB FACTORY