在android中获取安装应用程序的信息

时间:2011-06-01 09:18:52

标签: android

  

你好专家,我需要构建应用程序,分享移动应用程序到第二个移动设备,以便   我需要知道如何获取所有应用信息,如:姓名,日期,图标等   包com.AppInfo;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;

public class AppInfo extends Activity {
    /** Called when the activity is first created. */
     private ListView lView;
     private ArrayList results = new ArrayList();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);



        }}
class PInfo {
    private String appname = "";
    private String pname = "";
    private String versionName = "";
    private int versionCode = 0;
    private Drawable icon;
    private void prettyPrint() {
       // Log.v(appname + "\t" + pname + "\t" + versionName + "\t" + versionCode);

    }


private ArrayList<PInfo> getPackages() {
    ArrayList<PInfo> apps = getInstalledApps(false); /* false = no system packages */
    final int max = apps.size();
    for (int i=0; i<max; i++) {
        apps.get(i).prettyPrint();
    }
    return apps;
}

private ArrayList<PInfo> getInstalledApps(boolean getSysPackages) {
    ArrayList<PInfo> res = new ArrayList<PInfo>();        
    List<PackageInfo> packs = getPackageManager().getInstalledPackages(0);
    for(int i=0;i<packs.size();i++) {
        PackageInfo p = packs.get(i);
        if ((!getSysPackages) && (p.versionName == null)) {
            continue ;
        }
        PInfo newInfo = new PInfo();
        newInfo.appname = p.applicationInfo.loadLabel(getPackageManager()).toString();
        newInfo.pname = p.packageName;
        newInfo.versionName = p.versionName;
        newInfo.versionCode = p.versionCode;
        newInfo.icon = p.applicationInfo.loadIcon(getPackageManager());
        res.add(newInfo);
    }
    return res; 
}

4 个答案:

答案 0 :(得分:2)

答案 1 :(得分:1)

虽然可以使用getInstalledPackages()并使用PackageInfo

检索您想要的信息

但是,由于您想要将应用程序从一部手机分享到另一部手机,我不认为它至少可能,除非设备已植根

答案 2 :(得分:0)

C2MD非常适合发送少量数据,而且相当容易实现。如果您不想这样做,只需将短信发送到另一部手机,并在第二部手机上安装接收器。

答案 3 :(得分:0)

您可以使用PackageManager检索所需信息,ApplicationInfoPackageInfo都可以使用。

private static final String TAG = "MyActivity";  
...

final PackageManager pm = getPackageManager();
final List<ApplicationInfo> installedApps = pm.getInstalledApplications(PackageManager.GET_META_DATA);

for ( ApplicationInfo app : installedApps ) {
    Log.d(TAG, "Package: " + app.packageName);
    Log.d(TAG, "Directory: " + app.sourceDir);
    Log.d(TAG, "Icon: " + app.icon);

    try {
        PackageInfo packageInfo = pm.getPackageInfo(app.packageName, PackageManager.GET_PERMISSIONS);

        Date installTime = new Date( packageInfo.firstInstallTime );
        Date updateTime = new Date( packageInfo.lastUpdateTime );

        Log.d(TAG, "Installed: " + installTime.toString());
        Log.d(TAG, "Updated: " + updateTime.toString());
        Log.d(TAG, "Version: " + packageInfo.versionCode);
        Log.d(TAG, "Name: " + packageInfo.versionName);
    }
    catch ( PackageManager.NameNotFoundException e ) {
        e.printStackTrace();
    }
}