您的位置:首页 > 移动开发 > Android开发

android实用代码块

2014-04-02 18:47 260 查看
一、获取系统版本号:

PackageInfo info = this.getPackageManager().getPackageInfo(this.getPackageName(), 0); 

int versionCode=info.versionCode 

string versionName=info.versionNam

二、获取系统信息:

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)android中获取当前程序路径 

getApplicationContext().getFilesDir().getAbsolutePath() 

(2)android取已安装的程序列表 

List<PackageInfo> packageInfoList = getPackageManager().getInstalledPackages(0);
四、获取图片、应用名、包名

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. 在item试图上面添加代码: android:descendantFocusability="blocksDescendants" 

2.在listview里 添加代码 android:focusable="true"

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

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

DisplayMetrics displayMetrics = new DisplayMetrics(); 

this.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); 

int height = displayMetrics.heightPixels; 

int width = displayMetrics.widthPixels;
八 获取设备型号、SDK版本及系统版本

String device_model = Build.MODEL; // 设备型号 

String version_sdk = Build.VERSION.SDK; // 设备SDK版本 

String version_release = Build.VERSION.RELEASE; // 设备的系统版本
九,获取应用程序下所有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;

}

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android