2009-06-29 8 views
35

안드로이드 에뮬레이터가 부팅을 완료 한 후 안드로이드 응용 프로그램을 자동 시작하는 방법을 모르겠습니다. 누구든지 나를 도울 코드 스 니펫이 있습니까?Android 애플리케이션을 자동 시작하는 방법?

+0

- Prashast의 답변을 참조하십시오. –

+0

@Rajapandian Owner는 해결책 인 경우 답변을 수락하거나 의견에 귀하의 기대치를 언급해야합니다. 다른 사람들에게 도움이 될 것입니다. – naveejr

+0

http : // karanbalkar.co.kr/2014/01/autostart-application-at-boot-in-android/ –

답변

12

자동 부팅으로 전화 부팅시 자동 시작을 의미하는 경우 BOOT_COMPLETED 의도에 대해 BroadcastReceiver를 등록해야합니다. 안드로이드 시스템은 부트가 완료되면 그 의도를 브로드 캐스트합니다.

귀하가 원하는대로 할 수있는 서비스를 시작할 수 있습니다.

항상 유휴 상태에서도 시스템 리소스를 소모하므로 일반적으로 전화로 서비스가 실행되는 것은 좋지 않습니다. 필요한 경우에만 서비스/응용 프로그램을 시작한 다음 필요하지 않은 경우 중지하십시오.

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

(물론 앱에서 사용하는 다른 모든 권한을 나열해야합니다) :

36

당신은 매니페스트 권한 항목을 추가해야합니다.

그런 다음 BroadcastReceiver 클래스를 구현하면 간단하고 빠른 실행 파일이어야합니다. 가장 좋은 방법은이 수신기에 알람을 설정하여 서비스를 깨우는 것입니다 (Prahast가 작성한대로 계속 실행해야 할 필요가없는 경우).

public class BootUpReceiver extends BroadcastReceiver { 
@Override 
public void onReceive(Context context, Intent intent) { 
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
    PendingIntent pi = PendingIntent.getService(context, 0, new Intent(context, MyService.class), PendingIntent.FLAG_UPDATE_CURRENT); 
    am.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + interval, interval, pi); 
}} 

그런 다음 매니페스트 파일에 수신기 클래스를 추가 :

<receiver android:enabled="true" android:name=".receivers.BootUpReceiver" 
     android:permission="android.permission.RECEIVE_BOOT_COMPLETED"> 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
    </receiver> 
+0

간격은 어떻게됩니까? – asmgx

+0

이 줄의 ".receivers"는 "receiver android : enabled ="true "android : name =". receivers.BootUpReceiver " 은"Unresolvable package receivers "오류를 나타냅니다. – asmgx

16

을 편집 AndroidManifest.xml 추가 RECEIVE_BOOT_COMPLETED 허가

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

편집에 대한 AndroidManifest.xml 응용 프로그램 부분 권한

아래
<receiver android:enabled="true" android:name=".BootUpReceiver" 
android:permission="android.permission.RECEIVE_BOOT_COMPLETED"> 
<intent-filter> 
    <action android:name="android.intent.action.BOOT_COMPLETED" /> 
    <category android:name="android.intent.category.DEFAULT" /> 
</intent-filter> 
</receiver> 

아래 활동으로 작성하십시오.

public class BootUpReceiver extends BroadcastReceiver{ 
@Override 
public void onReceive(Context context, Intent intent) { 
    Intent i = new Intent(context, MyActivity.class); 
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    context.startActivity(i); 
} 
} 
+0

잘 동작합니다. 감사! –

0

항상이 주제로 들어갑니다. 나는 내 코드를 여기에 넣어서 다음 번에 i (또는 다른)가 사용할 수있게 할 것이다. (Phew는 내 저장소 코드를 검색하는 것을 싫어합니다.)

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

수신기 및 서비스를 추가합니다 :

<receiver android:enabled="true" android:name=".BootUpReceiver" 
     android:permission="android.permission.RECEIVE_BOOT_COMPLETED"> 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
    </receiver> 
    <service android:name="Launcher" /> 

만들기 클래스 실행기 :

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

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 

     new AsyncTask<Service, Void, Service>() { 

      @Override 
      protected Service doInBackground(Service... params) { 
       Service service = params[0]; 
       PackageManager pm = service.getPackageManager(); 
       try { 
        Intent target = pm.getLaunchIntentForPackage("your.package.id"); 
        if (target != null) { 
         service.startActivity(target); 
         synchronized (this) { 
          wait(3000); 
         } 
        } else { 
         throw new ActivityNotFoundException(); 
        } 
       } catch (ActivityNotFoundException | InterruptedException ignored) { 
       } 
       return service; 
      } 

      @Override 
      protected void onPostExecute(Service service) { 
       service.stopSelf(); 
      } 

     }.execute(this); 

     return START_STICKY; 
    } 
} 

안드로이드 재부팅 후 작업을 수행하는 클래스 BootUpReceiver 만들기

는 권한을 추가합니다. 예를 발사 MainActivity를 들어

는 :

당신이 잘못 @AdamC
public class BootUpReceiver extends BroadcastReceiver{ 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     Intent target = new Intent(context, MainActivity.class); 
     target.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     context.startActivity(target); 
    } 
} 
+0

<수신기에서 android : enabled = "true"android : name = ". BootUpReceiver" Unresolvable class BootUpReceiver – asmgx

+0

@asmgx : 답변을 업데이트했습니다. 거기에 넣는 것을 잊어 버렸습니다. – nafsaka

관련 문제