這是我的音樂服務類::::::
public class MusicService extends Service {
public static final String ACTION_NEXT = "NEXT" 。
public static final String ACTION_PREV = "prevIOUS"。
public static final String ACTION_PLAY = "play"。
public static final String ACTION_FORWARD = "FORWARD" 。
public static final String ACTION_REWIND = "REWIND"/span>;
public static final String ACTION_CONTINUE = "CONTINUE"/span>。
ActionPlaying actionPlaying;
動作動作。
private final IBinder mBinder = new MyBinder() 。
@Nullable
@Override
public IBinder onBind(Intent intent) {
return mBinder。
}
public class MyBinder extends Binder {
MusicService getService() {
return MusicService.this。
}
}
@Override; }
public int onStartCommand(Intent intent, int flags, int startId) {
String actionName = intent.getStringExtra("myActionName") 。
if (actionName != null) {
switch (actionName)
{
case ACTION_PLAY:
if(actionPlaying!= null){
if(action!= null){
actionPlaying.playClicked()。
action.playPauseClicked()。
}}
break。
case ACTION_NEXT:
if(actionPlaying != null){
actionPlaying.nextClicked()。
}
break。
case ACTION_PREV:
if(actionPlaying != null){
actionPlaying.prevClicked()。
}
break。
case ACTION_FORWARD:
if(actionPlaying != null){
actionPlaying.forwardClicked()。
}
break。
case ACTION_REWIND:
if(actionPlaying != null){
actionPlaying.rewindClicked()。
}
break。
case ACTION_CONTINUE:
Toast.makeText(this, "continue", Toast.LENGTH_SHORT).show()。
action.continueMediaPlayer()。
}
}
return START_STICKY;
}
public void setCallBack(ActionPlaying actionPlaying){
this.actionPlaying = actionPlaying。
}
public void setCallBack(Action action) {
this.action = action。
}
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent)。
notificationManager.cancelAll()。
saveData()。
simpleExoPlayer.release()。
}
}
這是我的媒體播放器的初始化 ::::::::::::::
public void prepareMedia() { isPlaying=true; simpleExoPlayer = new SimpleExoPlayer.Builder(MediaPlayer_Activity.this).build()。 MediaItem mediaItem = MediaItem.fromUri(audioUrl); simpleExoPlayer.addMediaItem(mediaItem)。 simpleExoPlayer.prepare(); simpleExoPlayer.seekTo(songPrevPosition)。 }
請告訴我出了什么問題,LogCat沒有顯示任何東西,我不知道如何阻止它,問題出在哪里....。 請幫助我
uj5u.com熱心網友回復:
在9個Android版本之后,我們對簡單服務進行了保護。簡單服務可以活10秒,不能再多了。你需要宣告你的服務為Foreground Service。它將在狀態欄中看起來像通知。
要了解全部資訊,請閱讀檔案
。uj5u.com熱心網友回復:
根據Android 開發人員檔案 :
- 你需要將你的后臺行程升級到前臺,以便在不需要你的應用程式對用戶可見的情況下使其上線。
- 為了將您的服務改為前臺服務,您的服務需要有一個通知,以告知用戶您的音樂播放器服務正在作業。
一個簡單的前臺服務代碼可能看起來像這樣:
public int onStartCommand(intent意圖。int flags, int startId) {
super.onStartCommand(intent, flags, startId)。
Notification notification = buildStartingNotification()。
startForeground(startId, notification)。
...
return START_STICKY;
}
private Notification buildStartingNotification() {
int priority =NotificationCompat.PRIORITY_LOW;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
優先級 = NotificationManager.IMPORTANCE_LOW;
createNotificationChannel(priority);
}
Notification notification =
new Notification.Builder(this, CHANNEL_ID)
.setContentTitle("Your_notification_title")
.setContentText("Your_notification_description")
.setSmallIcon(R.drawable.icon)
.setPriority(優先級)
.build()。
}
private void createNotificationChannel(int priority) {
CharSequence name = getText("Your_Channel_Name")。
String description = getString("Your_Channel_Desc")。
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, priority)。
channel.setDescription(description)。
NotificationManager notificationManager = getSystemService(NotificationManager.class)。
notifyManager.createNotificationChannel(channel)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/306686.html
標籤:
上一篇:為什么日志級別沒有變化?
