Android常用代码

一、获取系统版本号:

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

int versionCode=nfo.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"

不让文本框输入中文:

1. 在item试图上面添加代码: android:descendantFocusability="blocksDescendants" 

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

获取屏幕宽高

DisplayMetrics displayMetrics = new DisplayMetrics();  

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

int height = displayMetrics.heightPixels;  

int width = displayMetrics.widthPixels;

程序中安装apk
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);
获取设备型号、SDK版本及系统版本
String device_model = Build.MODEL; // 设备型号    

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

String version_release = Build.VERSION.RELEASE; // 设备的系统版本

当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();

   }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值