質問

I want to set ringvolume to 0 when a user opens my teaching application then return it to the original volume when the user quits. What's the best way of doing this?
I can set volume to 0 with no problems
The difficulty is knowing when to restore it, my application has multiple activities which are used frequently and I'm worried about the performance implications of setting the volume to 0 for every onCreate()and restoring it for every onStop() It also seems messy and labour intensive!
Does anyone have any suggestions?

As an addendum,

I've thought of a way of doing half of it:
create a boolean leaving =true;
when user opens new intent for my next activity set this to true
in the onPause() use this variable to check if the user is "leaving" or simply opening another one of "my activities"
This still means that I must set ringer to silent in all my onResume()s
Does anyone have any further solutions?

役に立ちましたか?

解決

The problem here is that your app may not always be at the top of the activity stack when it is running. What if the user switches to another app, or some alarm or phone call takes over the focus?

Here are some ways I would go about achieving what you are trying to do (expanded from the comments below):

Option 1:

If the mute action does not affect the performance of your app (from my own experience I suspect it won't), you could simplify the solution you suggested in your question with a single abstract base class, overloading onResume() and onPause() to ensure you capture the event when the user returns to your already created activity:

public abstract class TeachingAppActivity extends Activity {
    public void onResume() {
        super.onResume();
        // Perform mute action here...
    }

    public void onPause() {
        super.onPause();
        // Perform unmute action here...
    }
}

public class MyActivity1 extends TeachingAppActivity {
} 

public class MyActivity2 extends TeachingAppActivity {
}

This would simplify your code, but keep the action contained within your activities: If another app grabs the focus, the ringer will be restored. If the user returns to MyActivity2, the ringer will be muted again.

Option 2:

If the mute action is indeed going to affect the performance of your app, or you simply don't want to perform execute it in every activity's onResume() and onPause(), you could instead use a worker thread and a status bar notification:

  1. In each Activity onResume(); start an AsyncTask (or Runnable task), you could subclass Activity to share this code with your other activities.
  2. This worker thread first checks, then mutes, the volume. If the volume is changed, it then displays a notification icon that informs the user that the volume has been muted.
  3. The worker thread can then terminate, having performed this check and action without affecting UI performance.
  4. The volume will remain muted for the duration of the app, if the user jumps to another app (or an incoming call/message takes the focus), he/she is still aware that the volume is muted because the notification icon is still present.
  5. If the user presses the notification icon, a broadcast intent restores the volume. This could also be triggered by a button or action within your app.

If the user presses the Home button, or presses the Back button repeatedly in your app, the volume will not be restored. This is intentional, since they have not "quit" your app, they have simply moved to the bottom of the activity stack. The rule will be: as long as the notification icon is visible, the volume has been muted by your app.

The added benefit here is that if another app changes the volume, your app will always check when any of your activities receives the focus again.

他のヒント

use the function

@Override
public void onResme() {
super.onResme();
//mute volume here
}

It will help you to make what you want

use the function

@Override
public void onDestry() {
  super.onDestroy();
  // re-enable sound here
}

in your main activity, so you can response to both activity "quit" and "kill by a task manager".

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top