最近在用android 随时不停更新的一些常用代码,以备查找
目录
Broadcast接收系统广播的intent 监控应用程序包的安装 删除
进制转换
二进制转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添加权限
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
获取当前设备的IMEI
/**
*判断是否手机
*
**/
public static boolean isPhone(Context context) {
TelephonyManager telephony = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
if (telephony.getPhoneType() == TelephonyManager.PHONE_TYPE_NONE) {
return false;
} else {
return true;
}
}
/**
*
*取IMEI
**/
@TargetApi(Build.VERSION_CODES.CUPCAKE)
public static String getDeviceIMEI(Context context) {
String deviceId;
if (isPhone(context)) {
TelephonyManager telephony = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
deviceId = telephony.getDeviceId();
} else {
deviceId = Settings.Secure.getString(context.getContentResolver(),
Settings.Secure.ANDROID_ID);
}
return deviceId;
}
获取当前设备的版本号
/**
* 获取APP版本, 此处取的是版本数值编号
* @return
*/
public static int getVersionCode() {
// 包管理器 可以获取清单文件信息
PackageManager packageManager = getPackageManager();
try {
// 获取包信息
// 参1 包名 参2 获取额外信息的flag 不需要的话 写0
PackageInfo packageInfo = packageManager.getPackageInfo(
getPackageName(), 0);
return packageInfo.versionCode;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return 0;
}
/**
* 获取APP版本, 此处取的是版本名称
* @return
*/
public static String getAppVersion(Context context) {
String version = "0" ;
try {
version = context.getPackageManager().getPackageInfo(
context.getPackageName(), 0 ).versionName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return version;
}