活动地址:CSDN21天学习挑战赛
学习日记
1,学习博文
《Android 10.0 app获取不到序列号解决方案》:Android 10.0 app获取不到序列号解决方案_安卓兼职framework应用工程师的博客-CSDN博客_android10 获取序列号
2,学习知识点
1.权限问题
作者分享了其app开发遇到获取不到设备序列号的问题,追寻原因发现是没有相应权限问题。最后通过直接注释掉判断权限的逻辑,成功解决该问题。
3,学习的收获
在Android开发过程中,会遇到很多的权限问题。有的权限问题可能会有弹窗提示,有的权限问题可能只在log中就一条提示信息。所以追踪权限问题有时候需要比较大的耐心和细心去分析代码。像解决Android权限问题需要根据具体的需求采取合适的解决方案。例如作者解决获取序列号权限的方案是比较简单,使用比较多的方案,直接去掉权限判断。
4,实操
在开发的产品中,经常遇到客户需求去掉一些权限提示窗,默认给予权限。像这类的有提示框的权限问题相对比较好跟踪。只需跟踪提示框的信息,分析其创建过程去掉就行。这边举个简单的例子:app对某些usb设备操作会提示“要允许xxx访问xxx吗”等字样,跟踪代码发现该提示框在frameworks/base/packages/SystemUI/src/com/android/systemui/usb/UsbPermissionActivity.java中创建,所以修改逻辑,直接走点击“确定”实现的代码部分就行。
public void onCreate(Bundle icicle) { //对话框的创建过程 super.onCreate(icicle); Intent intent = getIntent(); mDevice = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE); mAccessory = (UsbAccessory)intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY); mPendingIntent = (PendingIntent)intent.getParcelableExtra(Intent.EXTRA_INTENT); mUid = intent.getIntExtra(Intent.EXTRA_UID, -1); mPackageName = intent.getStringExtra(UsbManager.EXTRA_PACKAGE); boolean canBeDefault = intent.getBooleanExtra(UsbManager.EXTRA_CAN_BE_DEFAULT, false); PackageManager packageManager = getPackageManager(); ApplicationInfo aInfo; try { aInfo = packageManager.getApplicationInfo(mPackageName, 0); } catch (PackageManager.NameNotFoundException e) { Log.e(TAG, "unable to look up package name", e); finish(); return; } String appName = aInfo.loadLabel(packageManager).toString(); final AlertController.AlertParams ap = mAlertParams; ap.mTitle = appName; boolean useRecordWarning = false; if (mDevice == null) { // Accessory Case ap.mMessage = getString(R.string.usb_accessory_permission_prompt, appName, mAccessory.getDescription()); mDisconnectedReceiver = new UsbDisconnectedReceiver(this, mAccessory); } else { boolean hasRecordPermission = PermissionChecker.checkPermissionForPreflight( this, android.Manifest.permission.RECORD_AUDIO, -1, aInfo.uid, mPackageName) == android.content.pm.PackageManager.PERMISSION_GRANTED; boolean isAudioCaptureDevice = mDevice.getHasAudioCapture(); useRecordWarning = isAudioCaptureDevice && !hasRecordPermission; int strID = useRecordWarning ? R.string.usb_device_permission_prompt_warn : R.string.usb_device_permission_prompt; ap.mMessage = getString(strID, appName, mDevice.getProductName()); mDisconnectedReceiver = new UsbDisconnectedReceiver(this, mDevice); } ap.mPositiveButtonText = getString(android.R.string.ok); ap.mNegativeButtonText = getString(android.R.string.cancel); ap.mPositiveButtonListener = this; ap.mNegativeButtonListener = this; // Don't show the "always use" checkbox if the USB/Record warning is in effect if (!useRecordWarning && canBeDefault && (mDevice != null || mAccessory != null)) { // add "open when" checkbox LayoutInflater inflater = (LayoutInflater) getSystemService( Context.LAYOUT_INFLATER_SERVICE); ap.mView = inflater.inflate(com.android.internal.R.layout.always_use_checkbox, null); mAlwaysUse = (CheckBox) ap.mView.findViewById(com.android.internal.R.id.alwaysUse); if (mDevice == null) { mAlwaysUse.setText(getString(R.string.always_use_accessory, appName, mAccessory.getDescription())); } else { mAlwaysUse.setText(getString(R.string.always_use_device, appName, mDevice.getProductName())); } mAlwaysUse.setOnCheckedChangeListener(this); mClearDefaultHint = (TextView)ap.mView.findViewById( com.android.internal.R.id.clearDefaultHint); mClearDefaultHint.setVisibility(View.GONE); } setupAlert(); //创建对话框显示 } public void onClick(DialogInterface dialog, int which) { //点击“确定“执行过程 if (which == AlertDialog.BUTTON_POSITIVE) { mPermissionGranted = true; } finish(); }
修改如下:
diff --git a/frameworks/base/packages/SystemUI/src/com/android/systemui/usb/UsbPermissionActivity.java b/frameworks/base/packages/SystemUI/src/com/android/systemui/usb/UsbPermissionActivity.java index 98b4209..535a00d 100644 --- a/frameworks/base/packages/SystemUI/src/com/android/systemui/usb/UsbPermissionActivity.java +++ b/frameworks/base/packages/SystemUI/src/com/android/systemui/usb/UsbPermissionActivity.java @@ -135,6 +135,8 @@ public class UsbPermissionActivity extends AlertActivity } - setupAlert(); + //setupAlert(); + mPermissionGranted = true; + finish(); } @Override