如何以编程方式启用和禁用otg? 我试过这个,但我的应用程序崩溃了
public class MainActivity extends Activity
{
Button button;
int level;
String status="";
UsbDevice device;
private UsbManager mUsbManager;
private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context c, Intent i)
{
level = i.getIntExtra("level", 0);
}
};
BroadcastReceiver mUsbDetachReceiver = new BroadcastReceiver()
{
public void onReceive(Context context, Intent intent)
{
status = intent.getStringExtra("status");
}
};
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
registerReceiver(mBatInfoReceiver, new IntentFilter(
Intent.ACTION_BATTERY_CHANGED));
button = (Button)findViewById(R.id.check);
}
public void getStatus(View v)
{
if(level>25 )
{
/*registerReceiver(mUsbDetachReceiver,new IntentFilter(
Intent.ACTION_UMS_DISCONNECTED));*/
ComponentName componentName = new ComponentName(MainActivity.this,"usb.host");
Toast.makeText(MainActivity.this, "Battery Status "+level+"%", Toast.LENGTH_LONG).show();
PackageManager pm = MainActivity.this.getPackageManager();
pm.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.PERMISSION_GRANTED);;
}
}
}
堆栈跟踪 06-14 16:59:14.966:E / AndroidRuntime(10154):致命异常:主06-14 16:59:14.966:E / AndroidRuntime(10154):进程: com.example.batteryindicator,PID:10154 06-14 16:59:14.966: E / AndroidRuntime(10154):java.lang.IllegalStateException:不能 执行活动的方法06-14 16:59:14.966: E / AndroidRuntime(10154):at android.view.View $ 1.onClick(View.java:4290)06-14 16:59:14.966: E / AndroidRuntime(10154):at android.view.View.performClick(View.java:5246)06-14 16:59:14.966: E / AndroidRuntime(10154):at android.widget.TextView.performClick(TextView.java:10571)06-14 16:59:14.966:E / AndroidRuntime(10154):at android.view.View $ PerformClick.run(View.java:21200)06-14 16:59:14.966:E / AndroidRuntime(10154):at android.os.Handler.handleCallback(Handler.java:739)06-14 16:59:14.966:E / AndroidRuntime(10154):at android.os.Handler.dispatchMessage(Handler.java:95)06-14 16:59:14.966:E / AndroidRuntime(10154):at android.os.Looper.loop(Looper.java:145)06-14 16:59:14.966: E / AndroidRuntime(10154):at android.app.ActivityThread.main(ActivityThread.java:6946)06-14 16:59:14.966:E / AndroidRuntime(10154):at java.lang.reflect.Method.invoke(Native Method)06-14 16:59:14.966: E / AndroidRuntime(10154):at java.lang.reflect.Method.invoke(Method.java:372)06-14 16:59:14.966: E / AndroidRuntime(10154):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1404) 06-14 16:59:14.966:E / AndroidRuntime(10154):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)06-14 16:59:14.966:E / AndroidRuntime(10154):引起: java.lang.reflect.InvocationTargetException 06-14 16:59:14.966: E / AndroidRuntime(10154):at java.lang.reflect.Method.invoke(Native 方法)06-14 16:59:14.966:E / AndroidRuntime(10154):at java.lang.reflect.Method.invoke(Method.java:372)06-14 16:59:14.966: E / AndroidRuntime(10154):at android.view.View $ 1.onClick(View.java:4285)06-14 16:59:14.966: E / AndroidRuntime(10154):... 11更多06-14 16:59:14.966: E / AndroidRuntime(10154):引起: java.lang.IllegalArgumentException:组件类usb.host没有 存在于com.example.batteryindicator 06-14 16:59:14.966: E / AndroidRuntime(10154):at android.os.Parcel.readException(Parcel.java:1550)06-14 16:59:14.966: E / AndroidRuntime(10154):at android.os.Parcel.readException(Parcel.java:1499)06-14 16:59:14.966: E / AndroidRuntime(10154):at android.content.pm.IPackageManager $存根$ Proxy.setComponentEnabledSetting(IPackageManager.java:4180) 06-14 16:59:14.966:E / AndroidRuntime(10154):at android.app.ApplicationPackageManager.setComponentEnabledSetting(ApplicationPackageManager.java:2044) 06-14 16:59:14.966:E / AndroidRuntime(10154):at com.example.batteryindicator.MainActivity.getStatus(MainActivity.java:75)