Android Question How to convert to B4A B4XPage code that opens the default calculator apps by intent

toby

Well-Known Member
Licensed User
Longtime User
The code below is supposed to open the default calculator apps on a device. Could some expert kindly convert it to B4A?

TIA

B4X:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_APP_CALCULATOR);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Source
 
Last edited:
Solution
This solution works for old default projects (activity). But B4XPage projects cause the error mentioned in post #3. Any guru can make it work for B4XPage projects?
For B4xPage project you posted #3, you only need to move the inline java code into Main module. it will work

teddybear

Well-Known Member
Licensed User
Here is B4A code
Note: It does not work in android emulator

B4X:
Sub Button1_Click
    Dim jo As JavaObject
    jo.InitializeContext
    jo.RunMethod("openCalculator", Null)
End Sub

#If JAVA
import android.content.Intent;
public void openCalculator() {
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_APP_CALCULATOR);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
   }
#End If
 
Last edited:
Upvote 0

toby

Well-Known Member
Licensed User
Longtime User
I got the "cannot find symbol startActivity(intent);
" error while compiling the above code. Could someone tell me what went wrong, please? Test app attached.

TIA
B4A Version: 12.20
Parsing code. (0.03s)
Java Version: 11
Building folders structure. (0.01s)
Running custom action. (0.04s)
Compiling code. (0.07s)
Compiling layouts code. (0.00s)
Organizing libraries. (0.00s)
(AndroidX SDK)
Compiling resources (0.07s)
Linking resources (0.29s)
Compiling debugger engine code. (1.59s)
Compiling generated Java code. Error
B4A line: 15
End Sub
src\com\ddg\test\b4xmainpage.java:112: error: cannot find symbol
startActivity(intent);
^
symbol: method startActivity(Intent)
location: class b4xmainpage
 

Attachments

  • openCalculators.zip
    16.2 KB · Views: 106
Upvote 0

Alex_197

Well-Known Member
Licensed User
Longtime User
I got the "cannot find symbol startActivity(intent);
" error while compiling the above code. Could someone tell me what went wrong, please? Test app attached.

TIA
I'm not sure if B4XPages supports StartActivity.
 
Upvote 0

Alex_197

Well-Known Member
Licensed User
Longtime User
Check my project
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
    Dim jo As JavaObject
    jo.InitializeContext
    jo.RunMethod("openCalculator", Null)
End Sub

#If JAVA
import android.content.Intent;
public void openCalculator() {
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_APP_CALCULATOR);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
   }
#End If

Private Sub Button2_Click
    Dim Pm As PackageManager 'Phone library
    Dim Inte As Intent
    For Each st As String In Pm.GetInstalledPackages
        If st.Contains("calc") =True Then
            Inte=Pm.GetApplicationIntent(st)
            If Inte.IsInitialized Then StartActivity(Inte)
            Exit
        End If
    Next
End Sub
 

Attachments

  • OpenCalculatorIntent.zip
    9.5 KB · Views: 104
Upvote 0

Alex_197

Well-Known Member
Licensed User
Longtime User
The problem associated with this approach is that Google considers the required permission QUERY_ALL_PACKAGES dangerous and wouldn't allow an app to have it normally.
my project doesn't need this permission - check it
 
Upvote 0

toby

Well-Known Member
Licensed User
Longtime User
Here is B4A code
Note: It does not work in android emulator

B4X:
Sub Button1_Click
    Dim jo As JavaObject
    jo.InitializeContext
    jo.RunMethod("openCalculator", Null)
End Sub

#If JAVA
import android.content.Intent;
public void openCalculator() {
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_APP_CALCULATOR);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
   }
#End If
This solution works for old default projects (activity). But B4XPage projects cause the error mentioned in post #3. Any guru can make it work for B4XPage projects?
 
Upvote 0

toby

Well-Known Member
Licensed User
Longtime User
yes it does. check my attached project
My project failed because it's B4XPage project while yours work because it's an activity based project. Now I'm wondering how to make it work with B4Xpage projects
 
Upvote 0

toby

Well-Known Member
Licensed User
Longtime User
my project doesn't need this permission - check it
Using your test app, when I clicked Button2, nothing happened until I added the following to the Manifest
B4X:
AddManifestText(<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"/>)
This requirement is confirmed at this post

About my test phone: Google Pixel 4A, Android 13
 
Upvote 0

teddybear

Well-Known Member
Licensed User
This solution works for old default projects (activity). But B4XPage projects cause the error mentioned in post #3. Any guru can make it work for B4XPage projects?
For B4xPage project you posted #3, you only need to move the inline java code into Main module. it will work
 
Last edited:
Upvote 0
Solution
Top