Android Question how to open other app from Service or swith myself?

xiaoyao

Active Member
Licensed User
Longtime User
when i run in service, i wanto show my app.main activity ,how to do?
OPENAPP(Application.PackageName)??

Sub OPENAPP(PACKNAME As String )
B4X:
Sub OPENAPP(PACKNAME As String )
    'GOOD
    Try
        Dim Intent1 As Intent
        Dim pm As PackageManager
        Intent1 = pm.GetApplicationIntent (PACKNAME)
        StartActivity (Intent1)
    Catch
        ToastMessageShow ("Failed to launch app!  Is it installed?", True)
    End Try
    
End Sub
 

xiaoyao

Active Member
Licensed User
Longtime User
like this,but can't find answer:
Bringing my app to the front | B4X Programming Forum

Android provides a simple way to switch an application from the background to the foreground by sending an Intent and starting an Activity. We can use the following code to achieve this function:


Intent intent = new Intent(context, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);

In the above code, MainActivity is the class name of the Activity we want to start, and context is the current context.

This code will create a new task stack and add MainActivity to the task stack. It will then display the task stack to the foreground and display the MainActivity.

Method 2: Use ActivityManager
Another way is to use the ActivityManager class to switch the application to the foreground. This method is relatively complex, but allows for more fine-grained control. Here is sample code:

ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> processes = am.getRunningAppProcesses();

for (ActivityManager.RunningAppProcessInfo process : processes) {
if (process.processName.equals(context.getPackageName())) {
am.moveTaskToFront(process.pid, 0);
break;
}
 
Last edited:
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
Upvote 0

xiaoyao

Active Member
Licensed User
Longtime User
From Android 10 it's not possible to bring an app in foreground from background freely.
You must request the
Draw Over Apps permission
as a workaround. How long will it last?
We can't know.
Here you can check the list of limitations that Google added year by year
https://www.b4x.com/android/forum/threads/android-jar-targetsdkversion-minsdkversion.87610/
what's Draw Over Apps permission?

if i make a app top in android desktop like a icon of i small window?maybe i click it ,will show main activity?
 
  • Like
Reactions: omo
Upvote 0
Top