openOtherAPP

转载地址:http://hunankeda110.iteye.com/blog/1960444
* 启动另外一个程序*

第一种方式知道包名和类名:

 Intent intent = new Intent();
 intent.setAction(Intent.ACTION_MAIN);
 intent.addCategory(Intent.CATEGORY_LAUNCHER);
    設置包名兩種方式
    1、 PackageManager pm=this.getPackageManager();
     intent= pm.getLaunchIntentForPackage("pad.android.marketing.gps");
     intent.setFlags              (Intent.FLAG_ACTIVITY_NEW_TASKIntent.FLAG_ACTIVITY_RESET_TASK_IF_NEED     ED);
    2、ComponentName cm = new ComponentName("pad.android.marketing.gps", "pad.android.marketing.gps.MainActivity");
      intent.setComponent(cm);     
 startActivity(intent);


第二种启动不知道类名的,包名需要从设备查找。
    Intent intent1 = new Intent();
    intent1.setAction(Intent.ACTION_MAIN);
    intent1.addCategory(Intent.CATEGORY_LAUNCHER);
    //包管理器
    PackageManager pm = context.getPackageManager();
    //已经安装的app信息集合
    List<ApplicationInfo> appList = pm.getInstalledApplications(0);
    for (int i = 0; i < appList.size(); i++) {
        Log.e("TAG", "appinfo: " + appList.get(i));
    }
    //包信息实例化
    PackageInfo packageInfo = null;
    try {
        //指定包的信息
        packageInfo = pm.getPackageInfo("pad.android.marketing.gps", 0);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    //指定app进入界面activity。
    Intent intent = new Intent(Intent.ACTION_MAIN, null);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    intent.setPackage(packageInfo.packageName);
    //查找指定包中activity信息
    List<ResolveInfo> infoList = pm.queryIntentActivities(intent, 0);
    ResolveInfo ri = infoList.iterator().next();
    if (ri != null) {
        //启动activity的名字
        String classname = ri.activityInfo.name;
        Log.e("TAG", "class " +classname );
    }
    ComponentName cn = new ComponentName("pad.android.marketing.gps", className);
    intent1.setComponent(cn);
    startActivity(intent1);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值