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

标签 android

我想创建可以播放流媒体音乐的应用程序。当我按下主页时,我的应用程序可以在后台运行,但是当我打开另一个使用更多内存的应用程序时,我的应用程序将停止并被 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/

相关文章:

javascript - Android 上的 Chrome - 不加载本地网站 javascript 和 css

android - AppCompat 标题颜色

android - 通过 Renderscript 在 GPU 中分配内存

android - 隐藏工具栏时无法启用 webview 的滚动行为

root 设备上的 Android 屏幕截图

java - 将连接的字符串按每组 160 个字符进行分组,并对剩余的字符串执行相同的操作

android - 指纹:尝试次数过多的问题

android - 如何在 Android 中打开 Instagram 的相机?

android - 强制 MediaRecorder 使用内部麦克风

android - onClick 自定义 TextView 不适用于低于 5 的 Android 版本