最近在用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" />
转载:https://blog.csdn.net/xinqingch/article/details/116496960
查看评论