博客
关于我
android 常用的代码
阅读量:520 次
发布时间: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/

你可能感兴趣的文章
React input defaultValue不会更新状态怎么办?
查看>>
PHP函数__autoload失效原因(与smarty有关)
查看>>
PHP函数判断移动端和PC端
查看>>
Springboot基础入门
查看>>
php函数性能优化中应注意哪些问题?
查看>>
PHP函数操作数字和汉字互转(100以内)
查看>>
PHP函数方法
查看>>
PHP创建目录mkdir无写入权限的问题解决方案
查看>>
PHP删除指定目录下的所有文件和文件夹 | 删除指定文件
查看>>
php删除文件夹下面所有文件包括(删除文件夹)不删除文件夹
查看>>
React Collapse Pane 项目教程
查看>>
php判断ip黑名单程序代码
查看>>
php判断复选框是否被选中的方法
查看>>
PHP判断指定目录下是否存在文件
查看>>
php判断数组是否为空
查看>>
PHP判断数组是否有重复值、获取重复值
查看>>
springboot基于Web的社区留守儿童管理系统源码毕设+论文
查看>>
Springboot基于Redisson实现Redis分布式可重入锁【案例到源码分析】
查看>>
PHP利用正则表达式实现手机号码中间4位用星号(*)替换显示
查看>>
PHP加密与安全的最佳实践
查看>>