Android学习笔记:抓取U盘实际路径

  1. 创建接收器
public class USBReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

StorageManager mStorageManager = (StorageManager) context.getSystemService(Activity.STORAGE_SERVICE);
String action = intent.getAction();
Log.d(TAG, "receive broadcast\n"+action);
if(action.equals(Intent.ACTION_MEDIA_MOUNTED)) {
   String path = intent.getData().getPath();
    //path = getCorrectPath(path);//获取正确的,完整的路径
    Log.d(TAG, "THE PATH IS:\n"+path);
}

}
}

2. 定义变量

USBReceiver usbBroadcast;

3. 在onCreate 下面注册广播

usbBroadcast = new USBReceiver();
IntentFilter usbFilter= new IntentFilter();
usbFilter.addAction(Intent.ACTION_MEDIA_MOUNTED);
usbFilter.addAction(Intent.ACTION_MEDIA_REMOVED);
usbFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
usbFilter.addDataScheme("file");
registerReceiver(usbBroadcast,usbFilter);

4. 程序退出前需要注销广播

参考链接:

https://blog.csdn.net/LJX_ahut/article/details/89672880

https://www.jianshu.com/p/42982aa8184

Android 学习笔记:接收广播开启activity时遇到Exception的问题

在别的安卓机器上调试成功的代码,在另外一台机器上测试的时候,发现收到广播启动activity的时候,会出现
android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
的异常提示。网上搜了一下,发现必须设置Intent标志为FLAG_ACTIVITY_NEW_TASK。我是这样设置的:

 

Intent intentS= new Intent(getApplicationContext(), RunActivity.class);
intentS.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intentS);

设置之后重新运行,就不会抛出上面的异常了。

本文参考链接:https://willy2016.pixnet.net/blog/post/215860320-android–api-%E7%B4%9A%E5%88%A5%E5%8D%87%E7%B4%9A-28%2C-android-pie-9.0-%E9%81%87%E5%88%B0-error

tinker board 学习笔记

今天用build 好的代码在tinker board上运行,发现GPIO的i2c上完全没反应,检查硬件接线也没问题,跟同事请教,才发现是权限的问题。因为我们的i2c用的是 /dev/i2c-1这个节点,所以需要配置一下对应的权限:

chmod 777 /dev/i2c-1

设置之后,再运行程序,i2c通讯就正常了。