Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starting my android application automatically after connecting the USB cable.

Tags:

android

usb

Is it possible me to start my android application if the user connecting USB cable to the device ?. I am going through the this link . Am I in the correct path ?

like image 339
Kamalone Avatar asked Dec 13 '22 00:12

Kamalone


1 Answers

Register an Receiver for ACTION_POWER_CONNECTED in Manifest as:

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

and in Code part

public class OnPowerReceiver extends BroadcastReceiver { 

@Override
    public void onReceive(Context context, Intent intent) {
        Intent i = new Intent(context, Your_Activity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);   
    }   
}   
like image 152
ρяσѕρєя K Avatar answered Apr 06 '23 04:04

ρяσѕρєя K