Android基础项目手机卫士总结

智慧北京.note//设置一个没有标题的activity
requestWindowFeature(Window.FEATURE_NO_TITLE);

设置自己的版本号
PackageManager pm = getPackageManager();
try {
PackageInfo packageInfo = pm.getPackageInfo(this.getPackageName(), 0);
versionName = packageInfo.versionName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}


安装指定的apk
//一种隐式调用
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.setData(Uri.fromFile(file));
intent.setType("application/vnd.android.package-archive");
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
startActivityForResult(intent,0);



查询电话号码

ContentResolver contentResolver = getContentResolver();
//做查询过程
Cursor query = contentResolver.query(Uri.parse("content://com.android.contacts/raw_contacts"), new String[]{"contact_id"}, null, null, null);
contactList.clear();
while (query.moveToNext()) {
String id = query.getString(0);
Cursor indexCursor = contentResolver.query(Uri.parse("content://com.android.contacts/data"), new String[]{"data1", "mimetype"}, "raw_contact_id=?", new String[]{id}, null);
HashMap hashMap = new HashMap();
while (indexCursor.moveToNext()) {
String data = indexCursor.getString(0);
String type = indexCursor.getString(1);
if (type.equals("vnd.android.cursor.item/phone_v2")) {
if (!TextUtils.isEmpty(data)) {
if (!(data==null)){
if (!data.equals("")){
hashMap.put("phone", data);

}


}
}
} else if (type.equals("vnd.android.cursor.item/name")) {
if (!TextUtils.isEmpty(data)) {
if (!(data==null)){
if (!data.equals("")){
hashMap.put("name", data);
}
}
}
}

}
contactList.add(hashMap);
indexCursor.close();
}
query.close();
mHhandler.sendEmptyMessage(0);

}


拿到手机的内存大小



private void initTitle() {
//获取磁盘的可用大小
String path= Environment.getDataDirectory().getAbsolutePath();
//获取sd可用的大小
String sdPath=Environment.getExternalStorageDirectory().getAbsolutePath();
String memoryAvailSpace= Formatter.formatFileSize(this, getAvailSpace(path));
String sdmemoryAvailSpace= Formatter.formatFileSize(this, getAvailSpace(sdPath));
tv_sd_memory= (TextView) findViewById(R.id.tv_sd_memory);
tv_memory= (TextView) findViewById(R.id.tv_memory);
tv_sd_memory.setText("sd卡内存"+sdmemoryAvailSpace);
tv_memory.setText("磁盘内存"+memoryAvailSpace);
}

private long getAvailSpace(String path) {
//拿到内存的块数 和每一个的大小然后相乘
StatFs statFs=new StatFs(path);
//拿到块数
long block = statFs.getAvailableBlocks();
//拿到每一块的大小
long size = statFs.getBlockSize();
return block*size;

}




//这是一种悬浮的窗口

View popupView = View.inflate(this, R.layout.popupwindow_view, null);
TextView tv_uninstall= (TextView) popupView.findViewById(R.id.tv_uninstall);
TextView tv_start= (TextView) popupView.findViewById(R.id.tv_start);
TextView tv_share= (TextView) popupView.findViewById(R.id.tv_share);
tv_uninstall.setOnClickListener(this);
tv_start.setOnClickListener(this);
tv_share.setOnClickListener(this);


AlphaAnimation alphaAnimation=new AlphaAnimation(0,1);

alphaAnimation.setDuration(600);
alphaAnimation.setFillAfter(true);
ScaleAnimation scaleAnimation = new ScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
scaleAnimation.setDuration(600);
scaleAnimation.setFillAfter(true);

AnimationSet animationSet = new AnimationSet(true);
animationSet.addAnimation(alphaAnimation);
animationSet.addAnimation(scaleAnimation);

