• android 程序打开第三方程序


    因为在开发过程中需要开启扫描第三方程序,并且点击启动的效果,所以对这个功能进行了实现,并且分享出来个大家。


    之前看到网上说需要获取包名和类名,然后通过  intent 才能打开这个程序,其实不必要这样。


    在android API中,有这么一个接口,

    Intent android.content.pm. PackageManager.getLaunchIntentForPackage( String packageName)

    实际说的就是传入包名即可返回Intent,继而startActivity 即可打开第三方程序了。


    以下是Demo:


    界面 GetApplicationActivity.


    public class GetApplicationActivity extends Activity implements
    		OnItemClickListener {
    
    	private GetApplicationAdpater mGetApplicationAdpater;
    	private ArrayList<ApplicationData> applicationDatas;
    	private ArrayList<ApplicationData> tempApplicationDatas;
    	private ListView mListView;
    	private PackageManager mPackageManager;
    	private static final int Refresh_List_View = 0x001;
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		mListView = (ListView) findViewById(R.id.application_list);
    		initData();
    	}
    	private void initData() {
    		applicationDatas = new ArrayList<ApplicationData>();
    		tempApplicationDatas = new ArrayList<ApplicationData>();
    		mGetApplicationAdpater = new GetApplicationAdpater(this,
    				applicationDatas);
    		mListView.setAdapter(mGetApplicationAdpater);
    		mListView.setOnItemClickListener(this);
    		getData();
    		mPackageManager = getPackageManager();
    	}
    
    	private void getData() {
    		GetApplicationAsyncTask mGetApplicationAsyncTask = new GetApplicationAsyncTask();
    		mGetApplicationAsyncTask.getApplicationList(this,
    				new GetApplicationList() {
    					@Override
    					public void dataCallback(
    							ArrayList<ApplicationData> applicationDatas) {
    						if (applicationDatas != null) {
    							tempApplicationDatas = applicationDatas;
    						}
    						mHandler.sendEmptyMessage(Refresh_List_View);
    					}
    
    				});
    	}
    
    	private Handler mHandler = new Handler() {
    		public void handleMessage(Message msg) {
    			switch (msg.what) {
    			case Refresh_List_View:
    				int count = tempApplicationDatas.size();
    				applicationDatas.clear();
    				for (int i = 0; i < count; i++) {
    					applicationDatas.add(tempApplicationDatas.get(i));
    				}
    				mGetApplicationAdpater.notifyDataSetChanged();
    				break;
    
    			default:
    				break;
    			}
    		};
    	};
    
    	@Override
    	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
    		// TODO Auto-generated method stub
    		String packageName = applicationDatas.get(position).getPackageName();
    		Intent i = mPackageManager.getLaunchIntentForPackage(packageName);
    		if (i != null)
    			startActivity(i);
    	}
    }
    


    java


    适配器 GetApplicationAdpater.java


    public class GetApplicationAdpater extends BaseAdapter{
    	
    	private ArrayList<ApplicationData> applicationDatas;
    	private LayoutInflater mLayoutInflater;
    	
    	public GetApplicationAdpater(Context context,ArrayList<ApplicationData> applicationDatas) {
    		super();
    		this.applicationDatas = applicationDatas;
    		mLayoutInflater = LayoutInflater.from(context);
    	}
    
    	@Override
    	public int getCount() {
    		// TODO Auto-generated method stub
    		return applicationDatas.size();
    	}
    
    	@Override
    	public Object getItem(int position) {
    		// TODO Auto-generated method stub
    		return applicationDatas.get(position);
    	}
    
    	@Override
    	public long getItemId(int position) {
    		// TODO Auto-generated method stub
    		return position;
    	}
    
    	@Override
    	public View getView(int position, View convertView, ViewGroup parent) {
    		ViewHolder mViewHolder;
    		ApplicationData mApplicationData = applicationDatas
    				.get(position);
    
    		if (convertView == null) {
    			mViewHolder = new ViewHolder();
    			convertView = mLayoutInflater
    					.inflate(R.layout.list_item_view, null);
    			mViewHolder.image = (ImageView) convertView
    					.findViewById(R.id.image);
    			mViewHolder.title = (TextView) convertView.findViewById(R.id.title);
    			convertView.setTag(mViewHolder);
    		} else {
    			mViewHolder = (ViewHolder) convertView.getTag();
    		}
    		mViewHolder.image.setImageDrawable(mApplicationData.getiCon());
    		mViewHolder.title.setText(mApplicationData.getAppName());
    		return convertView;
    	}
    
    	public final class ViewHolder {
    		public ImageView image;
    		public TextView title;
    	}
    
    }
    


    获取程序包名类 GetApplicationAsyncTask.java 


    public class GetApplicationAsyncTask extends
    		AsyncTask<Integer, Integer, String> {
    	private GetApplicationList mGetApplicationList;
    	private ArrayList<ApplicationData> applicationDatas;
    	private Context mContext;
    
    	@Override
    	protected String doInBackground(Integer... params) {
    		PackageManager mPackageManager = mContext.getPackageManager();
    		List<PackageInfo> packageInfos = mPackageManager
    				.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);  获取程序列表
    		applicationDatas = new ArrayList<ApplicationData>();
    		for (PackageInfo packs : packageInfos) {
    			if((packs.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM)==0)  //主要过滤系统的程序
    			{
    				ApplicationData mApplicationData= new ApplicationData();
    				mApplicationData.setiCon(packs.applicationInfo.loadIcon(mPackageManager));
    				mApplicationData.setAppName(packs.applicationInfo.loadLabel(mPackageManager).toString());
    				mApplicationData.setPackageName(packs.packageName);
    				applicationDatas.add(mApplicationData);
    			}
    		}
    		return null;
    	}
    
    	@Override
    	protected void onPostExecute(String result) {
    		// TODO Auto-generated method stub
    		mGetApplicationList.dataCallback(applicationDatas);
    	}
    
    	public void getApplicationList(Context context,
    			GetApplicationList getApplicationList) {
    		this.mGetApplicationList = getApplicationList;
    		this.mContext = context;
    		this.execute();
    	}
    
    	public interface GetApplicationList {
    		public void dataCallback(ArrayList<ApplicationData> applicationDatas);
    	}
    }
    


    代码比较简单,我就不做解释了。


  • 相关阅读:
    AtCoder Beginner Contest 250
    Codeforces Round #786 (Div. 3)
    Codeforces Round #787 (Div. 3)
    Educational Codeforces Round 128 (Rated for Div. 2)
    OpenSSL测试大数
    大数库GMP测试
    实验一密码引擎商用密码算法实现2交叉测试(选做)
    数据转换16进制字符
    NOI 2021 题目选做
    NOI 2017 题目选做
  • 原文地址:https://www.cnblogs.com/aukle/p/3235555.html
Copyright © 2020-2023  润新知