my app killed by android system when it running in background

前端 未结 2 941
闹比i
闹比i 2020-12-18 11:13

I want to create application that can play a streaming music . When I press home my app can running in background but when I open another application that use more memory my

相关标签:
2条回答
  • 2020-12-18 11:47

    You have to implement a foreground service:

    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.

    Example:

    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);
    
    0 讨论(0)
  • 2020-12-18 12:10

    Look the activity lifecycle :)

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

    I think you must do a service.

    0 讨论(0)
提交回复
热议问题