android 获取第三方应用程序包名并启动



说明:

    第一行,应用名,icon

   第二行,启动类名

  第三行, 应用包名

  点击启动


代码下载地址:

             Demo下载

              



package com.rmt.getappinfo;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity implements Runnable ,OnItemClickListener{
private static final String TAG = "appinfo";
private ListView softlist;
private ProgressDialog pd;
private Context mContext;
private PackageManager mPackageManager;
private List<ResolveInfo> mAllApps;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("文件管理器");
mContext = this;
mPackageManager = getPackageManager();
softlist = (ListView) findViewById(R.id.softlist);
pd = ProgressDialog.show(this, "请稍候..", "正在收集软件信息...", true,false);
Thread thread = new Thread(this);
thread.start();
}

/**
* 检查系统应用程序,添加到应用列表中
*/
private void bindMsg(){
//应用过滤条件
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
mAllApps = mPackageManager.queryIntentActivities(mainIntent, 0);
softlist.setAdapter(new MyAdapter(mContext, mAllApps));
softlist.setOnItemClickListener(this);
//按报名排序
Collections.sort(mAllApps, new ResolveInfo.DisplayNameComparator(mPackageManager));
}

@Override
public void run() {
bindMsg();
handler.sendEmptyMessage(0);

}
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
pd.dismiss();
}
};

class MyAdapter extends BaseAdapter{
private List<ResolveInfo> resInfo;
private ResolveInfo res;
private LayoutInflater infater=null;

public MyAdapter(Context context, List<ResolveInfo> resInfo) {
this.resInfo = resInfo;
infater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
return resInfo.size();
}

@Override
public Object getItem(int arg0) {
return arg0;
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null || convertView.getTag() == null) {
convertView = infater.inflate(R.layout.soft_row, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}
else{
holder = (ViewHolder) convertView.getTag() ;
}


//获取应用程序包名,程序名称,程序图标
res = resInfo.get(position);
holder.appIcon.setImageDrawable(res.loadIcon(mPackageManager));
holder.tvAppLabel.setText(res.loadLabel(mPackageManager).toString());
holder.tvPkgName.setText(res.activityInfo.packageName+'\n'+res.activityInfo.name);
Log.d(TAG, res.loadLabel(mPackageManager).toString() + "\n" + res.activityInfo.packageName+'\n'+res.activityInfo.name);
return convertView;
}
}

//设定界面布局
class ViewHolder {
ImageView appIcon;
TextView tvAppLabel;
TextView tvPkgName;

public ViewHolder(View view) {
this.appIcon = (ImageView) view.findViewById(R.id.img);
this.tvAppLabel = (TextView) view.findViewById(R.id.name);
this.tvPkgName = (TextView) view.findViewById(R.id.desc);
}
}
/**
* 单击应用程序后进入系统应用管理界面
*/
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
ResolveInfo res = mAllApps.get(position);
//该应用的包名和主Activity
String pkg = res.activityInfo.packageName;
String cls = res.activityInfo.name;
ComponentName componet = new ComponentName(pkg, cls);
Intent i = new Intent();
i.setComponent(componet);
startActivity(i);

}

}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/softlist"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:drawSelectorOnTop="false" />

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/vw1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/img"
        android:layout_width="32dip"
        android:layout_height="32dip"
        android:layout_margin="4dip" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/name"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/desc"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="10dip"
            android:textSize="14sp" />
    </LinearLayout>



</LinearLayout>



  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值