zoukankan      html  css  js  c++  java
  • 从应用中启动另外的应用

    项目中有时会需要启动另外的程序来协助实现一些功能,如系统打电话会调用电话程序,打开图片会打开图片程序等。

    让我们来看看用代码是如何实现这个功能的。

     /**
             * 启动应用
             * @param context
             * @param packageName 包名
             * @return
             */
            public static boolean startPackage(Context context, String packageName) {
                    final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
                    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    
                    final List<ResolveInfo> apps = context.getPackageManager()
                                    .queryIntentActivities(mainIntent, 0);
                    // final List<ResolveInfo> matches = new ArrayList<ResolveInfo>();
    
                    if (apps != null) {
                            // Find all activities that match the packageName
                            int count = apps.size();
                            for (int i = 0; i < count; i++) {
                                    final ResolveInfo resolveInfo = apps.get(i);
                                    final ActivityInfo activityInfo = resolveInfo.activityInfo;
                                    if (packageName.equals(activityInfo.packageName)) {
                                            // matches.add(resolveInfo);
                                            String className = activityInfo.name;
                                            AspLog.i(TAG, "startPackage: " + packageName + " & "
                                                            + className);
                                            ComponentName cn = new ComponentName(packageName, className);
                                            final Intent it = new Intent(Intent.ACTION_MAIN);
                                            it.addCategory(Intent.CATEGORY_LAUNCHER);
                                            it.setComponent(cn);
                                            it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                            context.startActivity(it);
                                            return true;
                                    }
                            }
                    }
    
                    // MMToast mToast = new MMToast(activity, MMToast.LENGTH_SHORT);
                    // mToast.setView(R.layout.login_message_panel);
                    // mToast.setTipImage(R.drawable.login_tip_failure);
                    // mToast.setText(R.string.open_app_error);
                    // mToast.show();
                    return false;
            }

    更多的移动互联网的发展趋势拓者设计吧效果图移动互联网应用相关的资料请到互联网的一点事www.yidin.net 留言

    欢迎各位同学加入 android 技术二群 222392467 

  • 相关阅读:
    [杂题]FZU2190 非提的救赎
    [模拟]ZOJ3480 Duck Typing
    [模拟]ZOJ3485 Identification Number
    [数论]ZOJ3593 One Person Game
    [博弈]ZOJ3591 Nim
    [杂题]URAL1822. Hugo II's War
    二分图相关
    KMP算法详解
    中国国家集训队论文集目录(1999-2009)
    最短路Dijkstra算法的一些扩展问题
  • 原文地址:https://www.cnblogs.com/ondream/p/3023439.html
Copyright © 2011-2022 走看看