Android检查手机上是否安装了第三方软件的方法------本文以百度地图为例
转于:http://blog.csdn.net/ljb_blog/article/details/11047655
---------------------------------------------------------------------------------------
- package com.example.myapi.thirdbaidumap;
- import java.net.URISyntaxException;
- import java.util.ArrayList;
- import java.util.List;
- import android.app.Activity;
- import android.content.Context;
- import android.content.Intent;
- import android.content.pm.PackageInfo;
- import android.content.pm.PackageManager;
- import android.net.Uri;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import com.example.myapi.R;
- /**
- * 测试程序跳转到第三方地图,并将数据传递过去。
- * @author tony
- *
- */
- public class ThirdBaiduMapTestActivity extends Activity implements OnClickListener{
- private Button btn_test;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.thirdbaidumaptest);
- btn_test = (Button)findViewById(R.id.baidu_btn_test);
- btn_test.setOnClickListener(this);
- }
- @Override
- public void onClick(View v) {
- switch(v.getId()){
- case R.id.baidu_btn_test:
- //如果已安装,
- if(isAvilible(this,"com.baidu.BaiduMap")){//传入指定应用包名
- Intent intent = null;
- try {
- // intent = Intent.getIntent("intent://map/direction?origin=latlng:34.264642646862,108.95108518068|name:我家&destination=大雁塔&mode=driving®ion=西安&src=yourCompanyName|yourAppName#Intent;scheme=bdapp;package=com.baidu.BaiduMap;end");
- intent = Intent.getIntent("intent://map/direction?" +
- "origin=大柏树&" +
- "destination=江湾镇" +
- "&mode=driving&" +
- "region=上海市" +
- "&src=yourCompanyName|yourAppName#Intent;scheme=bdapp;package=com.baidu.BaiduMap;end");
- startActivity(intent); //启动调用
- } catch (URISyntaxException e) {
- Log.e("intent", e.getMessage());
- }
- }else{//未安装
- //market为路径,id为包名
- //显示手机上所有的market商店
- Uri uri = Uri.parse("market://details?id=com.baidu.BaiduMap");
- Intent intent = new Intent(Intent.ACTION_VIEW, uri);
- startActivity(intent);
- }
- break;
- }
- }
- /**
- * 检查手机上是否安装了指定的软件
- * @param context
- * @param packageName:应用包名
- * @return
- */
- private boolean isAvilible(Context context, String packageName){
- //获取packagemanager
- final PackageManager packageManager = context.getPackageManager();
- //获取所有已安装程序的包信息
- List<PackageInfo> packageInfos = packageManager.getInstalledPackages(0);
- //用于存储所有已安装程序的包名
- List<String> packageNames = new ArrayList<String>();
- //从pinfo中将包名字逐一取出,压入pName list中
- if(packageInfos != null){
- for(int i = 0; i < packageInfos.size(); i++){
- String packName = packageInfos.get(i).packageName;
- packageNames.add(packName);
- }
- }
- //判断packageNames中是否有目标程序的包名,有TRUE,没有FALSE
- return packageNames.contains(packageName);
- }
- }
相关推荐
jtguve 2020-01-24
zmosquito 2019-12-12
etzt 2019-11-17
magvwiz 2011-02-22
林小木 2016-01-17
段雷 2014-12-24
chenlaoyang 2016-05-07
nutian 2012-05-13
worldkun 2015-02-10
lovecloudpan 2015-01-15
tingpurg 2014-01-05
jiejiao0 2013-10-08
QcloudYingJoy 2016-12-04
XSxing 2016-07-11
shilongdred 2019-04-23
羽吉公 2012-05-28
sasac 2015-03-25
深圳湾 2018-04-23