Some Android functions

1. 隐藏软键盘

    private void hideKeyboard() {
        final Activity activity = getActivity();
        if (activity != null) {
            View view = activity.getCurrentFocus();
            InputMethodManager imm = (InputMethodManager)
                    activity.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }

 2. 判断有没有实体导航键,HOME BACK RECENT

一般是去看property qemu.hw.mainkeys

可以用adb shell getprop或者在BSP里使用SystemProperties去查看

 

3. 获取Internal和外部SD卡容量大小:

StorageManager mStorageManager;
mStorageManager = context.getSystemService(StorageManager.class);

final List<VolumeInfo> volumes = mStorageManager.getVolumes();
        Collections.sort(volumes, VolumeInfo.getDescriptionComparator());

        for (VolumeInfo vol : volumes) {
            if (vol.getType() == VolumeInfo.TYPE_PRIVATE) {
                //Internal storage
                final long volumeTotalBytes = PrivateStorageInfo.getTotalSize(vol,
                        sTotalInternalStorage);
            } else if (vol.getType() == VolumeInfo.TYPE_PUBLIC) {
                // SD card
            }
        }

public static long getTotalSize(VolumeInfo info, long totalInternalStorage) {
        final Context context = AppGlobals.getInitialApplication();
        final StorageStatsManager stats = context.getSystemService(StorageStatsManager.class);
        try {
            return stats.getTotalBytes(info.getFsUuid());
        } catch (IOException e) {
            Log.w(TAG, e);
            return 0;
        }
    }

 4. frameworks修改 支持屏幕旋转180度

frameworks/base/core/res/res/values/config.xml

<!-- If true, the screen can be rotated via the accelerometer in all 4
         rotations as the default behavior. -->
    <bool name="config_allowAllRotations">true</bool>

参考 https://blog.csdn.net/Aaron121314/article/details/78235938

 

5. 启用/禁用锁屏旋转

修改 frameworks/base/core/res/res/values/config.xml

    <!-- Disable lockscreen rotation by default -->
    <bool name="config_enableLockScreenRotation">false</bool>

去使用这个config的位置在 frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarWindowManager.java

    private boolean shouldEnableKeyguardScreenRotation() {
        Resources res = mContext.getResources();
        return SystemProperties.getBoolean("lockscreen.rot_override", false)
                || res.getBoolean(R.bool.config_enableLockScreenRotation);
    }

 6. AlertDialog 点击确定/取消,dialog不消失

不管在点击事件中有没有dismiss(), cancel(),默认的AlertDialog,点击确定或取消后dialog都会消失。

需要把默认的点击事件设置为null,然后在show()之后,重新获取button来添加监听事件,此时不显示呼叫dismiss或者cancel,dialog不会消失。

AlertDialog alertDialog = new AlertDialog.Builder(this)
                 .setTitle("test")
                  .setMessage("message")
                  .setPositiveButton("确定", null)
                  .setNegativeButton("取消", null)
                  .show();

          alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  if (dontClose) {
                      Toast.makeText(getApplicationContext(), "此时dialog不关闭", Toast.LENGTH_LONG).show();
                      return;
                  } else {
                      alertDialog.cancel();
                  }
              }
          });

 7. 判断app是不是运行在前台(参照Settings中的方法)

private boolean isSettingsRunOnTop() {
        final ActivityManager activityManager =
            getApplicationContext().getSystemService(ActivityManager.class);
        final String taskPkgName = activityManager.getRunningTasks(1 /* maxNum */)
            .get(0 /* index */).baseActivity.getPackageName();
        return TextUtils.equals(getPackageName(), taskPkgName);
    }

 

posted @ 2019-01-16 10:18  Kunkka_An  阅读(379)  评论(0编辑  收藏  举报