Question

I have a ContentObserver set up to check for audio changes in a service. I want to know if there's a way to restrain the triggering of the ContentObserver to only certain apps.

Say, I'm playing a game where I can change the volume, after which I change the volume in the settings. I want the service to trigger an alert only when the volume is changed by the game. How do I go about this?

NOTE: The game is a 3rd party app.

EDIT Is there a way to receive the stack order of the commands? Say a program accesses settings to change the volume, obviously settings is gonna know which program is trying to change the volume. Is there a way to get settings to throw the app's package name? Or is there a way to know if it was changed directly in the settings.

Say I'm in the app drawer and change the volume, that would be categorized as direct change and via an app a third party change. Is there a way to find out when it's a direct change?

Was it helpful?

Solution 2

I found a way to circumvent this.

To the service with the ContentObserver, I added this piece of code

private RunningAppProcessInfo getForegroundApp() {
RunningAppProcessInfo result=null, info=null;

if(mActivityManager==null)
    mActivityManager = (ActivityManager)mContext.getSystemService(Context.ACTIVITY_SERVICE);
List <RunningAppProcessInfo> l = mActivityManager.getRunningAppProcesses();
Iterator <RunningAppProcessInfo> i = l.iterator();
while(i.hasNext()){
    info = i.next();
    if(info.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND
            && !isRunningService(info.processName)){
        result=info;
        break;
    }
}
return result;
}

So, now that I know the foreground app, the probability that it triggered the volume change is very high. So I can exclude it from triggering any respone. It's not flawless, but it works.

OTHER TIPS

you can make a broadcast intent of your own and fire it when game makes the change. Then receive it in your second app.

If game is a third party app, then there would be no way I guess.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top