博客
关于我
android 常用的代码
阅读量:493 次
发布时间:2019-03-07

本文共 4221 字,大约阅读时间需要 14 分钟。

在Android开发中,随时需要一些常用的代码示例以备查找。以下是一些常见的代码片段,涵盖了进制转换、文件操作、权限管理等多个方面。

1. 进制转换

1.1 二进制转10进制

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

1.2 10进制转二进制

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

1.3 10进制转16进制

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

1.4 16进制转10进制

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

1.5 二进制转16进制

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

1.6 16进制转二进制

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

1.7 16进制高位补0

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

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

public static String decToHex(int dec) {    String hex = "";    while (dec != 0) {        String h = Integer.toString(dec & 0xff, 16);        if ((h.length() & 1) == 1) {            h = '0' + h;        }        hex = hex + h;        dec = dec >> 8;    }    return hex;}

2. 睡眠处理

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

3. 调用浏览器

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

4. 广播接收

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();        }    }}

5. Toast使用

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

6. 文件操作

6.1 写文件

public void writefile(String str, String path) {    File file;    FileOutputStream out;    try {        file = new File(path);        file.createNewFile();        out = new FileOutputStream(file);        String infoToWrite = str;        out.write(infoToWrite.getBytes());        out.close();    } catch (IOException e) {        DisplayToast(e.toString());    }}

6.2 读文件

public String getinfo(String path) {    File file;    String str = "";    FileInputStream in;    try {        file = new File(path);        in = new FileInputStream(file);        int length = (int) file.length();        byte[] temp = new byte[length];        in.read(temp, 0, length);        str = EncodingUtils.getString(temp, TEXT_ENCODING);        in.close();    } catch (IOException e) {        DisplayToast(e.toString());    }    return str;}

7. 打电话

public static void call(Context context, String phoneNumber) {    context.startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNumber)));}

8. 跳转拨号界面

public static void callDial(Context context, String phoneNumber) {    context.startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNumber)));}

9. 发送短信

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);}

10. 唤醒屏幕并解锁

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 wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK, "bright");    wl.acquire();    wl.release();}

注意事项

  • 需要在AndroidManifest.xml中添加权限:

这些代码示例可以帮助开发者快速完成常见的Android操作,方便查找和使用。

转载地址:http://jrfjz.baihongyu.com/

你可能感兴趣的文章
Alibaba Cloud Linux 2 LTS 正式发布,提供更高性能和更多保障!
查看>>
李笑来必读书籍整理
查看>>
vue书籍整理
查看>>
记Java中有关内存的简单认识
查看>>
Mybatis配置解析
查看>>
http头部 Expect
查看>>
Hadoop(十六)之使用Combiner优化MapReduce
查看>>
C#实现outlook自动签名
查看>>
MySQL 5.5 My.cnf 模版
查看>>
使用mysqladmin ext了解MySQL运行状态【转】
查看>>
【程序员的脑洞故事】盘古,开辟天地
查看>>
《机器学习Python实现_10_06_集成学习_boosting_gbdt分类实现》
查看>>
Java JFR 民间指南 - 事件详解 - jdk.ObjectAllocationSample
查看>>
对比讲解lambda表达式与传统接口函数实现方式
查看>>
使用java8API遍历过滤文件目录及子目录及隐藏文件
查看>>
精讲响应式WebClient第2篇-GET请求阻塞与非阻塞调用方法详解
查看>>
java9系列第二篇-资源自动关闭的语法增强
查看>>
CoreCLR源码探索(八) JIT的工作原理(详解篇)
查看>>
【数组】59. 螺旋矩阵 II
查看>>
【哈希表】1. 两数之和
查看>>