当前位置:  编程技术>移动开发
本页文章导读:
    ▪获取SD卡下 未安装 APK文件 的图标等        获取SD卡上 未安装 APK文件 的图标等 各版本通用 /** * 获取未安装的apk信息 * * @param ctx * @param apkPath * @return */ public static AppInfoData getApkFileInfo(Context ctx, String apkPath) { System.out.print.........
    ▪ jesson.shen浅谈HTTP中兑现UDP/TCP        jesson.shen浅谈HTTP中实现UDP/TCP http://350526.blog.51cto.com/340526/365509 ......
    ▪ LayoutInflater种使用理解小结       LayoutInflater类使用理解小结 前段时间学习,发现好多地方用到了 LayoutInflater这个类,我小结一下,希望给大家也给自己以后学习带来帮助。LayoutInfalter 看api解释 This class is used to instantiate la.........

[1]获取SD卡下 未安装 APK文件 的图标等
    来源: 互联网  发布时间: 2014-02-18
获取SD卡上 未安装 APK文件 的图标等
各版本通用

/**
	 * 获取未安装的apk信息
	 * 
	 * @param ctx
	 * @param apkPath
	 * @return
	 */
	public static AppInfoData getApkFileInfo(Context ctx, String apkPath) {
		System.out.println(apkPath);
		File apkFile = new File(apkPath);
		if (!apkFile.exists() || !apkPath.toLowerCase().endsWith(".apk")) {
			System.out.println("文件路径不正确");
			return null;
		}
		AppInfoData appInfoData;
		String PATH_PackageParser = "android.content.pm.PackageParser";
		String PATH_AssetManager = "android.content.res.AssetManager";
		try {
			//反射得到pkgParserCls对象并实例化,有参数
			Class<?> pkgParserCls = Class.forName(PATH_PackageParser);
			Class<?>[] typeArgs = {String.class};
			Constructor<?> pkgParserCt = pkgParserCls.getConstructor(typeArgs);
			Object[] valueArgs = {apkPath};
			Object pkgParser = pkgParserCt.newInstance(valueArgs);
			
			//从pkgParserCls类得到parsePackage方法
			DisplayMetrics metrics = new DisplayMetrics();
			metrics.setToDefaults();//这个是与显示有关的, 这边使用默认
			typeArgs = new Class<?>[]{File.class,String.class,
									DisplayMetrics.class,int.class};
			Method pkgParser_parsePackageMtd = pkgParserCls.getDeclaredMethod(
					"parsePackage", typeArgs);
			
			valueArgs=new Object[]{new File(apkPath),apkPath,metrics,0};
			
			//执行pkgParser_parsePackageMtd方法并返回
			Object pkgParserPkg = pkgParser_parsePackageMtd.invoke(pkgParser,
					valueArgs);
			
			//从返回的对象得到名为"applicationInfo"的字段对象	
			if (pkgParserPkg==null) {
				return null;
			}
			Field appInfoFld = pkgParserPkg.getClass().getDeclaredField(
					"applicationInfo");
			
			//从对象"pkgParserPkg"得到字段"appInfoFld"的值
			if (appInfoFld.get(pkgParserPkg)==null) {
				return null;
			}
			ApplicationInfo info = (ApplicationInfo) appInfoFld
					.get(pkgParserPkg);			
			
			//反射得到assetMagCls对象并实例化,无参
			Class<?> assetMagCls = Class.forName(PATH_AssetManager);			
			Object assetMag = assetMagCls.newInstance();
			//从assetMagCls类得到addAssetPath方法
			typeArgs = new Class[1];
			typeArgs[0] = String.class;
			Method assetMag_addAssetPathMtd = assetMagCls.getDeclaredMethod(
					"addAssetPath", typeArgs);
			valueArgs = new Object[1];
			valueArgs[0] = apkPath;
			//执行assetMag_addAssetPathMtd方法
			assetMag_addAssetPathMtd.invoke(assetMag, valueArgs);
			
			
			//得到Resources对象并实例化,有参数
			Resources res = ctx.getResources();
			typeArgs = new Class[3];
			typeArgs[0] = assetMag.getClass();
			typeArgs[1] = res.getDisplayMetrics().getClass();
			typeArgs[2] = res.getConfiguration().getClass();
			Constructor<Resources> resCt = Resources.class
					.getConstructor(typeArgs);
			valueArgs = new Object[3];
			valueArgs[0] = assetMag;
			valueArgs[1] = res.getDisplayMetrics();
			valueArgs[2] = res.getConfiguration();
			res = (Resources) resCt.newInstance(valueArgs);
			
			
			// 读取apk文件的信息
			appInfoData = new AppInfoData();
			if (info!=null) {
				if (info.icon != 0) {// 图片存在,则读取相关信息
					Drawable icon = res.getDrawable(info.icon);// 图标
					appInfoData.setAppicon(icon);
					}
				if (info.labelRes != 0) {
					String neme = (String) res.getText(info.labelRes);// 名字
					appInfoData.setAppname(neme);
				}else {
					String apkName=apkFile.getName();
					appInfoData.setAppname(apkName.substring(0,apkName.lastIndexOf(".")));
				}
				String pkgName = info.packageName;// 包名			
				appInfoData.setApppackage(pkgName);
			}else {
				return null;
			}			
			PackageManager pm = ctx.getPackageManager();
			PackageInfo packageInfo = pm.getPackageArchiveInfo(apkPath, PackageManager.GET_ACTIVITIES);
			if (packageInfo != null) {
				appInfoData.setAppversion(packageInfo.versionName);//版本号
				appInfoData.setAppversionCode(packageInfo.versionCode+"");//版本码
			}
			return appInfoData;
		} catch (Exception e) {	
			e.printStackTrace();
		}
		return null;
	}


    
[2] jesson.shen浅谈HTTP中兑现UDP/TCP
    来源: 互联网  发布时间: 2014-02-18
jesson.shen浅谈HTTP中实现UDP/TCP
http://350526.blog.51cto.com/340526/365509

    
[3] LayoutInflater种使用理解小结
    来源: 互联网  发布时间: 2014-02-18
LayoutInflater类使用理解小结
前段时间学习,发现好多地方用到了 LayoutInflater这个类,我小结一下,希望给大家也给自己以后学习带来帮助。
LayoutInfalter 看api解释 This class is used to instantiate layout XML file into its corresponding View objects. 作用相当于 类.findViewById();不过前者是实例化xml文件(多用于局部实例化,另外的布局文件实例化,经常和适配器结合使用返回当前的view),后者实例化view控件, inflater 可以理解为扩展,放大。

实例化方法在api有两种,看源代码发现有三种。
第一种方法:
LayoutInflater inflater = LayoutInflater.from(this);
View layout = inflater.inflate(R.layout.main, null);
源码中:
    public static LayoutInflater from(Context context) {
        LayoutInflater LayoutInflater =
                (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (LayoutInflater == null) {
            throw new AssertionError("LayoutInflater not found.");
        }
        return LayoutInflater;
    }

其实第一种方法,其实调用的就是第三种方法。

第二种方法:
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.main, null);
第三种方法:
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.main, null);

