2 kohant kohant 于 2014.09.26 16:50 提问

Android如何根据包名加载指定应用

楼主想实现一个加载指定应用到gridview上,从网上参考了别人的代码然后改来改去都没能成功,楼主是初学者,java和android都比较薄弱,求各大侠能指点一下loadapps那一段代码要怎么写( ⊙o⊙ )

public class Appinfo extends Activity {
private List mApps;
GridView mGrid;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    loadApps();
    setContentView(R.layout.appinfo);
    mGrid = (GridView) findViewById(R.id.myListView1);
    mGrid.setAdapter(new AppsAdapter());

    mGrid.setOnItemClickListener(listener);
}

private OnItemClickListener listener= new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        // TODO Auto-generated method stub
        ResolveInfo info = mApps.get(position);         
    String pkg = info.activityInfo.packageName;         
           Uri uri = Uri.fromParts("package", pkg, null);
        Intent it = new Intent(Intent.ACTION_DELETE, uri);
           startActivity(it);

    }

};

private void loadApps() {
    List<ResolveInfo> mAppstr;

    Intent mainIntent = new Intent(Intent.ACTION_MAIN,null);
    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);   
   mAppstr = getPackageManager().queryIntentActivities(mainIntent, 0);

    if (!mAppstr.isEmpty()) {
        List<Intent> targetedShareIntents = new ArrayList<Intent>();
        for (ResolveInfo info : mAppstr) {
            Intent targeted = new Intent(Intent.ACTION_MAIN,null);

            ActivityInfo activityInfo = info.activityInfo;

            // judgments : activityInfo.packageName, activityInfo.name, etc.
            if (activityInfo.packageName.contains("com.xx") ||activityInfo.name.contains("com.xx")) {
                 targeted.setPackage(activityInfo.packageName);
                targetedShareIntents.add(targeted);
            }
           mApps = getPackageManager().queryIntentActivities(targetedShareIntents, 0);

        }}
}   

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public class AppsAdapter extends BaseAdapter{

    @Override
    public final int getCount() {
        // TODO Auto-generated method stub
        return mApps.size();    
    }

    @Override
    public final Object getItem(int position) {
        // TODO Auto-generated method stub
        return mApps.get(position);
    }

    @Override
    public final long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) { 
        // TODO Auto-generated method stub
        ImageView i;
        if(convertView == null){
            i = new ImageView(Appinfo.this);
            i.setScaleType(ImageView.ScaleType.FIT_CENTER);
            i.setLayoutParams(new GridView.LayoutParams(100,100));
        }
        else {
            i =(ImageView) convertView;
        }
        ResolveInfo info = mApps.get(position);
        i.setImageDrawable(info.activityInfo.loadIcon(getPackageManager()));

        return i;
    }


}

}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!