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)时阻止输入法弹出
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); } }
另:其他方法:
1,首先在你的service的onDestory方法里面写上启动你自己的代码,为什么要写这个?因为如果用户是在设置->应用程序->正在运行服务这里面杀掉你service的话会调用到onDestory方法的,这里就可以启动了,2:监听屏幕关闭广播,屏幕已关闭,就启动服务。3:监听屏幕解锁广播,一样的道理,这样,基本上,你的service就达到永不停止了。对用户来说有点变态,但很多软件都这样。
以上部分代码是从别人博客摘抄而来,没有指明出处抱歉。
当listview滑动到底部或者顶部,会出现金色动画,去掉的办法
listView.setOverScrollMode(View.OVER_SCROLL_NEVER);
获取应用程序下所有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; }
检测字符串中是否包含汉字
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; }
检测字符串中只能包含:中文、数字、下划线(_)、横线(-)
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
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实现渐变效果
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()); }