獲取未安裝APK的一些基本信息的代碼
即使APK沒有安裝在手機上,我們也可以獲取它的某些信息,比如包名,版本,應用圖標,activities等等,代碼如下:
MainActivity:
package com.home.getapkinfo;import java.io.File; import java.util.ArrayList; import android.app.Activity; import android.content.Intent; import android.content.pm.ActivityInfo; import android.content.pm.ApplicationInfo; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.os.Environment; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener { private Button getPackageBtn; private Button getVersionNameBtn; private Button getVersionCodeBtn; private Button getActivitiesBtn; private Button getIcon; private ImageView iconImage; private EditText apkName; private TextView packageNameText; private TextView versionNameText; private TextView versionCodeText; private PackageManager pm; private String apkPath; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); pm = getPackageManager(); initWidget(); } @Override public void onClick(View v) { // 驗證應用名 String name = apkName.getText().toString(); if ("".equals(name) || null == name) { Toast.makeText(this, "應用名稱不能為空", Toast.LENGTH_SHORT).show(); return; } File file = new File(Environment.getExternalStorageDirectory(), name + ".apk"); if (!file.exists()) { Toast.makeText(this, "該應用不存在", Toast.LENGTH_SHORT).show(); return; } apkPath = file.getAbsolutePath(); if (v == getPackageBtn) { packageNameText.setText("package name:" + getPackageName(apkPath)); } else if (v == getVersionNameBtn) { versionNameText.setText("version name:" + getVersionName(apkPath)); } else if (v == getVersionCodeBtn) { versionCodeText.setText("version code:" + getVersionCode(apkPath) + ""); } else if (v == getActivitiesBtn) { ArrayList<String> list = getActivitiesName(apkPath); Intent intent = new Intent(this, ShowActivity.class); intent.putStringArrayListExtra("activityList", list); startActivity(intent); } else if (v == getIcon) { Drawable drawable = getApkIcon(apkPath); if (drawable != null) { iconImage.setBackground(drawable); } } } private void initWidget() { getPackageBtn = (Button) findViewById(R.id.main_btn_getpackagename); getVersionCodeBtn = (Button) findViewById(R.id.main_btn_getversioncode); getVersionNameBtn = (Button) findViewById(R.id.main_btn_getversionname); apkName = (EditText) findViewById(R.id.main_et_apkName); getPackageBtn.setOnClickListener(this); getVersionCodeBtn.setOnClickListener(this); getVersionNameBtn.setOnClickListener(this); packageNameText = (TextView) findViewById(R.id.main_tv_packagename); versionCodeText = (TextView) findViewById(R.id.main_tv_versioncode); versionNameText = (TextView) findViewById(R.id.main_tv_versionname); getActivitiesBtn = (Button) findViewById(R.id.main_btn_getactivities); getActivitiesBtn.setOnClickListener(this); iconImage = (ImageView) findViewById(R.id.main_iv); getIcon = (Button) findViewById(R.id.main_btn_geticon); getIcon.setOnClickListener(this); } /** * 獲取APK的包名 * * @param apkPath * @return */ private String getPackageName(String apkPath) { PackageInfo pi = pm.getPackageArchiveInfo(apkPath, PackageManager.GET_ACTIVITIES); String packageName = null; if (pi != null) { packageName = pi.packageName; } return packageName; } /** * 獲取APK版本名稱(versionName) * * @param apkPath * @return */ private String getVersionName(String apkPath) { PackageInfo pi = pm.getPackageArchiveInfo(apkPath, PackageManager.GET_ACTIVITIES); String versionName = null; if (pi != null) { versionName = pi.versionName; } return versionName; } /** * 獲取APK版本號(versionCode) * * @param apkPath * @return */ private int getVersionCode(String apkPath) { PackageInfo pi = pm.getPackageArchiveInfo(apkPath, PackageManager.GET_ACTIVITIES); int versionCode = 1; if (pi != null) { versionCode = pi.versionCode; } return versionCode; } /** * 獲取APK的所有activity的name * * @param apkPath * @return */ private ArrayList<String> getActivitiesName(String apkPath) { PackageInfo pi = pm.getPackageArchiveInfo(apkPath, PackageManager.GET_ACTIVITIES); ArrayList<String> list = null; if (pi != null) { list = new ArrayList<String>(); ActivityInfo[] ais = pi.activities; if (ais != null) { for (ActivityInfo ai : ais) { String name = ai.name; if (name != null && !"".equals(name)) { list.add(name); } } } } return list; } /** * 獲取應用程序圖片Drawable * * @param apkPath * @return */ private Drawable getApkIcon(String apkPath) { PackageInfo pi = pm.getPackageArchiveInfo(apkPath, PackageManager.GET_ACTIVITIES); if (pi != null) { ApplicationInfo appInfo = pi.applicationInfo; appInfo.sourceDir = apkPath; appInfo.publicSourceDir = apkPath; try { return appInfo.loadIcon(pm); } catch (OutOfMemoryError e) { e.printStackTrace(); } } return null; } } </pre><a style="text-indent:0px;" title="派生到我的代碼片" href="/misc/goto?guid=4959614224645940267" target="_blank"></a></div>
</div> </div>
ShowActivity:
package com.home.getapkinfo;import java.util.ArrayList; import android.app.Activity; import android.os.Bundle; import android.widget.ListView; public class ShowActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.show_activity); ListView listView = (ListView) findViewById(R.id.show_activity_lv); ArrayList<String> list = getIntent().getStringArrayListExtra( "activityList"); if (list != null) { listView.setAdapter(new MyAdapter(this, list)); } } } </pre><a style="text-indent:0px;" title="派生到我的代碼片" href="/misc/goto?guid=4959614224645940267" target="_blank"></a></div>
</div> </div>
MyAdapter:
package com.home.getapkinfo;import java.util.ArrayList; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; public class MyAdapter extends BaseAdapter { private ArrayList<String> list; private Context context; public MyAdapter(Context context, ArrayList<String> list) { this.context = context; this.list = list; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int arg0) { return list.get(arg0); } @Override public long getItemId(int arg0) { return arg0; } @Override public View getView(int positon, View arg1, ViewGroup arg2) { TextView tv = new TextView(context); tv.setText(list.get(positon)); tv.setTextSize(18); return tv; } } </pre><a style="text-indent:0px;" title="派生到我的代碼片" href="/misc/goto?guid=4959614224645940267" target="_blank"></a></div>
</div> </div>
main.xml:
<LinearLayout xmlns:android=" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" ><EditText android:id="@+id/main_et_apkName" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="輸入應用名稱即可,默認路徑是SD卡根目錄,默認后綴是.apk" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/main_btn_getpackagename" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="包名" /> <Button android:id="@+id/main_btn_getversionname" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="版本名" /> <Button android:id="@+id/main_btn_getversioncode" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="版本號" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/main_btn_geticon" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="APK圖標" /> <Button android:id="@+id/main_btn_getactivities" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="activity name" /> </LinearLayout> <TextView android:id="@+id/main_tv_packagename" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="這里顯示版本名" android:textSize="18sp" /> <TextView android:id="@+id/main_tv_versionname" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="這里顯示包名" android:textSize="18sp" /> <TextView android:id="@+id/main_tv_versioncode" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="這里顯示版本號" android:textSize="18sp" /> <ImageView android:id="@+id/main_iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal"/> </LinearLayout> </pre><a style="text-indent:0px;" title="派生到我的代碼片" href="/misc/goto?guid=4959614224645940267" target="_blank"></a></div>
</div> </div>
show_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" ><ListView android:id="@+id/show_activity_lv" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> </pre><a style="text-indent:0px;" title="派生到我的代碼片" href="/misc/goto?guid=4959614224645940267" target="_blank"></a></div>
</div> </div>
權限:
</div> </div> 來自:http://blog.csdn.net/u010142437/article/details/19568483<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!相關經驗
相關文檔
目錄
sesese色