-1

I am developing a dash cam android application. What I am wondering is that is there any way to auto launch the application when the power is connected to the android device. I am thinking to make the app fully automated like when the car engine starts and the power connects to the device with the usb cable, the application automatically launch and start to operate. I read some posts about auto start when the device boots. but, is there any way to auto start when the power connects to the device? thank you.

1 Answers1

0

first you have to make service app. and using BroadcastReceiver for get state of power.

  private final BroadcastReceiver mConnectionReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (Intent.ACTION_POWER_CONNECTED.equals(intent.getAction())) {
                Log.e("mConnectionReceiver", "AC Connected by onReceive");
                // turn on your app via using Intent

            } else if (Intent.ACTION_POWER_DISCONNECTED.equals(intent.getAction())) {
                Log.e("mConnectionReceiver", "AC Disconnected by onReceive");
            }
        }
    };

for make service app

public class test extends AppCompatActivity{ 
    
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate( savedInstanceState ); 
        setContentView( R.layout.activity_main ); // run Background Service 
        Intent serviceintent = new Intent( test.this, MyService.class ); 
        startService( serviceintent );
    }
}

and serviceThread

public class ServiceThread extends Thread {
    Handler handler; 
    boolean isRun = true; 

    public ServiceThread(Handler handler) { 
        this.handler = handler; 
    } 
    public void stopForever() { 
        synchronized (this) { 
            this.isRun = false; 
        } 
    } 
    public void run() { 
        while (isRun) { 
            handler.sendEmptyMessage( 0 );
            try { 
                Thread.sleep( 1000 );
            } catch (Exception e) {
            } 
        } 
    } 
}

and make Service

public class MyService extends Service { 
    ServiceThread thread;

    private final BroadcastReceiver mConnectionReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (Intent.ACTION_POWER_CONNECTED.equals(intent.getAction())) {
                Log.e("mConnectionReceiver", "AC Connected by onReceive");
                // turn on your app via using Intent

            } else if (Intent.ACTION_POWER_DISCONNECTED.equals(intent.getAction())) {
                Log.e("mConnectionReceiver", "AC Disconnected by onReceive");
            }
        }
    };


    @Override public IBinder onBind(Intent intent) { 
        return null;
    } 
    
    @Override public int onStartCommand(Intent intent, int flags, int startId) { 
        myServiceHandler handler = new myServiceHandler(); 
        thread = new ServiceThread( handler ); 
        thread.stopForever(); return START_STICKY; 
}  
    public void onDestroy() {
        myServiceHandler handler = new myServiceHandler(); 
        thread = new ServiceThread( handler ); 
        thread.start(); 
    } 
    public void start() { 
        myServiceHandler handler = new myServiceHandler(); 
        thread = new ServiceThread( handler ); 
        thread.start(); 
    } 
    public void stop() { 
        myServiceHandler handler = new myServiceHandler(); 
        thread = new ServiceThread( handler ); 
        thread.stopForever(); 
    } 
    public class myServiceHandler extends Handler { 
        @Override public void handleMessage(android.os.Message msg) {
            IntentFilter filter = new IntentFilter();
            filter.addAction(Intent.ACTION_POWER_CONNECTED);
            filter.addAction(Intent.ACTION_POWER_DISCONNECTED);
            registerReceiver(mConnectionReceiver, filter);
    } 
}

this is just example. so check this and fix what you want.

S T
  • 1,068
  • 2
  • 8
  • 16