美文网首页
android 怎么启动系统自带计算器

android 怎么启动系统自带计算器

作者: 全球顶尖伪极客 | 来源:发表于2019-05-27 00:49 被阅读0次

https://www.jianshu.com/p/4d9f4011b398

 public static void openCalculatorApp(Activity mContext) {
       try {
           ComponentName componetName = new ComponentName(
                   //这个是另外一个应用程序的包名
                   "com.android.calculator2",
                   //这个参数是要启动的Activity
                   "com.android.calculator2.Calculator");
           Intent intent = new Intent();
           Bundle bundle = new Bundle();
           intent.putExtras(bundle);
           intent.setComponent(componetName);
           mContext.startActivity(intent);
       } catch (ActivityNotFoundException e) {
           openJSQ(mContext);
       } catch (Exception e) {
           Toast.makeText(mContext, "程序异常:" + e.getMessage(), Toast.LENGTH_LONG).show();
       }
   }

   public static void openPhoneApp(Activity mContext) {
       try {
           //系统 联系人 界面
           ComponentName componetName = new ComponentName(
                   //这个是另外一个应用程序的包名
                   "com.android.contacts",
                   //这个参数是要启动的Activity
                   "com.android.contacts.activities.PeopleActivity");
           Intent intent = new Intent();
           Bundle bundle = new Bundle();
           intent.putExtras(bundle);
           intent.setComponent(componetName);
           mContext.startActivity(intent);

       } catch (ActivityNotFoundException e) {
           Toast.makeText(mContext, "未找到通讯录", Toast.LENGTH_LONG).show();
       } catch (Exception e) {
           Toast.makeText(mContext, "程序异常:" + e.getMessage(), Toast.LENGTH_LONG).show();
       }
   }
   public static void openJSQ(Activity mContext){
       try {
           PackageInfo pak = getAllApps(mContext, "Calculator","calculator"); //大小写
           if(pak != null){
               Intent intent = new Intent();
               intent = mContext.getPackageManager().getLaunchIntentForPackage(pak.packageName);
               mContext.startActivity(intent);
           }else{
               Toast.makeText(mContext, "未找到计算器", Toast.LENGTH_SHORT).show();
           }
       } catch (Exception e) {
           e.printStackTrace();
           Toast.makeText(mContext, "程序异常:" + e.getMessage(), Toast.LENGTH_LONG).show();
       }
   }
   public static PackageInfo getAllApps(Context context, String app_flag_1, String app_flag_2) {
       try {
           PackageManager pManager = context.getPackageManager();
           // 获取手机内所有应用
           List<PackageInfo> packlist = pManager.getInstalledPackages(0);
           for (int i = 0; i < packlist.size(); i++) {
               PackageInfo pak = (PackageInfo) packlist.get(i);
               if(pak.packageName.contains(app_flag_1)||pak.packageName.contains(app_flag_2)){
                   return pak;
               }
           }
       } catch (Exception e) {
           e.printStackTrace();
       }
       return null;
   }

相关文章

网友评论

      本文标题:android 怎么启动系统自带计算器

      本文链接:https://www.haomeiwen.com/subject/bjiqtctx.html