分享到:
点击次数:333  更新时间:2016-10-11 16:24:04  【打印此页】  【关闭
澳门新濠天地娱乐场澳门新濠天地娱乐定制
当前位置:主页 > 新闻动态 > 技术交流 > 获取未安装APK的一些基本信息的代码

获取未安装APK的一些基本信息的代码

责任编辑:admin 发布时间:2016-11-10 11:10 浏览次数:

即使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; 
        } 
     
    } 
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)); 
            } 
        } 
    } 
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; 
        } 
     
    } 
main.xml:
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/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> 
show_activity.xml:
    <?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/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> 
权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />