android 自己总结的小工具类

本文汇总了多个Android开发中常用的工具方法,包括dp与px单位转换、网络状态判断、屏幕亮度调节、检查设备是否root、杀死当前进程及防止按钮快速重复点击等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 dp 与 px 的相互转换

  public class DensityUtil {  
  
     
    public static int dip2px(Context context, float dpValue) {  
        final float scale = context.getResources().getDisplayMetrics().density;  
        return (int) (dpValue * scale + 0.5f);  
    }  
  
     
    public static int px2dip(Context context, float pxValue) {  
        final float scale = context.getResources().getDisplayMetrics().density;  
        return (int) (pxValue / scale + 0.5f);  
    }  
}  


2 -------- 判断网络状态-----------------------------

    android:name="android.permission.ACCESS_NETWORK_STATE" />  
  
 private boolean getNetWorkStatus() {  
  
   boolean netSataus = false;  
   ConnectivityManager cwjManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);  
  
   cwjManager.getActiveNetworkInfo();  
  
   if (cwjManager.getActiveNetworkInfo() != null) {  
   netSataus = cwjManager.getActiveNetworkInfo().isAvailable();  
   }  
  
   if (!netSataus) {  
   Builder b = new AlertDialog.Builder(this).setTitle("没有可用的网络")  
   .setMessage("是否对网络进行设置?");  
   b.setPositiveButton("是", new DialogInterface.OnClickListener() {  
   public void onClick(DialogInterface dialog, int whichButton) {  
   Intent mIntent = new Intent("/");  
   ComponentName comp = new ComponentName(  
   "com.android.settings",  
   "com.android.settings.WirelessSettings");  
   mIntent.setComponent(comp);  
   mIntent.setAction("android.intent.action.VIEW");  
   startActivityForResult(mIntent,0);   
   }  
   }).setNeutralButton("否", new DialogInterface.OnClickListener() {  
   public void onClick(DialogInterface dialog, int whichButton) {  
   dialog.cancel();  
   }  
   }).show();  
   }  
  
   return netSataus;  
   }  



3 ----------调节屏幕亮度-------

public void setBrightness(int level) { 
ContentResolver cr = getContentResolver(); 
Settings.System.putInt(cr, "screen_brightness", level); 
Window window = getWindow(); 
LayoutParams attributes = window.getAttributes(); 
float flevel = level; 
attributes.screenBrightness = flevel / 255; 
getWindow().setAttributes(attributes); 



4--------检查手机是否root ---------------------

 public static boolean checkRootingState(Context context)
    {
        
        final String ROOT_PATH = Environment.getExternalStorageDirectory() + "";
        final String ROOTING_PATH_1 = "/system/bin/su";
        final String ROOTING_PATH_2 = "/system/xbin/su";
        final String ROOTING_PATH_3 = "/system/app/SuperUser.apk";
        final String ROOTING_PATH_4 = context.getFilesDir().getPath() + "data/com.noshufou.android.su";
        
        // System.out.println("1111111111111111111   " + ROOTING_PATH_4);
        
        String[] rootFilesPath = new String[] { ROOT_PATH + ROOTING_PATH_1, ROOT_PATH + ROOTING_PATH_2,
                ROOT_PATH + ROOTING_PATH_3, ROOT_PATH + ROOTING_PATH_4 };
        
        boolean isRootingFlag = false;
        
        try
        {
            Runtime.getRuntime().exec("su");
            isRootingFlag = true;
        } catch (Exception e)
        {
            isRootingFlag = false;
        }
        
        if (!isRootingFlag)
        {
            isRootingFlag = checkRootingFiles(createFiles(rootFilesPath));
        }
        
        return isRootingFlag;
        
    }


5 --------------杀死当前进程--------------------------
  public static void killProc()
    {
        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(0);
    }


6 ----------防止快速点击按钮----------------
   public static boolean isFastDoubleClick()
    {
        long time = System.currentTimeMillis();
        long timeX = time - lastClickTime;
        if (0 < timeX && timeX < 500)
        {
            return true;
        }
        lastClickTime = time;
        return false;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值