gpt4 book ai didi

android - 关闭内存不足的通知?

转载 作者:行者123 更新时间:2023-11-30 02:42:10 25 4
gpt4 key购买 nike

enter image description here

正如您从图片中看到的,这就是当系统内存不足时我花哨的媒体播放器通知所发生的情况。此通知由服务发出,当系统内存不足时,系统也会自动停止该服务。

从行为上看,ServiceonDestroy() 似乎没有被调用,因为它是为了摆脱通知而实现的。所以,我决定按如下方式实现 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 或 Log。

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

PS:发生这种情况时,可以确定 MediaPlayer 已停止播放(自动)。这只是删除通知的问题。

最佳答案

当服务终止时,它会将此事件通知所有客户端。因此,对应的ServiceConnection客户端 Activity 中的对象接收 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);
}

关于android - 关闭内存不足的通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25607637/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com