提取apk文件(一)

前言

我是函数开始学习android编程了。今天是3月16日。

阅读要求:

读者应该会基本的android开发基础。

主题

第一个作品就是如何从手机里提取出apk文件啦。

因为要学习android编程,我认为还是从别人的实例学起比较好。

提取了apk,上传到pc然后进行反编译就能看到资源文件和源代码了。

然后就可以好好学习了,但不要照搬人家的东西呀。

正文

本次的UI布局相当简单。

就是一个listview。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android
="http://schemas.android.com/apk/res/android">
<com.google.ads.AdView android:id="@id/adView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" ads:adSize="BANNER" ads:adUnitId="***********"
xmlns:ads
="http://schemas.android.com/apk/lib/com.google.ads" />
<ListView android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_below="@id/adView" />
</RelativeLayout>

一个相对布局,上面一个广告的view,下面填充listview就行。

然后就需要定制一个listitem来显示apk的信息,包括图标,名称,包名,文件大小等信息吧。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="fill_parent" android:layout_height="70.0dip"
xmlns:android
="http://schemas.android.com/apk/res/android">
<ImageView android:layout_gravity="center" android:id="@android:+id/icon" android:layout_width="65.0dip" android:layout_height="65.0dip" android:layout_marginRight="5.0dip" />
<LinearLayout android:layout_gravity="center" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView android:textSize="16.0sp" android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleLine="true" />
<TextView android:id="@+id/packageName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleLine="true" />
<TextView android:id="@+id/size" android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleLine="true" />
</LinearLayout>
</LinearLayout>

这里面一个重要的东西是dip,用dip做单位比像素更能适用不同分辨率的屏幕。

这样UI就写完了。

先来完成基础的功能。

1 后台加载所有的app

  

    /**
* 载入apps
*/
private void loadApps() {
     //显示进度对话框
final Dialog infoDialog = new ProgressDialog(this);
runOnUiThread(new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
if (adapter != null)
adapter.clear();
infoDialog.show();
}
});
    //开启一个线程载入所有的apps
new Thread() {
public void run() {
Intent it = new Intent("android.intent.action.MAIN", null);
it.addCategory("android.intent.category.LAUNCHER");
List apps = getPackageManager().queryIntentActivities(it, 128);
int i = apps.size() - 1;
if (i > 0) {
adapter = new ApkListAdapter(getApplicationContext(),
R.layout.list_item, apps);
}
runOnUiThread(new Runnable() {
public void run() {
setListAdapter(adapter);
}
});
infoDialog.dismiss();
}

}.start();
}

 

 这里主要用了一个意图intent获取了app的list然后放到list的适配器里去。这个过程中显示进度对话框。这里注意

runOnUiThread
这个方法。

2 适配器的写法

 

static class ApkListAdapter extends ArrayAdapter<ResolveInfo> {
PackageManager pm;

public ApkListAdapter(Context paramContext, int paramInt,
List<ResolveInfo> paramList) {
super(paramContext, paramInt, paramList);
this.pm = paramContext.getPackageManager();
}

// 组装view
public View getView(int postion, View item, ViewGroup viewGroup) {
View itemview = item;
if(itemview==null){
itemview = ((LayoutInflater)getContext().getSystemService("layout_inflater")).inflate(R.layout.list_item, null);
}
ResolveInfo apkInfo = (ResolveInfo) getItem(postion);
((ImageView) itemview.findViewById(R.id.icon))
.setImageDrawable(apkInfo.loadIcon(this.pm));
((TextView) itemview.findViewById(R.id.name)).setText(apkInfo
.loadLabel(pm));
((TextView) itemview.findViewById(R.id.packageName))
.setText(apkInfo.activityInfo.packageName);
return itemview;
}
}

 

 适配器是list和listItem间的桥梁,主要的方法是getview,将组装好的view交给list。这里要了解下包管理器的相关知识吧。

3 完成一个基本的功能 点击一个item打开相应的应用

        //点击时启动相应的app
getListView().setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View item, int postion,
long arg3) {
// TODO Auto-generated method stub

ResolveInfo apkInfo = (ResolveInfo) adapterView
.getItemAtPosition(postion);
Intent localIntent = new Intent("android.intent.action.MAIN");
localIntent.setClassName(
apkInfo.activityInfo.packageName,
apkInfo.activityInfo.name);
startActivity(localIntent);
}
});


运行截图

总结

1 掌握listview的使用,listactivity的使用。

2 如何构造一个listApdater。

3 读取应用信息,启动应用的intent的写法。

4 布局的基本知识

5 避免UI线程被堵塞的方法。

 

 

 

 

 

posted @ 2012-03-16 21:13  我是函数  阅读(3474)  评论(2编辑  收藏  举报