PackageInfo info = this.getPackageManager().getPackageInfo(this.getPackageName(), 0); int versionCode=nfo.versionCode string versionName=info.versionNam
<SPAN style="FONT-SIZE: 16px">String archiveFilePath="sdcard/download/Law.apk";//安裝包路徑 PackageManager pm = getPackageManager(); PackageInfo info = pm.getPackageArchiveInfo(archiveFilePath, PackageManager.GET_ACTIVITIES); if(info != null){ ApplicationInfo appInfo = info.applicationInfo; String appName = pm.getApplicationLabel(appInfo).toString(); String packageName = appInfo.packageName; //獲得安裝包名稱 String version=info.versionName; //獲得版本信息 Toast.makeText(test4.this, "packageName:"+packageName+";version:"+version, Toast.LENGTH_LONG).show(); Drawable icon = pm.getApplicationIcon(appInfo);//獲得圖標信息 TextView tv = (TextView)findViewById(R.id.tv); //顯示圖標 tv.setBackgroundDrawable(icon);</SPAN>
<SPAN style="FONT-SIZE: 16px">(1)android中獲取當前程序路徑 getApplicationContext().getFilesDir().getAbsolutePath() (2)android取已安裝的程序列表 List<PackageInfo> packageInfoList = getPackageManager().getInstalledPackages(0);</SPAN>
<SPAN style="FONT-SIZE: 16px">PackageManager pManager = MessageSendActivity.this.getPackageManager(); List<PackageInfo> appList = Utils.getAllApps(MessageSendActivity.this); for(int i=0;i<appList.size();i++) { PackageInfo pinfo = appList.get(i); ShareItemInfo shareItem = new ShareItemInfo(); //set Icon shareItem.setIcon(pManager.getApplicationIcon(pinfo.applicationInfo)); //set Application Name shareItem.setLabel(pManager.getApplicationLabel(pinfo.applicationInfo).toString()); //set Package Name shareItem.setPackageName(pinfo.applicationInfo.packageName); }</SPAN>
<SPAN style="FONT-SIZE: 16px">1. 在item試圖上面添加代碼: android:descendantFocusability="blocksDescendants" 2.在listview裏 添加代碼 android:focusable="true"</SPAN>
<SPAN style="FONT-SIZE: 16px">android:digits="1234567890qwertyuiopasdfghjklzxcvbnm`-=[]\;,./~!@#$%^*()_+}{:?&<>"'" 這樣就不會輸入中文了。 </SPAN>
DisplayMetrics displayMetrics = new DisplayMetrics(); this.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); int height = displayMetrics.heightPixels; int width = displayMetrics.widthPixels;
EditText有焦點(focusable爲true)阻止輸入法彈出 editText.setInputType(InputType.TYPE_NULL); // 關閉軟鍵盤 當EidtText無焦點(focusable=false)時阻止輸入法彈出 java
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
4.0上若是還用Theme.Dialog,只能說很土,跟總體UI風格差異很大 請使用android:theme="@android:style/Theme.Holo.DialogWhenLarge"
Intent intent = new Intent(); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setAction(android.content.Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(「APK」),"application/vnd.android.package-archive"); startActivity(intent);
String device_model = Build.MODEL; // 設備型號 String version_sdk = Build.VERSION.SDK; // 設備SDK版本 String version_release = Build.VERSION.RELEASE; // 設備的系統版本
public void SharePhoto(String photoUri,final Activity activity) { Intent shareIntent = new Intent(Intent.ACTION_SEND); File file = new File(photoUri); shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file)); shareIntent.setType("image/jpeg"); StartActivity(Intent.createChooser(shareIntent, activity.getTitle())); }
public void forceStopPackage(final String packageName) { if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES) != PackageManager.PERMISSION_GRANTED) { String msg = "Permission Denial: forceStopPackage() from pid=" + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES; Slog.w(TAG, msg); throw new SecurityException(msg); } long callingId = Binder.clearCallingIdentity(); try { IPackageManager pm = ActivityThread.getPackageManager(); int pkgUid = -1; synchronized(this) { try { pkgUid = pm.getPackageUid(packageName); } catch (RemoteException e) { } if (pkgUid == -1) { Slog.w(TAG, "Invalid packageName: " + packageName); return; } //begin:加入一個判斷條件 if (packageName.equals("你的進程名")) { return; } //end: 加入一個判斷條件 forceStopPackageLocked(packageName, pkgUid); } } finally { Binder.restoreCallingIdentity(callingId); } }
另:其餘方法: android
1,首先在你的service的onDestory方法裏面寫上啓動你本身的代碼,爲何要寫這個?由於若是用戶是在設置->應用程序->正在運行服務這裏面殺掉你service的話會調用到onDestory方法的,這裏就能夠啓動了,2:監聽屏幕關閉廣播,屏幕已關閉,就啓動服務。3:監聽屏幕解鎖廣播,同樣的道理,這樣,基本上,你的service就達到永不中止了。對用戶來講有點變態,但不少軟件都這樣。
以上部分代碼是從別人博客摘抄而來,沒有指明出處抱歉。 git
當listview滑動到底部或者頂部,會出現金色動畫,去掉的辦法 app
listView.setOverScrollMode(View.OVER_SCROLL_NEVER); ide
獲取應用程序下全部Activity 動畫
public static ArrayList<String> getActivities(Context ctx) { ArrayList<String> result = new ArrayList<String>(); Intent intent = new Intent(Intent.ACTION_MAIN, null); intent.setPackage(ctx.getPackageName()); for (ResolveInfo info : ctx.getPackageManager().queryIntentActivities(intent, 0)) { result.add(info.activityInfo.name); } return result; }
檢測字符串中是否包含漢字 ui
public static boolean checkChinese(String sequence) { final String format = "[\\u4E00-\\u9FA5\\uF900-\\uFA2D]"; boolean result = false; Pattern pattern = Pattern.compile(format); Matcher matcher = pattern.matcher(sequence); result = matcher.find(); return result; }
檢測字符串中只能包含:中文、數字、下劃線(_)、橫線(-) this
public static boolean checkNickname(String sequence) { final String format = "[^\\u4E00-\\u9FA5\\uF900-\\uFA2D\\w-_]"; Pattern pattern = Pattern.compile(format); Matcher matcher = pattern.matcher(sequence); return !matcher.find(); }
檢查有沒有應用程序來接受處理你發出的intent spa
public static boolean isIntentAvailable(Context context, String action) { final PackageManager packageManager = context.getPackageManager(); final Intent intent = new Intent(action); List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY); return list.size() > 0; }
使用TransitionDrawable實現漸變效果 rest
private void setImageBitmap(ImageView imageView, Bitmap bitmap) { // Use TransitionDrawable to fade in. final TransitionDrawable td = new TransitionDrawable(new Drawable[] { new ColorDrawable(android.R.color.transparent), new BitmapDrawable(mContext.getResources(), bitmap) }); //noinspection deprecation imageView.setBackgroundDrawable(imageView.getDrawable()); imageView.setImageDrawable(td); td.startTransition(200); }
比使用AlphaAnimation效果要好,可避免出現閃爍問題。
掃描指定的文件
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));
用途:從本軟件新增、修改、刪除圖片、文件某一個文件(音頻、視頻)須要更新系統媒體庫時使用,沒必要掃描整個SD卡。
Dip轉px
public static int dipToPX(final Context ctx, float dip) { return (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, ctx.getResources().getDisplayMetrics()); }