代码:
aa.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
 
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="/blog_article/@drawable/icon/index.html"
>
</ImageView>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="aaaaaaaa"
>
</TextView>
</LinearLayout>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
   <Button
   android:id="@+id/button_1"
   android:layout_width="wrap_content"
    android:layout_height="wrap_content"
   />
  
</LinearLayout>
MainActivity.java
package com.aqi.app;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
private Button button = null;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        button = (Button) findViewById(R.id.button_1);
        button.setText("局部对话框");
        button.setOnClickListener(new  View.OnClickListener() {
public void onClick(View v) {
Builder diaLog = new AlertDialog.Builder(MainActivity.this);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout =  inflater.inflate(R.layout.aa, null);
diaLog.setView(layout);
diaLog.create();
diaLog.show();
}
});
    }
}


网上找了点儿:
另外补充下,getSystemService是Activity中的方法,根据传入的name来取得对应的服务对象,这些服务名称参数都是Context类中的常量:

传入的Name 返回的对象 说明
WINDOW_SERVICE WindowManager 管理打开的窗口程序
LAYOUT_INFLATER_SERVICE LayoutInflater 取得xml里定义的view
ACTIVITY_SERVICE ActivityManager 管理应用程序的系统状态
POWER_SERVICE PowerManger 电源的服务
ALARM_SERVICE AlarmManager 闹钟的服务
NOTIFICATION_SERVICE NotificationManager 状态栏的服务
KEYGUARD_SERVICE KeyguardManager 键盘锁的服务
LOCATION_SERVICE LocationManager 位置的服务,如GPS
SEARCH_SERVICE SearchManager 搜索的服务
VEBRATOR_SERVICE Vebrator 手机震动的服务
CONNECTIVITY_SERVICE Connectivity 网络连接的服务
WIFI_SERVICE WifiManager Wi-Fi服务
TELEPHONY_SERVICE TeleponyManager 电话服务

    
最新技术文章:
▪Android开发之登录验证实例教程
▪Android开发之注册登录方法示例
▪Android获取手机SIM卡运营商信息的方法
▪Android实现将已发送的短信写入短信数据库的...
▪Android发送短信功能代码
▪Android根据电话号码获得联系人头像实例代码
▪Android中GPS定位的用法实例
▪Android实现退出时关闭所有Activity的方法
▪Android实现文件的分割和组装
▪Android录音应用实例教程
▪Android双击返回键退出程序的实现方法
▪Android实现侦听电池状态显示、电量及充电动...
▪Android获取当前已连接的wifi信号强度的方法
▪Android实现动态显示或隐藏密码输入框的内容
▪根据USER-AGENT判断手机类型并跳转到相应的app...
▪Android Touch事件分发过程详解
▪Android中实现为TextView添加多个可点击的文本
▪Android程序设计之AIDL实例详解
▪Android显式启动与隐式启动Activity的区别介绍
▪Android按钮单击事件的四种常用写法总结
▪Android消息处理机制Looper和Handler详解
▪Android实现Back功能代码片段总结
▪Android实用的代码片段 常用代码总结
▪Android实现弹出键盘的方法
▪Android中通过view方式获取当前Activity的屏幕截...
▪Android提高之自定义Menu(TabMenu)实现方法
▪Android提高之多方向抽屉实现方法
▪Android提高之MediaPlayer播放网络音频的实现方法...
▪Android提高之MediaPlayer播放网络视频的实现方法...
▪Android提高之手游转电视游戏的模拟操控
 


站内导航:


特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

©2012-2021,