在广播接收器上我可以打开一个活动

时间:2011-09-06 07:41:59

标签: android usb

公共类USB扩展了BroadcastReceiver {

 private static final String TAG = "DetactUSB";


@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub

     if (intent.getAction().equalsIgnoreCase( "android.intent.action.UMS_CONNECTED"))
        {
                TextView textView = new TextView(context);

                textView.setPadding(10,10,10,10);
                textView.setText("USB connected……….");
                Toast toastView = new Toast(context);
                toastView.setDuration(Toast.LENGTH_LONG);
                toastView.setGravity(Gravity.CENTER, 0,0);
                toastView.setView(textView);
                toastView.show();
                Log.i(TAG,"USB connected..");

        }
        if (intent.getAction().equalsIgnoreCase( "android.intent.action.UMS_DISCONNECTED"))
        {
                TextView textView = new TextView(context);
                textView.setPadding(10,10,10,10);
                textView.setText("USB Disconnected……….");
                Toast toastView = new Toast(context);
                toastView.setDuration(Toast.LENGTH_LONG);
                toastView.setGravity(Gravity.CENTER, 0,0);
                toastView.setView(textView);
                toastView.show();
        }
    } 

连接USB时是否可以打开活动。

1 个答案:

答案 0 :(得分:0)

您可以从接收器开始这样的活动:

Intent intent = new Intent(context, activitytobelaunch.class);
intent .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent );