Reputation: 15
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!
Upvotes: 0
Views: 888
Reputation: 3562
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
}
}
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);
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);
Upvotes: 1
Reputation: 16
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
Upvotes: 0