Android- listview, service mediaplayer, and boolean flags

后端 未结 2 2028
温柔的废话
温柔的废话 2021-01-07 12:31

I currently have a listview and when you click on an item it runs a service with a mediaplayer. If I click on another item in the

2条回答
  •  爱一瞬间的悲伤
    2021-01-07 13:05

    This gets pretty complicated but I used the following to see if my service was running:

    private boolean isMyServiceRunning() {
    ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if ("com.example.MyService".equals(service.service.getClassName())) {
            return true;
        }
    }
    return false;
    }
    

    I added this in my listview class and put if statements in each case to see if it was running and if so would stop the service.

    I also made my all of my binding conenctions public so that the listview class could access them and start them on click.

    If anyone wants to further understand, etc message me.

提交回复
热议问题