zoukankan      html  css  js  c++  java
  • 【学习笔记】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:UsersAdministratorDesktopprojectjni>F:UsersAdministratorAppDataLocal dk21.1.6352462uild dk-build

    作者:Dozeoo
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    git使用小结
    关于vtordisp知多少?
    虚函数与虚继承寻踪
    最简git Server配置
    StarUML序
    CacheHelper对缓存的控制
    Web Service的一些经验和技巧总结
    月份信息二维坐标图绘制(绘制箭头算法)续
    dynamic与xml的相互转换
    如何将XML与OBJECT进行相互转换(泛型以及通用方法)
  • 原文地址:https://www.cnblogs.com/nightnine/p/14360897.html
Copyright © 2011-2022 走看看