How to Auto-start an Android Application?

后端 未结 4 678
花落未央
花落未央 2020-11-27 11:02

I\'m not sure how to autostart an android application after the android emulator completes its booting. Does anyone have any code snippets that will help me?

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-27 11:28

    You have to add a manifest permission entry:

    
    

    (of course you should list all other permissions that your app uses).

    Then, implement BroadcastReceiver class, it should be simple and fast executable. The best approach is to set an alarm in this receiver to wake up your service (if it's not necessary to keep it running ale the time as Prahast wrote).

    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);
        }
    }
    

    Then, add a Receiver class to your manifest file:

    
        
            
            
        
    
    

提交回复
热议问题