关闭内存不足的通知?

时间:2014-09-01 14:08:43

标签: android

enter image description here

从图中可以看出,当系统内存不足时,这就是我喜欢的媒体播放器通知。此通知是从服务发出的,当系统内存不足时,系统也会自动停止服务。

从行为来看,似乎没有调用Service' onDestroy(),因为它是为了摆脱通知而实现的。所以,我决定实施onTrimMemory()如下:

@Override
public void onTrimMemory(int memory){
    Log.d("TRIM", "Please, Trim Memory");
    String stop = getResources().getString(R.string.stopping_playback);
    if(MusicService.status == 1){
        stopService(PodcastrApplication.newInstance().getMusicServiceIntent());
    }
    Toast.makeText(PodcastrApplication.this, stop, Toast.LENGTH_SHORT).show();
}  

但那没用。我甚至没有看到Toast或日志。

那么,当系统内存不足时,如何删除通知?

PS:当发生这种情况时,可以确定MediaPlayer已经停止播放(自动)。只需删除通知即可。

1 个答案:

答案 0 :(得分:1)

当服务终止时,它会通知所有客户此事件。因此,客户端活动中相应的ServiceConnection对象接收onServiceDisconnected()回调。您可以从此方法中删除通知。

编辑:

private ServiceConnection connection = new ServiceConnection(){

        public void onServiceDisconnected(ComponentName name){

            ClientActivity.this.runOnUiThread(new Runnable({

                public void run(){
                    getSystemService("notification").cancelAll();
                }
            }));
        }

        public void onServiceConnected(...){...}//do something if you need to
};
public void onPause(){        

    Intent i = getIntentForService();
    bindService(i, connection, Context.BIND_AUTO_CREATE);
}
public void onStop(){//or onDestroy()
    unBindService(connection);
}
相关问题