连接USB线后自动启动我的Android应用程序.

Kam*_*one 4 usb android

如果用户将USB线连接到设备,是否可以启动我的Android应用程序?我正在浏览这个链接.我在正确的道路上吗?

ρяσ*_*я K 9

在Manifest中注册ACTION_POWER_CONNECTED接收器:

<receiver android:name=".OnPowerReceiver">
        <intent-filter>
                <action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
        </intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)

在代码部分

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);   
    }   
}   
Run Code Online (Sandbox Code Playgroud)