提取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線程被堵塞的方法。

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM