How to call Android calculator from another app for all phones

0
454
How to call Android calculator from another app for all phones

How to call Android calculator from another app for all phones

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();

 

Also, you can make a public method to call the calculator in your app like this.

public void openMyCalculator(Context context){

    // 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 = context.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);
    }

    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(context,"STARTING",Toast.LENGTH_SHORT).show();

            context.startActivity(i);}
        else {
            Toast.makeText(context,"SORRY I CANT OPEN CALCULATOR :(",Toast.LENGTH_SHORT).show();

        }
    }


    else{
        Toast.makeText(context,"SORRY I CANT START CALCULATOR :(",Toast.LENGTH_SHORT).show();


    }
}

Now call the calculator method using

openMyCalculator(MainActivity.this);

 

If you have any questions, feel free to ask in the comments below. I try my best to respond to every comment that comes my way. If for any reason you don’t get a response, feel free to ask me on Twitter, and Facebook and if you want to follow me on those social media links as well to see different pictures and just talk about different things going on in the tech world.