Wednesday, 3 April 2013

How to Open Calculator In ANDROID

If we want to open the inbuilt calculator of ANDROID ,here is the sniffet...
 
public class CalculatorActivity extends Activity {
    public static final String CALCULATOR_PACKAGE ="com.android.calculator2";
    public static final String CALCULATOR_CLASS ="com.android.calculator2.Calculator";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        Intent calculatorintent = new Intent();
        
        calculatorintent.setAction(Intent.ACTION_MAIN);
        calculatorintent.addCategory(Intent.CATEGORY_LAUNCHER);
        calculatorintent.setComponent(new ComponentName(CALCULATOR_PACKAGE,CALCULATOR_CLASS));

        CalculatorActivity.this.startActivity(calculatorintent);       
    } }
The above may not work with every devices, so you may need this one..
 
 ArrayList<HashMap<String,Object>> items =new ArrayList<HashMap<String,Object>>();
        PackageManager pm;
        final PackageManager p = getPackageManager();
        List<PackageInfo> packs = p.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(p));
            map.put("packageName", pi.packageName);
            items.add(map);
         }
        }
        if(items.size()>=1){
         String packageName = (String) items.get(0).get("packageName");
         Intent i = p.getLaunchIntentForPackage(packageName);
         if (i != null)
           startActivity(i);
         } 
         else{
               // Application not found
            }

1 comment: