How to call android calculator on my app for all phones

前端 未结 4 1606
佛祖请我去吃肉
佛祖请我去吃肉 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:32

    You'll have to check if the default calculator package name(com.android.calculator2) is available with this:

    try{
         ApplicationInfo info = getPackageManager()
                                 .getApplicationInfo("com.android.calculator2", 0 );
    
        } catch( PackageManager.NameNotFoundException e ){
         //application doesn't exist
    }
    

    Samsung uses a lot of apps preinstalled with TouchWiz which aren't available in stock Android. Therefore if you get an Exception in the above you can check if Samsung's calculator is available. Btw I think com.android.calculator2 is not HTC specific.

    0 讨论(0)
  • 2020-12-03 02:35

    Well this is a Modified Answer of @ρяσѕρєя K becouse it works fine in samsung mobiles and only packages have "calc" but not all mobiles like HTC AND LENOVO ETC

    And for Api >= 15 ,You can use BUT !!!

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

    THIS MAY CAN CAUSE ERROR LIKE THIS

    android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.MAIN cat=[android.intent.category.APP_CALCULATOR] flg=0x10000000 }

    SO LETS TAKE A LOOK AT THIS

    LOAD all apps to Array

        // Declare universal if you want Access any where from scope
    
    
    ArrayList<HashMap<String,Object>> items;
        PackageManager pm ;    
    List<PackageInfo> packs;
    
        // initialise From Oncreate if you want
        items =new  ArrayList<HashMap<String,Object>>(); 
        pm = getPackageManager();
        packs = pm.getInstalledPackages(0);  
            for (PackageInfo pi : packs)
     {
                HashMap<String, Object> map = new HashMap<String, Object>();
                map.put("appName", pi.applicationInfo.loadLabel(pm));
                map.put("packageName", pi.packageName);
                items.add(map); 
     }
    

    THIS IS TRICK PART We are Traversing through all Apps to get App Named or Matches "Calculator"

    public void opencalculator(){
      int d=0;
      if(items.size()>=1){
      int j=0;
     for(j=0;j<items.size();j++){
     String AppName = (String) items.get(j).get("appName");
    // Log.w("Name",""+AppName);
        if(AppName.matches("Calculator"))
                         {
                                d=j;
                                break;
                         }
                        }
                        String packageName = (String) items.get(d).get("packageName");
    
                        Intent i = pm.getLaunchIntentForPackage(packageName);
                        if (i != null){
                            Toast.makeText(getContext(),"STARTING",Toast.LENGTH_SHORT).show();
    
                            startActivity(i);}
                        else {
                            Toast.makeText(getContext(),"SORRY I CANT OPEN CALCULATOR :(",Toast.LENGTH_SHORT).show();
    
                        }
                    }
    
    
                    else{
                        Toast.makeText(getContext(),"SORRY I CANT START CALCULATOR :(",Toast.LENGTH_SHORT).show();
    
    
                    }
    }
    

    CALL OPENCALCULATOR

    opencalculator();
    
    0 讨论(0)
  • 2020-12-03 02:37

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

    ArrayList<HashMap<String,Object>> items =new ArrayList<HashMap<String,Object>>();
    
    final PackageManager pm = getPackageManager();
    List<PackageInfo> packs = pm.getInstalledPackages(0);  
    for (PackageInfo pi : packs) {
    if( pi.packageName.toString().toLowerCase().contains("calcul")){
        HashMap<String, Object> map = new HashMap<String, Object>();
        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);
    
    0 讨论(0)
  • 2020-12-03 02:47
    Intent i = new Intent();
    i.setAction(Intent.ACTION_MAIN);
    i.addCategory(Intent.CATEGORY_APP_CALCULATOR);
    startActivity(i);
    
    0 讨论(0)
提交回复
热议问题