How to call android calculator on my app for all phones

前端 未结 4 1635
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-03 02:17
  public static final String CALCULATOR_PACKAGE =\"com.android.calculator2\";
  public static final String CALCULATOR_CLASS =\"com.android.calculator2.Calculator\";
         


        
4条回答
  •  囚心锁ツ
    2020-12-03 02:37

    you can try as to Default calculator on all android devices:

    ArrayList> items =new ArrayList>();
    
    final PackageManager pm = getPackageManager();
    List packs = pm.getInstalledPackages(0);  
    for (PackageInfo pi : packs) {
    if( pi.packageName.toString().toLowerCase().contains("calcul")){
        HashMap map = new HashMap();
        map.put("appName", pi.applicationInfo.loadLabel(pm));
        map.put("packageName", pi.packageName);
        items.add(map);
     }
    }
    

    and now you can launch calculator application as:

    if(items.size()>=1){
    String packageName = (String) items.get(0).get("packageName");
    Intent i = pm.getLaunchIntentForPackage(packageName);
    if (i != null)
      startActivity(i);
    } 
    else{
          // Application not found
       }
    

    And for Api >= 15 ,You can use

    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_APP_CALCULATOR);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
    

提交回复
热议问题