本文共 4221 字,大约阅读时间需要 14 分钟。
在Android开发中,随时需要一些常用的代码示例以备查找。以下是一些常见的代码片段,涵盖了进制转换、文件操作、权限管理等多个方面。
String two = "0001";int ten = Integer.parseInt(two, 2);
int ten = 10;String two = Integer.toBinaryString(ten);
int ten = 10;String sixteen = Integer.toHexString(ten);
String sixteen = "A6";int ten = Integer.parseInt(sixteen, 16);
String two = "0001";int ten = Integer.parseInt(two, 2);String sixteen = Integer.toHexString(ten);
String sixteen = "A6";int ten = Integer.parseInt(sixteen, 16);String two = Integer.toBinaryString(ten);
public static String ten2Hex2(int num) { String strHex2 = String.format("%08x", num).toUpperCase(); return strHex2;}
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;}
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);
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(); } }}
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(); out = new FileOutputStream(file); String infoToWrite = str; out.write(infoToWrite.getBytes()); out.close(); } catch (IOException e) { DisplayToast(e.toString()); }}
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;}
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 wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK, "bright"); wl.acquire(); wl.release();}
这些代码示例可以帮助开发者快速完成常见的Android操作,方便查找和使用。
转载地址:http://jrfjz.baihongyu.com/