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

时间:2011-08-10 08:54:36

标签: android

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

谢谢

2 个答案:

答案 0 :(得分:6)

您必须实施前台服务:

https://developer.android.com/guide/components/services.html#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);

答案 1 :(得分:3)

查看活动生命周期:)

http://developer.android.com/guide/topics/fundamentals/activities.html

我认为你必须提供服务。

相关问题