注册应用程序启动时,USB产品连接到PC连接到、启动时、应用程序、产品

2023-09-07 10:43:42 作者:小女子゛不特别却是唯1╮

我创建了一个Android应用程序,我想如果运行的是Android USB项目被连接到PC上自动打开。

I created an Android application I would like to automatically open when USB item running Android gets connected to the PC.

是否可以登记自己的应用程序以处理USB连接?我该怎么办呢?

Is it possible to register my application to handle USB connection? How do I do that?

推荐答案

在你的Manifest.xml,补充一点:

In your Manifest.xml, add this:

<receiver android:name=".BattStatusShow">
    <intent-filter>
        <action android:name="android.intent.action.ums_connected" />
    </intent-filter>
</receiver>

然后让你的电池状态类扩展的BroadcastReceiver ,因为你的清单文件中注册它的USB消息。

Then have your battery status class extend BroadcastReceiver, since your registered it for USB messages in the Manifest file.

public class BattStatusShow extends BroadcastReceiver{ 
  if (intent.getAction().equalsIgnoreCase( "android.intent.action.UMS_CONNECTED")) {
    //do stuff here, like signal the view of your app it needs to do something
  }
}

您不给什么你的应用程序多的信息,但如果它只是一个电池电量显示的应用程序,编程PC主机应用程序来谈谈你的Andr​​oid应用程序似乎矫枉过正。如果你想要做更多,看看诺基亚PC站的应用程序与诺基亚的交互方式和仿效的行为。

You don't give much information about what your application, but if it is only a battery display application, programming a PC host application to talk to your Android app seems overkill. If you want to do more, look at how the Nokia PC station app interacts with Nokia and emulate the behavior.