最近在用android 随时不停更新的一些常用代码,以备查找

目录

进制转换

二进制转10进制

10进制转二进制

10进制转16进制

16进制转10进制

二进制转16进制

16进制转二进制

16进制高位补0

十进制数据转换为16进制并高位在前,地位在后

睡多少秒后执行操作

调用浏览器 载入某网址

Broadcast接收系统广播的intent 监控应用程序包的安装 删除

使用Toast输出一个字符串

把一个字符串写进文件

把文件内容读出到一个字符串

打电话

跳转至拨号界面

发送短信

唤醒屏幕并解锁


进制转换

二进制转10进制

String two = "0001";int ten = Integer.parseInt(two, 2);


10进制转二进制

int ten = 10;String two = Integer.toBinaryString(ten);


10进制转16进制

int ten = 10;String sixteen = Integer.toHexString(ten);


16进制转10进制

String sixteen = "A6";int ten = Integer.parseInt(sixteen, 16);


二进制转16进制

String two = "0001";int ten = Integer.parseInt(two, 2);String sixteen = Integer.toHexString(ten);


16进制转二进制
 

String sixteen = "A6";int ten = Integer.parseInt(sixteen, 16);String two = Integer.toBinaryString(ten);

16进制高位补0

public static String ten2Hex2(int num) {    String strHex2 = String.format("%08x", num).toUpperCase();//高位补0    return strHex2;}

十进制数据转换为16进制并高位在前,地位在后

/**     * 十进制数据转换为16进制并高位在前,地位在后     * @param dec 十进制数据     * @return     */    public static String decToHex(int dec) {        String hex = "";        while(dec != 0) {            String h = Integer.toString(dec & 0xff, 16);            if((h.length() & 0x01) == 1)                h = '0' + h;            hex = hex + h;            dec = dec >> 8;        }        return hex;    }

睡多少秒后执行操作

new Handler().postDelayed(new Runnable() {                                @Override                                public void run() {                                    sendCommands(2,null);//睡一秒后执行握手动作                                }                            }, 1000);

调用浏览器 载入某网址

Uri uri = Uri.parse("http://www.066810.com");        Intent it = new Intent(Intent.ACTION_VIEW, uri);        startActivity(it);

Broadcast接收系统广播的intent 监控应用程序包的安装 删除

public class getBroadcast extends BroadcastReceiver {        @Override        public void onReceive(Context context, Intent intent) {                                 if(Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())){                    Toast.makeText(context, "有应用被添加", Toast.LENGTH_LONG).show();            }                else  if(Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())){                    Toast.makeText(context, "有应用被删除", Toast.LENGTH_LONG).show();            }                             else  if(Intent.ACTION_PACKAGE_REPLACED.equals(intent.getAction())){                    Toast.makeText(context, "有应用被替换", Toast.LENGTH_LONG).show();            }                                  else  if(Intent.ACTION_CAMERA_BUTTON.equals(intent.getAction())){                    Toast.makeText(context, "按键", Toast.LENGTH_LONG).show();            }                    }       }

使用Toast输出一个字符串

public void DisplayToast(String str){  Toast.makeText(this,str,Toast.LENGTH_SHORT).show();} 

把一个字符串写进文件

public void writefile(String str,String path ){    File file;    FileOutputStream out;     try {             //创建文件     file = new File(path);             file.createNewFile();                          //打开文件file的OutputStream             out = new FileOutputStream(file);             String infoToWrite = str;             //将字符串转换成byte数组写入文件             out.write(infoToWrite.getBytes());             //关闭文件file的OutputStream             out.close();                                                } catch (IOException e) {             //将出错信息打印到Logcat         DisplayToast(e.toString());                      }}

把文件内容读出到一个字符串

public String getinfo(String path){    File file;    String str="";     FileInputStream in;    try{    //打开文件file的InputStream     file = new File(path);         in = new FileInputStream(file);         //将文件内容全部读入到byte数组         int length = (int)file.length();         byte[] temp = new byte[length];         in.read(temp, 0, length);         //将byte数组用UTF-8编码并存入display字符串中         str =  EncodingUtils.getString(temp,TEXT_ENCODING);         //关闭文件file的InputStream                  in.close();    }catch (IOException e) {              DisplayToast(e.toString());             }    return str;}

打电话

/***打电话*/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)));}

发送短信

public static void sendSms(Context context, String phoneNumber,String content) {        Uri uri = Uri.parse("smsto:"                + (TextUtils.isEmpty(phoneNumber) ? "" : phoneNumber));        Intent intent = new Intent(Intent.ACTION_SENDTO, uri);        intent.putExtra("sms_body", TextUtils.isEmpty(content) ? "" : content);        context.startActivity(intent);}

唤醒屏幕并解锁

public static void wakeUpAndUnlock(Context context){         KeyguardManager km= (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);         KeyguardManager.KeyguardLock kl = km.newKeyguardLock("unLock");         //解锁         kl.disableKeyguard();         //获取电源管理器对象         PowerManager pm=(PowerManager) context.getSystemService(Context.POWER_SERVICE);         //获取PowerManager.WakeLock对象,后面的参数|表示同时传入两个值,最后的是LogCat里用的Tag         PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK,"bright");         //点亮屏幕         wl.acquire();         //释放         wl.release(); }需要在manifests添加权限

 

更多相关文章

  1. Android开发中使用矢量图
  2. Android分享文件到微信和QQ功能的实现(兼容android 7.0以上的共享
  3. Android热修复之AndFix
  4. 系统总结归纳一下android
  5. app在android studio的构建过程
  6. android的文件操作
  7. 分享几点Android(安卓)开发中的小技巧吧。不知道算不算?
  8. 浅谈android中的反编译
  9. NPM 和webpack 的基础使用

随机推荐

  1. 详解Ubuntu for Android:Thin Client
  2. android 下 数据持久化
  3. android 自动调整屏幕分辨率
  4. Fusion Garage宣布基于Android的Grid OS
  5. Android(安卓)4.0 Launcher2源码分析——
  6. (转)Android学习路线指南
  7. Android(安卓)Retrofit2.0实现文件上传和
  8. Android事件总线框架设计:EventBus3.0源码
  9. 常用控件及其属性(Button)
  10. android性能测试调试工具之dumpsys