简体   繁体   中英

Is there a way to stop audio on Android when my app crashes or hangs?

I've written an app for Android using Xamarin Studio. Occasionally, due to other code, the app will hang and become unresponsive. When the user clicks the home button, the audio continues to play instead of stopping. Is there a way for a "hung" app to know that it has been put in the background and force the audio to pause?

protected override void OnDestroy ()
{
    DependencyService.Get<IMediaController>().Stop();

    // Call base method
    base.OnDestroy ();
}

protected override void OnSleep()
{
    DependencyService.Get<IMediaController>().Pause();
}

I've determined that the way to do this is to monitor the Android running tasks. This enables me to determine if my app has been backgrounded. I have a thread which runs throughout the apps lifetime, and it is normally killed in OnSleep. If OnSleep is not called, then that thread will determine the app is non-responsive, and it will call OnSleep. This is for Xamarin. I got my ideas from this post. how to check the top activity from android app in background service

//Returns the top running task information
private static ActivityManager activityManager;
private static Android.App.ActivityManager.RunningTaskInfo runningTaskInfo;
public static ComponentName GetTopActivity()
{
    if(activityManager == null)
    {
        activityManager = (ActivityManager) Application.Context.GetSystemService(Context.ActivityService);
    }

    IList<Android.App.ActivityManager.RunningTaskInfo> runningTasks =   
        activityManager.GetRunningTasks(1);
    if(runningTasks != null && runningTasks.Count > 0)
    {
        runningTaskInfo = runningTasks[0];
        return runningTaskInfo.TopActivity;
    }
    else
    {
        return null;
    }
}

//This called from my thread every 2 seconds
public bool IsAppVisible()
{
    //have to do a complicated version of this to determine the current running tasks        //and whether our app is the most prominent.
    Android.Content.ComponentName componentName = AndroidUtils.GetTopActivity();
    bool isCurrentActivity;
    if(componentName != null)
    {
        isCurrentActivity = string.Compare(componentName.PackageName, "myPackage") == 0;
    }
    else
    {
        isCurrentActivity = false;
    }

    return isCurrentActivity;
}

if(DependencyService.Get<IDeviceUtility>().IsAppVisible() == false)
{
    //This needs to be manually called if the app becomes completely unresponive
    OnSleep();
    Debug.WriteLine("App is no longer visible");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM