常用代码块_Faustine

一,获取系统版本号:

1
2
3
PackageInfo info = this.getPackageManager().getPackageInfo(this.getPackageName(), 0);
int versionCode=nfo.versionCode
string versionName=info.versionName

二、获取系统信息:

1
2
3
4
5
6
7
8
9
10
11
12
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);

三、获取安装路径和已安装程序列表

1
2
3
4
5
(1)android中获取当前程序路径
getApplicationContext().getFilesDir().getAbsolutePath()
 
(2)android取已安装的程序列表
List<PackageInfo> packageInfoList = getPackageManager().getInstalledPackages(0

四、获取图片、应用名、包名

1
2
3
4
5
6
7
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));

五、解决listview上 Item上有按钮时 item本身不能点击的问题:

1
2
1. 在item试图上面添加代码: android:descendantFocusability="blocksDescendants"
2.在listview里 添加代码 android:focusable="true"

六、不让文本框输入中文:

1
android:digits="1234567890qwertyuiopasdfghjklzxcvbnm`-=[];,./~!@#$%^*()_+}{:?&<>"'" 这样就不会输入中文了。

七,获取屏幕宽高

1
2
3
4
DisplayMetrics displayMetrics = new DisplayMetrics(); 
this.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); 
int height = displayMetrics.heightPixels; 
int width = displayMetrics.widthPixels;

八 获取设备型号、SDK版本及系统版本

1
2
3
String device_model = Build.MODEL; // 设备型号  
String version_sdk = Build.VERSION.SDK; // 设备SDK版本  
String version_release = Build.VERSION.RELEASE; // 设备的系统版本

九,获取应用程序下所有Activity

1
2
3
4
5
6
7
8
9
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;
}
原文地址:https://www.cnblogs.com/jingAndroid/p/6705146.html