Android开发常用

Android开发中常用的各种代码

  • 进入界面是不让EditText自动获取焦点

在EditText的父布局中加上如下代码

android:focusable="true"
android:focusableInTouchMode="true"
  • Android Studio Logcat打印数据时,数据太长的情况会打印不全

通过截断数据的方式,分段打印

public static void tag(String tag, String msg) {
    if (tag == null || tag.length() == 0
            || msg == null || msg.length() == 0)
        return;
    int segmentSize = 3 * 1024;
    long length = msg.length();
    if (length <= segmentSize ) {// 长度小于等于限制直接打印
        Log.d(tag, msg);
    }else {
        while (msg.length() > segmentSize ) {// 循环分段打印日志
            String logContent = msg.substring(0, segmentSize );
            msg = msg.replace(logContent, "");
            Log.d(tag, logContent);
        }
        Log.d(tag, msg);// 打印剩余日志
    }
}
  • 拨打电话
public static void call(Context context, String phoneNumber) {
    context.startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNumber)));
}
  • 跳转到拨号界面
public static void callDial(Context context, String phoneNumber) {
    context.startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNumber)));
}
  • 判断当前App处于前台还是后台状态
public static boolean isApplicationBackground(final Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    @SuppressWarnings("deprecation")
    List tasks = am.getRunningTasks(1);
    if (!tasks.isEmpty()) {
        ComponentName topActivity = tasks.get(0).topActivity;
        if (!topActivity.getPackageName().equals(context.getPackageName())) {
            return true;
        }
    }
    return false;
}

需要添加权限

  • 判断当前手机是否处于锁屏(睡眠)状态
public static boolean isSleeping(Context context) {
    KeyguardManager kgMgr = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
    boolean isSleeping = kgMgr.inKeyguardRestrictedInputMode();
    return isSleeping;
}
  • 判断当前是否有网络连接
public static boolean isOnline(Context context) {
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Activity.CONNECTIVITY_SERVICE);
    NetworkInfo info = manager.getActiveNetworkInfo();
    if (info != null && info.isConnected()) {
        return true;
    }
    return false;
}
  • 安装APK
public static void installApk(Context context, File file) {
    Intent intent = new Intent();
    intent.setAction("android.intent.action.VIEW");
    intent.addCategory("android.intent.category.DEFAULT");
    intent.setType("application/vnd.android.package-archive");
    intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(intent);
}
  • 获取当前设备宽高
@SuppressWarnings("deprecation")
public static int getDeviceWidth(Context context) {
    WindowManager manager = (WindowManager) context
            .getSystemService(Context.WINDOW_SERVICE);
    return manager.getDefaultDisplay().getWidth();
}
 
@SuppressWarnings("deprecation")
public static int getDeviceHeight(Context context) {
    WindowManager manager = (WindowManager) context
            .getSystemService(Context.WINDOW_SERVICE);
    return manager.getDefaultDisplay().getHeight();
}
  • 判断当前设备是否为手机
public static boolean isPhone(Context context) {
    TelephonyManager telephony = (TelephonyManager) context
            .getSystemService(Context.TELEPHONY_SERVICE);
    if (telephony.getPhoneType() == TelephonyManager.PHONE_TYPE_NONE) {
        return false;
    } else {
        return true;
    }
}
  • 获取当前设备的IMEI,需要与上面的isPhone()一起使用
@TargetApi(Build.VERSION_CODES.CUPCAKE)
public static String getDeviceIMEI(Context context) {
    String deviceId;
    if (isPhone(context)) {
        TelephonyManager telephony = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        deviceId = telephony.getDeviceId();
    } else {
        deviceId = Settings.Secure.getString(context.getContentResolver(),
                Settings.Secure.ANDROID_ID);
 
    }
    return deviceId;
}
  • 获取当前设备的MAC地址
public static String getMacAddress(Context context) {
    String macAddress;
    WifiManager wifi = (WifiManager) context
            .getSystemService(Context.WIFI_SERVICE);
    WifiInfo info = wifi.getConnectionInfo();
    macAddress = info.getMacAddress();
    if (null == macAddress) {
        return "";
    }
    macAddress = macAddress.replace(":", "");
    return macAddress;
}
  • 获取当前程序的版本号
public static String getAppVersion(Context context) {
    String version = "0";
    try {
        version = context.getPackageManager().getPackageInfo(
                context.getPackageName(), 0).versionName;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return version;
}
  • px-dp转换
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);
}
  • 代码中设置TextView的drawable
//先从资源文件中获取Drawable
Drawable drawable = getResources().getDrawable(R.drawable.drawable);
//设置Drawable的边界,必须设置,不然显示不出来
//参数int left, int top, int right, int bottom
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
//设置到TextView中
//依次为left,top,right,bottom
textView.setCompoundDrawables(drawable, null, null, null);

你可能感兴趣的:(Android开发常用)