PopupWindow popupWindow = new PopupWindow(popupView, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
popupWindow.setBackgroundDrawable(new ColorDrawable());
//指定窗体的位置
popupWindow.showAsDropDown(view,50,-view.getHeight());
popupView.startAnimation(animationSet);




public class AppInfoProvider {
/**
*
* @retrun 包含手机的集合
* @param context 一个上下文的环境对象
*/
public static List getAppInfoList(Context context){
//拿到一个包的管理者
PackageManager packageManager = context.getPackageManager();
//拿到所有应用的一个集合
List packageInfoList=packageManager.getInstalledPackages(0);
List arrayList=new ArrayList();
//遍历每一个集合
for (PackageInfo packageInfo:packageInfoList){
AppInfo appInfo=new AppInfo();
//拿到每一个应用的一个包名
appInfo.packageName=packageInfo.packageName;
ApplicationInfo applicationInfo= packageInfo.applicationInfo;
//拿到应用的一个名字
appInfo.name=applicationInfo.loadLabel(packageManager).toString();
//获取图标
appInfo.icon=applicationInfo.loadIcon(packageManager);
//判断是否为系统的一个应用
if ((applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM)==ApplicationInfo.FLAG_SYSTEM){
//系统的应用
appInfo.isSystem=true;
}else{
//正常的应用
appInfo.isSystem=false;
}
if ((applicationInfo.flags&ApplicationInfo.FLAG_EXTERNAL_STORAGE)==ApplicationInfo.FLAG_EXTERNAL_STORAGE){
//系统的应用
appInfo.isSdCard=true;
}else{
//正常的应用
appInfo.isSdCard=false;
}
arrayList.add(appInfo);
}
return arrayList;
}
}

String packageName = scanInfo.packageName;
//源码 卸载一个应用只需要拿到那个应用的一个包名
Intent intent = new Intent("android.intent.action.DELETE");
intent.addCategory("android.intent.category.DEFAULT");
intent.setData(Uri.parse("package:" + packageName));
startActivity(intent);


//备份短信的一个功能
public static void backup(Context ctx,String path,ProgressDialog pd){
//获取备份短信的写入文件
FileOutputStream fos=null;
Cursor cursor=null;


try {
File file = new File(path);
cursor = ctx.getContentResolver().query(Uri.parse("content://sms/"), new String[]{"address", "date", "type", "body"}, null, null, null);

pd.setMax(cursor.getCount());
fos=new FileOutputStream(file);
XmlSerializer newSerializer= Xml.newSerializer();
newSerializer.setOutput(fos, "utf-8");
newSerializer.startDocument("utf-8", true);
newSerializer.startTag(null, "smss");
while (cursor.moveToNext()){

newSerializer.startTag(null,"sms");
newSerializer.startTag(null, "address");
newSerializer.text(cursor.getString(0)+"");
newSerializer.endTag(null, "address");

newSerializer.startTag(null, "date");
newSerializer.text(cursor.getString(1)+"");
newSerializer.endTag(null, "date");

newSerializer.startTag(null, "type");
newSerializer.text(cursor.getString(2)+"");
newSerializer.endTag(null, "type");


/*
newSerializer.startTag(null, "body");
newSerializer.text(cursor.getString(3)+"");
newSerializer.endTag(null, "body");
*/


/**
*
newSerializer.startTag(null, "body");
newSerializer.text(cursor.getString(3));
newSerializer.endTag(null, "body");
*/

Log.i(TAG, cursor.getString(3));
newSerializer.endTag(null,"sms");
index++;
pd.setProgress(index);

}

newSerializer.endTag(null,"smss");
newSerializer.endDocument();






} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (cursor!=null&&fos!=null){

cursor.close();
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
pd.dismiss();
}
}

//拿到 一个服务是否服务被开启的一个功能
public static boolean isRunning(Context context, String serviceName) {
ActivityManager mAM = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

List runningServices = mAM.getRunningServices(100);
//遍历所有的集合

for (ActivityManager.RunningServiceInfo runningServiceInfo:runningServices){
String className = runningServiceInfo.service.getClassName();
if (className.equals(serviceName)){
return true;
}
}
return false;
}
//杀死后台进程
//获取管理者对象
ActivityManager activityManager= (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
//指定包名的进程
activityManager.killBackgroundProcesses(killList.getPackageName());

//获取流量(R 手机(2G,3G,4G)下载流量)
long mobileRxBytes = TrafficStats.getMobileRxBytes();
//获取手机的总流量(上传+下载)
//T total(手机(2G,3G,4G)总流量(上传+下载))
long mobileTxBytes = TrafficStats.getMobileTxBytes();
//total(下载流量总和(手机+wifi))
long totalRxBytes = TrafficStats.getTotalRxBytes();
//(总流量(手机+wifi),(上传+下载))
long totalTxBytes = TrafficStats.getTotalTxBytes();
//意义不大
//流量获取模块(发送短信),运营商(联通,移动....),(流量监听)第三方接口,广告
//短信注册



//显示一个吐司在电话来的时候

final WindowManager.LayoutParams params = mParams;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.format = PixelFormat.TRANSLUCENT;
params.type = WindowManager.LayoutParams.TYPE_PHONE;
params.setTitle("Toast");
params.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
params.gravity= Gravity.LEFT+Gravity.TOP;

//加载一个吐司的一个布局
viewToast=View.inflate(getApplicationContext(), R.layout.toast_view, null);

params.x=SpUtils.getInt(getApplication(), ConstantValue.LOCATION_X, 0);
params.y=SpUtils.getInt(getApplication(), ConstantValue.LOCATION_Y, 0);

tv_toast= (TextView) viewToast.findViewById(R.id.tv_toast);
styleColor = new int[]{R.drawable.call_locate_white,
R.drawable.call_locate_orange,
R.drawable.call_locate_blue,
R.drawable.call_locate_gray,
R.drawable.call_locate_green};

int styleIndex=SpUtils.getInt(getApplication(), ConstantValue.TOAST_STYLE,0);
tv_toast.setBackgroundResource(styleColor[styleIndex]);
mWM.addView(viewToast,mParams);
query(number);
}


//读取短信信息


Object[] objects = (Object[]) intent.getExtras().get("pdus");
for (Object object : objects) {
SmsMessage sms = SmsMessage.createFromPdu((byte[]) object);
String originatingAddress = sms.getOriginatingAddress();
String displayMessageBody = sms.getDisplayMessageBody();
blackNumberDao = BlackNumberDao.getInstance(context);
int mode = blackNumberDao.getMode(originatingAddress);
if (mode == 1 || mode == 3) {
//拦截短信
abortBroadcast();
}
}



你可能感兴趣的:(Android基础项目手机卫士总结)