gpt4 book ai didi

android - 我的应用程序在后台运行时被 android 系统杀死

转载 作者:太空宇宙 更新时间:2023-11-03 12:01:11 25 4
gpt4 key购买 nike

我想创建可以播放流媒体音乐的应用程序。当我按下主页时,我的应用程序可以在后台运行,但是当我打开另一个使用更多内存的应用程序时,我的应用程序将停止并被 android 系统杀死。有人有其他想法在后台运行我的音乐播放器应用程序吗?

谢谢

最佳答案

你必须实现一个前台服务:

https://developer.android.com/guide/components/services.html#Foreground

A foreground service is a service that's considered to be something the user is actively aware of and thus not a candidate for the system to kill when low on memory. A foreground service must provide a notification for the status bar, which is placed under the "Ongoing" heading, which means that the notification cannot be dismissed unless the service is either stopped or removed from the foreground.

例子:

Notification notification = new Notification(R.drawable.icon, getText(R.string.ticker_text),
System.currentTimeMillis());
Intent notificationIntent = new Intent(this, ExampleActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, getText(R.string.notification_title),
getText(R.string.notification_message), pendingIntent);
startForeground(ONGOING_NOTIFICATION_ID, notification);

关于android - 我的应用程序在后台运行时被 android 系统杀死,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7008162/

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