0

I will explain my app first. My app consists of a service running in the background. The service is waiting for a certain app to be opened (spotify in my case).

Whenever Spotify opens it needs to show this popup with buttons and text. It has to basicly look like this: imgur.com/QHWZpu6

I've already tried DialogFragment but that doesn't work since there is no FragmentManager in the service class.

Do you guys have any suggestions? Thanks in advance!

Zohaib Amir
  • 3,482
  • 2
  • 11
  • 29
Robbertie
  • 15
  • 5
  • You can declare your activity as a Dialog in resource file to show it as a dialog. Not sure about detecting app launch since the log method doesn't work on newer devices. – Zohaib Amir Jul 30 '16 at 20:08
  • How about using a tiny and transparent-window activity? – frogatto Jul 30 '16 at 20:12

2 Answers2

1

Detect if App is Running/Launched

Take a look at ActivityManager

The method you are concerned with is ActivityManager.RunningAppProcessInfo(). It returns the package names of current running apps as a list. All you have to do is iterate through list and check if it matches app package, which in your case is spotify.

ActivityManager activityManager = (ActivityManager) this.getSystemService( ACTIVITY_SERVICE );
List<RunningAppProcessInfo> appInfos = activityManager.getRunningAppProcesses();
for(int i = 0; i < appInfos.size(); i++)
{
    if(appInfos.get(i).processName.equals("package name"))
    {
        //USE INTENT TO START YOUR ACTIVITY AS EXPLAINED BELOW
    }
}

Start Activity from Service

To start activity from your service, use Intent as following:

Intent popUpIntent = new Intent(this, MyActivity.class);
popUpIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(popUpIntent);

Popup Activity / Show as Dialog

In order to display your Activity as a dialog, you just need to set your activity theme to "Theme.Dialog" in your manifest file

 <activity 
  ...
 android:theme="@android:style/Theme.Dialog" 
  ...
   />

Or you can dynamically set the theme in the activity by calling setTheme() inside Activity class.

setTheme(android.R.style.Theme_Dialog);
Zohaib Amir
  • 3,482
  • 2
  • 11
  • 29
  • Thanks a lot for your detailed comment! Can you actually make a Theme_Dialog look like the example I gave you? – Robbertie Jul 30 '16 at 21:02
  • @Robbertie You can make your dialog activity look like anything. It works like a normal activity so it will look exactly like you design it in your layout file. – Zohaib Amir Jul 30 '16 at 22:06
  • Thanks a lot dude, its working. Only thing is that is doesn't keep the current app as background but it goes to my main activity. So instead of having spotify in the background it has my app layout.. – Robbertie Jul 31 '16 at 00:07
  • Nailed it. For future googlers: what I did is added this: http://stackoverflow.com/questions/2147144/android-how-to-display-a-dialog-over-a-native-screen – Robbertie Jul 31 '16 at 00:15
  • @Robbertie You may wanna accept the answer if your issue is resolved. – Zohaib Amir Aug 01 '16 at 11:30
0

A service class is not supposed to interact with the UI thread. A simple interaction to get past this is to use the service thread to update something stored in SharedPreferences and set a shared preferences change listener in the UI thread for whichever activity you want to handle the UI display.

void onSharedPreferenceChanged (SharedPreferences sharedPreferences, 
                String key)

Based on that image, it seems you actually want to display a UI element when you're activity is not currently the active application, you might want to consider a notification for this instead

Zebesta
  • 16
  • 1