1. 程式人生 > >Android基礎專案手機衛士總結

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<String, String> hashMap = new HashMap<String, String>(); 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<AppInfo> getAppInfoList(Context context){ //拿到一個包的管理者 PackageManager packageManager = context.getPackageManager(); //拿到所有應用的一個集合 List<PackageInfo> packageInfoList=packageManager.getInstalledPackages(0); List<AppInfo> arrayList=new ArrayList<AppInfo>(); //遍歷每一個集合 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<ActivityManager.RunningServiceInfo> 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(); } }