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

本文共 4311 字,大约阅读时间需要 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/

你可能感兴趣的文章
Numpy 科学计算库详解
查看>>
Numpy.fft.fft和numpy.fft.fftfreq有什么不同
查看>>
Numpy.ndarray对象不可调用
查看>>
Numpy:按多个条件过滤行?
查看>>
Numpy:条件总和
查看>>
numpy、cv2等操作图片基本操作
查看>>
numpy判断对应位置是否相等,all、any的使用
查看>>
Numpy如何使用np.umprod重写range函数中i的python
查看>>
numpy数组替换其中的值(如1替换为255)
查看>>
numpy数组索引-ChatGPT4o作答
查看>>
NUMPY矢量化np.prod不能构造具有超过32个操作数的ufunc
查看>>
Numpy矩阵与通用函数
查看>>
numpy绘制热力图
查看>>
numpy转PIL 报错TypeError: Cannot handle this data type
查看>>
Numpy闯关100题,我闯了95关,你呢?
查看>>
Nutch + solr 这个配合不错哦
查看>>
NutzCodeInsight 2.0.7 发布,为 nutz-sqltpl 提供友好的 ide 支持
查看>>
NutzWk 5.1.5 发布,Java 微服务分布式开发框架
查看>>
NUUO网络视频录像机 css_parser.php 任意文件读取漏洞复现
查看>>
NuxtJS 接口转发详解:Nitro 的用法与注意事项
查看>>