0

我在接收器中使用意图过滤器

 <receiver android:name=".extra_java_classes.MyReceiver">
        <intent-filter>
            <action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
            <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
        </intent-filter>
    </receiver>

广播接收器类

public class MyReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    Intent intent1 = new Intent("usb_detect");
    context.sendBroadcast(intent1);

}

}

在我的 MainActivity onCreateMethod

  usdReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context arg0, Intent intent) {

            String action = intent.getAction();

            if (action.equals("usb_detect")) {

                Toast.makeText(getApplicationContext(),"Please remove your usb",Toast.LENGTH_SHORT).show();

            }

        }
    };

在 MainActivity 中注册接收器 -

@Override
protected void onResume() {
    super.onResume();

    registerReceiver(usdReceiver, new IntentFilter("usb_detect"));

}

此代码广播是用户将 USB 连接到手机。但我希望广播接收器仅检测用户是否未禁用传输文件/传输照片。我不想检测仅用于充电选项的 USB 连接。我该怎么做?

4

0 回答 0