0

I am new to android. I want to know about..

I want to develop an app which start when another app(like gmail or facebook etc.) in the device started. I have no idea of how to develop this app. I want to support from API 21 & above

For older version of android below 8.0 i can use service and launch my app when specific app started (like applocker) But with higher version of android service is not allowed.

And How i can start a service or any alternative class/component in android 8.0 and higher when my app is not running(killed). thanks.

MPG
  • 785
  • 6
  • 20

1 Answers1

0

Start service with thread and call below functionality with certain period of sleep time like one second in thread. This will start your app when other app runs in foreground

List<ActivityManager.RunningAppProcessInfo> runningProcesses = ((ActivityManager) getSystemService(Context.ACTIVITY_SERVICE)).getRunningAppProcesses();
for (int currentProcess = 0; currentProcess < runningProcesses.size(); currentProcess++)
{
    if (runningProcesses.get(currentProcess).importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND)
    {
        if(!runningProcesses.get(currentProcess).pkgList[0].equalsIgnoreCase("your_app"))
        {
            //start your activity
            break;
        }
    }
}
Ganesan
  • 59
  • 5
  • Thanks for your partial answer, but how i can start a service in android 9.0 when my application is not running? – MPG Nov 22 '19 at 08:13