安卓应用启动其他应用的几种方式

来源:互联网 发布:免费男女啪啪软件 编辑:程序博客网 时间:2024/05/22 04:34

1,知道要启动的包名以及要启动的Activity:

Intent intent = new Intent();

                               /**下面方法的参数分别是指要启动应用的包名及对应的Activity*/
ComponentName cpn= new ComponentName("com.ldm.demo","com.ldm.demo.TextActivity");
intent .setComponent(cpn);
                               startActivity(intent );

2,只知道应用的包名:

                              PackageManager packageManager = getPackageManager();
                              Intent intent=new Intent();
                              intent =packageManager.getLaunchIntentForPackage("packageName");/**packageName指的就要启动应用的包名*/
                              if(intent==null){
                                         System.out.println("APP not found!");
                             }
                                        startActivity(intent);
                                }
                     }

3,知道要启动的包名以及要启动的Activity应用启动并传递数据:

  1.                           ComponentName componentName = new ComponentName(  "com.ldm.demo",    "com.ldm.demo.TestActivity");  
  2.                            Intent intent = new Intent();  
  3.                            Bundle bundle = new Bundle();  
  4.                            bundle.putString("data", data);  
  5.                            intent.putExtras(bundle);  
  6.                            intent.setComponent(componentName);  
  7.                            startActivity(intent);  
  8. 4,未知包名的情况下: 
  9.     /**获得手机内应用的包名,返回一个List集合**/   
           public List<PackageInfo> getAllApps() {     
                List<PackageInfo> apps = new ArrayList<PackageInfo>();     
                PackageManager packageManager = this.getPackageManager();     
                /**获取手机内所有应用 */    
                List<PackageInfo> paklist = pManager.getInstalledPackages(0);     
                for (int i = 0; i < paklist.size(); i++) {     
                    PackageInfo pak = (PackageInfo) paklist.get(i);     
                    /**判断是否为非系统预装的应用  (大于0为系统预装应用,小于等于0为非系统应用) */  
                    if ((pak.applicationInfo.flags & pak.applicationInfo.FLAG_SYSTEM) <= 0) {     
                        apps.add(pak);     
                    }     
                }     
                return apps;     
        }   
     获得包名后,就可以通过获得要启动的包名启动应用了: 
        public void launchApp() {   
                PackageManager packageManager = this.getPackageManager();   
                List<PackageInfo> packages = getAllApps();   
                PackageInfo pa = null;   
                for(int i=0;i<packages.size();i++){   
                    pa = packages.get(i);   
                    /**获得应用名*/   
                    String appLabel = packageManager.getApplicationLabel(pa.applicationInfo).toString();   
                    /**获得包名  */ 
                    String appPackage = pa.packageName;   
                    Log.d(""+i, appLabel+"  "+appPackage);   
                }   
                Intent intent = packageManager.getLaunchIntentForPackage("jp.co.johospace.jorte");
                startActivity(intent);   
            }  
原创粉丝点击