How to call android calculator on my app for all phones

前端 未结 4 1615
佛祖请我去吃肉
佛祖请我去吃肉 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: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> items;
        PackageManager pm ;    
    List packs;
    
        // initialise From Oncreate if you want
        items =new  ArrayList>(); 
        pm = getPackageManager();
        packs = pm.getInstalledPackages(0);  
            for (PackageInfo pi : packs)
     {
                HashMap map = new HashMap();
                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

    CALL OPENCALCULATOR

    opencalculator();
    

提交回复
热议问题