can't get internal calulator to come up

wheretheidivides

Active Member
Licensed User
Longtime User
I tried using this code on 2 phones and a tablet. All 3 times it came with "Calculator app not found." I was wondering is there another way to find the calculator app? this code is from beginner's guide.


B4X:
The subroutine below calls the internal calculator.
Sub Calculator
Dim i As Intent
i.Initialize("", "")
i.SetComponent("com.android.calculator2/.Calculator")
Try
StartActivity(i)
Catch
ToastMessageShow("Calculator app not found.", True)
End Try
End Sub
 

Mahares

Expert
Licensed User
Longtime User
Your code looks the same as mine. Mine works. Try copying and paste mine. If it does not work, comment the try, catch, end try to see the exact error.
B4X:
Sub Calculator
    Dim i As Intent
    i.Initialize("", "")
    i.SetComponent("com.android.calculator2/.Calculator")
    Try
        StartActivity(i)
    Catch
        ToastMessageShow("Calculator app not found.", True)
    End Try
End Sub
 
Upvote 0

JonPM

Well-Known Member
Licensed User
Longtime User
This may be called differently on different devices. Some manufacturers do that.
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
I know this would sound as reinventing the wheel (and probably is), but if there's a lack of a standard utility (like calculator), I would go for creating my very own inside my app.
 
Upvote 0

wheretheidivides

Active Member
Licensed User
Longtime User
Holy crap Batman, I figured it out.

1) There is internal calculator, but some manufacturers like Samsung take it out and put in their own.
2) Using erel's code listed in instructions, will only display internal calculator from google. So using a Samsung phone, you will get an error because it can't find it.

B4X:
Sub Calculator
    Dim i As Intent
    i.Initialize("", "")
    i.SetComponent("com.android.calculator2/.Calculator")
    Try
        StartActivity(i)
    Catch
        ToastMessageShow("Calculator app not found.", True)
    End Try
End Sub

3) You can use the following code from phone to look for package name of calculator. I assignisged this to a button so when I click a button a list pops up with all the packages.

B4X:
'-----------------------------------------
   Dim p As Phone
    Dim sb As StringBuilder
    sb.Initialize
    p.Shell("pm", Array As String("list", "packages"), sb, Null) 
    Msgbox(sb.ToString, "Installed packages:")
''-------------------------------------

4) I found a line that said "Package: com.sec.android.app.calculator

5) I just replaced the line for the internal calculator with the line for the Samsung calculator like

B4X:
'Pops up internal android calulator
   Dim i As Intent
      i.Initialize("", "")
      i.SetComponent("com.sec.android.app.calculator/.Calculator")
   Try
      StartActivity(i)
   Catch
      ToastMessageShow("Calculator app not found.", True)
   End Try


6) and it worked. So using this example, it will only run a calculator with the sameung name, not an internal one. So you have to first check for google calc, if none then go through a list of manufacters like samsung. I am not sure about the try and catch, but could someone give an example of modified code to check for internal calc first and if don't find, then look for Samsung. That way when we find other calcs, we can just add to the code.

7) Here's a beginning list of calculators by manufacturer or device. Maybe this could go into manual.
Standard Internal Google Calc: i.SetComponent("com.android.calculator2/.Calculator")
Samsung Galaxy S1 Epic 4 G Calc: i.SetComponent("com.sec.android.app.calculator/.Calculator")

8) So here's my nested try to look for internal calc then Samsung. It only does 2, hopefully someone wil rewrite without nested loop.
B4X:
'-------------------------------------
Sub Button1100Calculator_Click
'-------------------------------------
'Pops up internal android calulator
   Dim Cal As Intent
      Cal.Initialize("", "")
   '--------------------------
   'list of calcualtor package names by device
      'COMMENT: Cal.SetComponent("com.android.calculator2/.Calculator") 'internal google-need to get this to check
      'COMMENT: Cal.SetComponent("com.sec.android.app.calculator/.Calculator") 'samgsung epic 4g
   '---------------
   Try
      Cal.SetComponent("com.android.calculator2/.Calculator")
      StartActivity(Cal)
   Catch
      '==========
      Try
         Cal.SetComponent("com.sec.android.app.calculator/.Calculator")
         StartActivity(Cal)
      Catch
           Msgbox("Calculator not found for your device.  When the next screen pops up, scroll down list until you find something like 'Package: com.sec.android.app.calculator'. Write it down and send it to us.  We'll try to include it in the next update.","") 
         '-----------------------------------------
          Dim p As Phone
          Dim sb As StringBuilder
          sb.Initialize
          p.Shell("pm", Array As String("list", "packages"), sb, Null) 
          Msgbox(sb.ToString, "Installed packages:")
         '-------------------------------------
      End Try
      '==============
   End Try
   '----------------
   
End Sub
 
Last edited:
Upvote 0

wheretheidivides

Active Member
Licensed User
Longtime User
Now, the best way is use Package Manager to get the internal application package name and find calculator in all Strings if found then extract main class name or package for starting it from this way you will support all device or tablets for launching default calculator. SO really just search packages for 'calc' and store it as variable to be used with code from previous post. Does anyone know how to do this?

in java
=========
ArrayList<HashMap<String,Object>> items =new ArrayList<HashMap<String,Object>>();
PackageManager pm;
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
}
 
Last edited:
Upvote 0

dxxxyyyzzz

Member
Licensed User
Longtime User
B4X:
Dim Pm As PackageManager
   Dim Inte As Intent
   Dim Packages As List
   Dim st As String

   Packages = Pm.GetInstalledPackages
   
   For i = 0 To Packages.size - 1
      st=Packages.Get(i)
      If st.Contains("calc") =True Then
         Inte=Pm.GetApplicationIntent(st)
                If Inte.IsInitialized Then   StartActivity(Inte)
            Exit
      End If
   Next
 
Upvote 0

wheretheidivides

Active Member
Licensed User
Longtime User
Hey, that worked. Now if Erel would include that code in his manual of how to run a calculator, we'd be good. This is the stuff needed for reference not in forem but user's guide. Good Job.
 
Upvote 0
Top