【学习笔记】Android 学习(一)

Android USB 读取权限

转自:https://blog.csdn.net/shihunyewu/article/details/82685526

/**
     * 获得 usb 权限
     */
    private void openUsbDevice(){
        //before open usb device
        //should try to get usb permission
        tryGetUsbPermission();
    }
    UsbManager mUsbManager;
    private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";

    private void tryGetUsbPermission(){
        mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);

        IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
        registerReceiver(mUsbPermissionActionReceiver, filter);

        PendingIntent mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);

        //here do emulation to ask all connected usb device for permission
        for (final UsbDevice usbDevice : mUsbManager.getDeviceList().values()) {
            //add some conditional check if necessary
            //if(isWeCaredUsbDevice(usbDevice)){
            if(mUsbManager.hasPermission(usbDevice)){
                //if has already got permission, just goto connect it
                //that means: user has choose yes for your previously popup window asking for grant perssion for this usb device
                //and also choose option: not ask again
                afterGetUsbPermission(usbDevice);
            }else{
                //this line will let android popup window, ask user whether to allow this app to have permission to operate this usb device
                mUsbManager.requestPermission(usbDevice, mPermissionIntent);
            }
            //}
        }
    }


    private void afterGetUsbPermission(UsbDevice usbDevice){
        //call method to set up device communication
        //Toast.makeText(this, String.valueOf("Got permission for usb device: " + usbDevice), Toast.LENGTH_LONG).show();
        //Toast.makeText(this, String.valueOf("Found USB device: VID=" + usbDevice.getVendorId() + " PID=" + usbDevice.getProductId()), Toast.LENGTH_LONG).show();

        doYourOpenUsbDevice(usbDevice);
    }

    private void doYourOpenUsbDevice(UsbDevice usbDevice){
        //now follow line will NOT show: User has not given permission to device UsbDevice
        UsbDeviceConnection connection = mUsbManager.openDevice(usbDevice);
        //add your operation code here
    }

    private final BroadcastReceiver mUsbPermissionActionReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (ACTION_USB_PERMISSION.equals(action)) {
                synchronized (this) {
                    UsbDevice usbDevice = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                    if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                        //user choose YES for your previously popup window asking for grant perssion for this usb device
                        if(null != usbDevice){
                            afterGetUsbPermission(usbDevice);
                        }
                    }
                    else {
                        //user choose NO for your previously popup window asking for grant perssion for this usb device
                        Toast.makeText(context, String.valueOf("Permission denied for device" + usbDevice), Toast.LENGTH_LONG).show();
                    }
                }
            }
        }
    };

Android.mk 调用第三方静态库编译动态库SO

转自 https://blog.csdn.net/baidu_31872269/article/details/84554696
Android.mk文件内容如下,相比Android Studio创建C++工程来说比较简单可能就是不方便调试,编写CPP的jni代码还是Android studio开发比较方便

LOCAL_PATH := $(call my-dir)

#  load static libaray

include $(CLEAR_VARS) 
LOCAL_MODULE := live555
LOCAL_SRC_FILES := liblive555.a
include $(PREBUILT_STATIC_LIBRARY) 

#  load static libaray
include $(CLEAR_VARS)    
LOCAL_MODULE    := stdc++
LOCAL_SRC_FILES := libstdc++.a
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS) 
LOCAL_C_INCLUDES += \
     $(LOCAL_PATH)/live/jni \
     $(LOCAL_PATH)/live/jni/BasicUsageEnvironment/include \
     $(LOCAL_PATH)/live/jni/liveMedia/include \
     $(LOCAL_PATH)/live/jni/groupsock/include \
     $(LOCAL_PATH)/live/jni/UsageEnvironment/include \

#  load static libaray
LOCAL_STATIC_LIBRARIES := live555 stdc++

LOCAL_MODULE    := rtsplive555
LOCAL_SRC_FILES := \
	testRTSPClient.cpp
LOCAL_LDLIBS := -llog -lz

include $(BUILD_SHARED_LIBRARY)

编译命令:
C:\Users\Administrator\Desktop\project\jni>F:\Users\Administrator\AppData\Local\ndk\21.1.6352462\build\ndk-build

posted @ 2021-02-02 11:06  dozeoo  阅读(228)  评论(0编辑  收藏  举报