开发者

How to invoke the Android calculator from an app

I want to invoke the android calculator from within my app. I don't want to pass anything to it, just provide a quick launch button to it. Can anyone show开发者_开发百科 me how or point me to an example of how to do this?


Taken from here:

Intent i = new Intent();
i.setClassName("com.android.calculator2",
               "com.android.calculator2.Calculator");
startActivity(i); 


Try this if you don't know the package name of your Calculator App:

First 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); 
 }

Then traverse 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 Calculator",Toast.LENGTH_SHORT).show();

                        startActivity(i);}
                    else {
                        Toast.makeText(getContext(),"Error opening Calculator!",Toast.LENGTH_SHORT).show();

                    }
                }


                else{
                    Toast.makeText(getContext(),"Error starting Calculator!",Toast.LENGTH_SHORT).show();


                }
}

And then call the function

opencalculator();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