个人代码收集:持续整理中

1.获取应用软件版本

private PackageManager pm ;
 
     /**
     * 获取应用软件版本
     * @return
     */
    private String  getAppVersion(){
        pm = getPackageManager();
        try {
            PackageInfo pinfo  = pm.getPackageInfo("com.math.client", 0);
            String version = pinfo.versionName;
            return version;
        } catch (NameNotFoundException e) {
            e.printStackTrace();
            // can't reach
            return "";
        }
    }
View Code

 2.安装一个新的应用(两段代码,第一段代码摘抄过来未测试,第二段代码测试过没有问题)

/**
     * 安装一个新的应用
     * @param file    应用完整文件名称;
     */
    protected void installAPK(File file) {
        
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
        startActivity(intent);
    }
    
    private void installAPK() {
        File apkFile = new File(savePath, APK_NAME);
        if (!apkFile.exists()) {
            return;
        }
        cancelUpdate = false;//是否放弃更新标志位
        Intent intent = new Intent(Intent.ACTION_VIEW);
        // 从一个Activity中要通过intent调出另一个Activity的话,需要使用
        // FLAG_ACTIVITY_NEW_TASK,否则的话,会有force close:
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
        intent.setDataAndType(Uri.parse("file://" + apkFile.toString()),
                "application/vnd.android.package-archive");
        startActivity(intent);
    }
View Code

 3.拷贝assets目录文件的工具类

/**
     * 拷贝assets目录文件的工具类
     * @param context    上下文
     * @param fileName    assets目录下文件名称
     * @param url        目标完整路径那个名称
     * @return    拷贝成功 返回文件对象, 拷贝失败 返回null
     */
    public static File copyFile(Context context,String fileName,String url){
        try {
            InputStream is = context.getAssets().open(fileName);//打开assets目录下的文件
            File file = new File(url);
            FileOutputStream fos =new FileOutputStream(file);
            byte[] b = new byte[1024];
            int len = -1;
            while((len = is.read(b))!= -1){
                fos.write(b, 0, len);
                
            }
            fos.close();
            is.close();
            return file;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
View Code

4.读取res/raw文件夹文件

 public static String read(Context context, int id) {
        String str = null;
        try {
            InputStream in = context.getResources().openRawResource(id);
            int length = in.available();
            byte[] buffer = new byte[length];
            in.read(buffer);
            str = EncodingUtils.getString(buffer, "UTF-8");
            // 按照文件的编码类型选择合适的编码,如果不能调整会乱码
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return str;
    }
View Code

5.改变ListView组件的背景:

 拖动ListView,这时背景会变成黑色,调用ListView的setCacheColorHint(0x00000000)方法,将背景色改成透明。

6.获取Layout布局的两种方法:

View view = LayoutInflater.from(context).inflate(R.layout.布局文件, null);
View Code
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View Code

 7.打钩显示输入密码

if (cbk.isChecked()) {
                et.setTransformationMethod(HideReturnsTransformationMethod.getInstance());//显示密码内容
          }else {
                et.setTransformationMethod(PasswordTransformationMethod.getInstance()); //隐藏密码内容
          }
View Code

 8.监听开机启动自启动应用程序

  8.1 首先继承一个broadcastreceiver

public class ConnectBroadCastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
         if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
                Intent bootActivityIntent=new Intent(context,MainActivity.class);
                bootActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(MainActivity);//要启动应用程序的首界面
            }
    }

}
View Code

  8.2 在AndroidMenifest.xml中配置Receiver

<receiver android:name=".BootBroadcastReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"></action>
    </intent-filter>
</receiver>
View Code

  8.3 添加权限

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
View Code

9.获取手机系统版本

    /**
     * 获取手机系统版本
     * 
     * @return 手机版本号
     */
    public int getPhoneAndroidSDK() {
        return android.os.Build.VERSION.SDK_INT;
    }

10.获取手机IP地址

public String getLocalIpAddress() {
    try {
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); 
        en.hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); 
        enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    return inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (SocketException ex) {
        Log.e(LOG_TAG, ex.toString());
    }
    return null;
}
View Code

 11.Map<String, String> map取出里面的key value

for (String key : getMap().keySet()) {
            System.out.println("--key--"+key+"      "+"--value--"+getMap().get(key));
        }
原文地址:https://www.cnblogs.com/sishuiliuyun/p/3065268.html