我是一名新的Android开发人员,我制作了一个ShoutCast广播播放器。它运行良好,但是当我打开应用程序时,它可以工作几次但突然停止,我需要按停止并再次播放,但我需要处理Android播放器错误以自动重启错误。
package com.test.test;
import java.io.IOException;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.media.AudioManager.OnAudioFocusChangeListener;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.util.Log;
public class StreamService extends Service {
private static final String TAG = "StreamService";
MediaPlayer mp;
boolean isPlaying;
Intent MainActivity;
SharedPreferences prefs;
SharedPreferences.Editor editor;
Notification n;
NotificationManager notificationManager;
// Change this int to some number specifically for this app
int notifId = 85;
private OnAudioFocusChangeListener focusChangeListener =
new OnAudioFocusChangeListener() {
public void onAudioFocusChange(int focusChange) {
switch (focusChange) {
case (AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) :
// Lower the volume while ducking.
mp.setVolume(0.2f, 0.2f);
break;
case (AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) :
mp.pause();
break;
case (AudioManager.AUDIOFOCUS_LOSS) :
mp.stop();
break;
case (AudioManager.AUDIOFOCUS_GAIN) :
// Return the volume to normal and resume if paused.
mp.setVolume(1f, 1f);
mp.start();
break;
default: break;
}
}
};
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@SuppressWarnings("deprecation")
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate");
// Init the SharedPreferences and Editor
prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
editor = prefs.edit();
// Set up the buffering notification
notificationManager = (NotificationManager) getApplicationContext()
.getSystemService(NOTIFICATION_SERVICE);
Context context = getApplicationContext();
String notifTitle = context.getResources().getString(R.string.app_name);
String notifMessage = context.getResources().getString(R.string.buffering);
n = new Notification();
n.icon = R.drawable.ic_launcher;
n.tickerText = "Buffering";
n.when = System.currentTimeMillis();
Intent nIntent = new Intent(context, MainActivity.class);
nIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, nIntent, 0);
n.setLatestEventInfo(context, notifTitle, notifMessage, pIntent);
notificationManager.notify(notifId, n);
// It's very important that you put the IP/URL of your ShoutCast stream here
// Otherwise you'll get Webcom Radio
String url = "http://47.182.19.93:9888/";
mp = new MediaPlayer();
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mp.reset();
mp.setDataSource(url);
mp.prepare();
mp.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
Log.e(TAG, "SecurityException");
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
Log.e(TAG, "IllegalStateException");
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e(TAG, "IOException");
}
}
@SuppressWarnings("deprecation")
@Override
public void onStart(Intent intent, int startId) {
Log.d(TAG, "onStart");
mp.start();
// Set the isPlaying preference to true
editor.putBoolean("isPlaying", true);
editor.commit();
Context context = getApplicationContext();
String notifTitle = context.getResources().getString(R.string.app_name);
String notifMessage = context.getResources().getString(R.string.now_playing);
n.icon = R.drawable.ic_launcher;
n.tickerText = notifMessage;
n.flags = Notification.FLAG_NO_CLEAR;
n.when = System.currentTimeMillis();
Intent nIntent = new Intent(context, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, nIntent, 0);
n.setLatestEventInfo(context, notifTitle, notifMessage, pIntent);
// Change 5315 to some nother number
notificationManager.notify(notifId, n);
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
// Request audio focus for playback
int result = am.requestAudioFocus(focusChangeListener,
// Use the music stream.
AudioManager.STREAM_MUSIC,
// Request permanent focus.
AudioManager.AUDIOFOCUS_GAIN);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
// other app had stopped playing song now , so u can do u stuff now .
}
}
@Override
public void onDestroy() {
Log.d(TAG, "onDestroy");
mp.stop();
mp.release();
mp = null;
editor.putBoolean("isPlaying", false);
editor.commit();
notificationManager.cancel(notifId);
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
am.abandonAudioFocus(focusChangeListener);
}
}
08-26 12:00:49.755: V/MediaPlayer(4800): message received msg=3, ext1=0, ext2=0
08-26 12:00:49.755: V/MediaPlayer(4800): buffering 0
08-26 12:00:49.755: V/MediaPlayer(4800): callback application
08-26 12:00:49.755: V/MediaPlayer(4800): back from callback
08-26 12:00:49.755: V/MediaPlayer(4800): message received msg=5, ext1=0, ext2=0
08-26 12:00:49.755: V/MediaPlayer(4800): New video size 0 x 0
08-26 12:00:49.755: V/MediaPlayer(4800): callback application
08-26 12:00:49.755: V/MediaPlayer(4800): back from callback
08-26 12:00:49.755: V/MediaPlayer(4800): message received msg=1, ext1=0, ext2=0
08-26 12:00:49.755: V/MediaPlayer(4800): prepared
08-26 12:00:49.755: V/MediaPlayer(4800): signal application thread
08-26 12:00:49.755: V/MediaPlayer(4800): callback application
08-26 12:00:49.755: V/MediaPlayer(4800): prepare complete - status=0
08-26 12:00:49.755: V/MediaPlayer(4800): back from callback
08-26 12:00:49.760: V/MediaPlayer-JNI(4800): start
08-26 12:00:49.760: V/MediaPlayer(4800): start
08-26 12:00:49.775: D/StreamService(4800): onStart
08-26 12:00:49.775: V/MediaPlayer-JNI(4800): start
08-26 12:00:49.775: V/MediaPlayer(4800): start
08-26 12:00:49.890: W/MediaPlayer(4800): mediaplayer went away with unhandled events
08-26 12:00:49.890: W/MediaPlayer(4800): mediaplayer went away with unhandled events
08-26 12:00:49.890: W/MediaPlayer(4800): mediaplayer went away with unhandled events
08-26 12:00:49.890: I/MediaPlayer(4800): Don't send intent. msg.arg1 = 0, msg.arg2 = 0
08-26 12:00:50.755: V/MediaPlayer(4800): message received msg=3, ext1=0, ext2=0
08-26 12:00:50.755: V/MediaPlayer(4800): buffering 0
08-26 12:00:50.755: V/MediaPlayer(4800): callback application
08-26 12:00:50.755: V/MediaPlayer(4800): back from callback
08-26 12:00:51.755: V/MediaPlayer(4800): message received msg=3, ext1=0, ext2=0
08-26 12:00:51.755: V/MediaPlayer(4800): buffering 0
08-26 12:00:51.755: V/MediaPlayer(4800): callback application
08-26 12:00:51.755: V/MediaPlayer(4800): back from callback
08-26 12:00:52.755: V/MediaPlayer(4800): message received msg=3, ext1=0, ext2=0
08-26 12:00:52.755: V/MediaPlayer(4800): buffering 0
08-26 12:00:52.755: V/MediaPlayer(4800): callback application
08-26 12:00:52.760: V/MediaPlayer(4800): back from callback
08-26 12:00:53.760: V/MediaPlayer(4800): message received msg=3, ext1=0, ext2=0
08-26 12:00:53.760: V/MediaPlayer(4800): buffering 0
08-26 12:00:53.760: V/MediaPlayer(4800): callback application
08-26 12:00:53.760: V/MediaPlayer(4800): back from callback
08-26 12:00:54.760: V/MediaPlayer(4800): message received msg=3, ext1=0, ext2=0
08-26 12:00:54.760: V/MediaPlayer(4800): buffering 0
08-26 12:00:54.760: V/MediaPlayer(4800): callback application
08-26 12:00:54.760: V/MediaPlayer(4800): back from callback
08-26 12:00:55.760: V/MediaPlayer(4800): message received msg=3, ext1=0, ext2=0
08-26 12:00:55.760: V/MediaPlayer(4800): buffering 0
08-26 12:00:55.765: V/MediaPlayer(4800): callback application
08-26 12:00:55.765: V/MediaPlayer(4800): back from callback
08-26 12:00:56.765: V/MediaPlayer(4800): message received msg=3, ext1=0, ext2=0
08-26 12:00:56.765: V/MediaPlayer(4800): buffering 0
08-26 12:00:56.765: V/MediaPlayer(4800): callback application
08-26 12:00:56.765: V/MediaPlayer(4800): back from callback
08-26 12:00:57.770: V/MediaPlayer(4800): message received msg=3, ext1=0, ext2=0
08-26 12:00:57.770: V/MediaPlayer(4800): buffering 0
08-26 12:00:57.770: V/MediaPlayer(4800): callback application
08-26 12:00:57.770: V/MediaPlayer(4800): back from callback
08-26 12:00:58.770: V/MediaPlayer(4800): message received msg=3, ext1=0, ext2=0
08-26 12:00:58.770: V/MediaPlayer(4800): buffering 0
08-26 12:00:58.770: V/MediaPlayer(4800): callback application
08-26 12:00:58.770: V/MediaPlayer(4800): back from callback
08-26 12:00:59.775: V/MediaPlayer(4800): message received msg=3, ext1=0, ext2=0
08-26 12:00:59.775: V/MediaPlayer(4800): buffering 0
08-26 12:00:59.775: V/MediaPlayer(4800): callback application
08-26 12:00:59.775: V/MediaPlayer(4800): back from callback
08-26 12:01:00.775: V/MediaPlayer(4800): message received msg=3, ext1=0, ext2=0
08-26 12:01:00.775: V/MediaPlayer(4800): buffering 0
08-26 12:01:00.775: V/MediaPlayer(4800): callback application
08-26 12:01:00.775: V/MediaPlayer(4800): back from callback
08-26 12:01:01.780: V/MediaPlayer(4800): message received msg=3, ext1=0, ext2=0
08-26 12:01:01.780: V/MediaPlayer(4800): buffering 0
08-26 12:01:01.780: V/MediaPlayer(4800): callback application
08-26 12:01:01.780: V/MediaPlayer(4800): back from callback
08-26 12:01:02.780: V/MediaPlayer(4800): message received msg=3, ext1=0, ext2=0
08-26 12:01:02.780: V/MediaPlayer(4800): buffering 0
08-26 12:01:02.780: V/MediaPlayer(4800): callback application
08-26 12:01:02.780: V/MediaPlayer(4800): back from callback
08-26 12:01:03.785: V/MediaPlayer(4800): message received msg=3, ext1=0, ext2=0
08-26 12:01:03.785: V/MediaPlayer(4800): buffering 0
08-26 12:01:03.785: V/MediaPlayer(4800): callback application
08-26 12:01:03.790: V/MediaPlayer(4800): back from callback
08-26 12:01:04.790: V/MediaPlayer(4800): message received msg=3, ext1=0, ext2=0
08-26 12:01:04.790: V/MediaPlayer(4800): buffering 0
08-26 12:01:04.790: V/MediaPlayer(4800): callback application
08-26 12:01:04.790: V/MediaPlayer(4800): back from callback
08-26 12:01:05.790: V/MediaPlayer(4800): message received msg=3, ext1=0, ext2=0
08-26 12:01:05.790: V/MediaPlayer(4800): buffering 0
08-26 12:01:05.790: V/MediaPlayer(4800): callback application
08-26 12:01:05.790: V/MediaPlayer(4800): back from callback
08-26 12:01:06.790: V/MediaPlayer(4800): message received msg=3, ext1=0, ext2=0
08-26 12:01:06.790: V/MediaPlayer(4800): buffering 0
08-26 12:01:06.790: V/MediaPlayer(4800): callback application
08-26 12:01:06.795: V/MediaPlayer(4800): back from callback
08-26 12:01:07.795: V/MediaPlayer(4800): message received msg=3, ext1=0, ext2=0
08-26 12:01:07.795: V/MediaPlayer(4800): buffering 0
08-26 12:01:07.795: V/MediaPlayer(4800): callback application
08-26 12:01:07.795: V/MediaPlayer(4800): back from callback
08-26 12:01:08.795: V/MediaPlayer(4800): message received msg=3, ext1=0, ext2=0
08-26 12:01:08.795: V/MediaPlayer(4800): buffering 0
08-26 12:01:08.795: V/MediaPlayer(4800): callback application
08-26 12:01:08.795: V/MediaPlayer(4800): back from callback
08-26 12:01:09.795: V/MediaPlayer(4800): message received msg=3, ext1=0, ext2=0
08-26 12:01:09.795: V/MediaPlayer(4800): buffering 0
08-26 12:01:09.795: V/MediaPlayer(4800): callback application
08-26 12:01:09.795: V/MediaPlayer(4800): back from callback
08-26 12:01:10.795: V/MediaPlayer(4800): message received msg=3, ext1=0, ext2=0
08-26 12:01:10.795: V/MediaPlayer(4800): buffering 0
08-26 12:01:10.795: V/MediaPlayer(4800): callback application
08-26 12:01:10.795: V/MediaPlayer(4800): back from callback
08-26 12:01:11.795: V/MediaPlayer(4800): message received msg=3, ext1=0, ext2=0
08-26 12:01:11.795: V/MediaPlayer(4800): buffering 0
08-26 12:01:11.795: V/MediaPlayer(4800): callback application
08-26 12:01:11.795: V/MediaPlayer(4800): back from callback
08-26 12:01:12.795: V/MediaPlayer(4800): message received msg=3, ext1=0, ext2=0
08-26 12:01:12.795: V/MediaPlayer(4800): buffering 0
08-26 12:01:12.795: V/MediaPlayer(4800): callback application
08-26 12:01:12.795: V/MediaPlayer(4800): back from callback
08-26 12:01:13.800: V/MediaPlayer(4800): message received msg=3, ext1=0, ext2=0
08-26 12:01:13.800: V/MediaPlayer(4800): buffering 0
08-26 12:01:13.800: V/MediaPlayer(4800): callback application
08-26 12:01:13.800: V/MediaPlayer(4800): back from callback
08-26 12:01:14.800: V/MediaPlayer(4800): message received msg=3, ext1=0, ext2=0
08-26 12:01:14.800: V/MediaPlayer(4800): buffering 0
08-26 12:01:14.800: V/MediaPlayer(4800): callback application
08-26 12:01:14.800: V/MediaPlayer(4800): back from callback
08-26 12:01:15.800: V/MediaPlayer(4800): message received msg=3, ext1=0, ext2=0
08-26 12:01:15.800: V/MediaPlayer(4800): buffering 0
08-26 12:01:15.800: V/MediaPlayer(4800): callback application
08-26 12:01:15.800: V/MediaPlayer(4800): back from callback
08-26 12:01:16.800: V/MediaPlayer(4800): message received msg=3, ext1=0, ext2=0
08-26 12:01:16.800: V/MediaPlayer(4800): buffering 0
08-26 12:01:16.800: V/MediaPlayer(4800): callback application
08-26 12:01:16.805: V/MediaPlayer(4800): back from callback
08-26 12:01:17.805: V/MediaPlayer(4800): message received msg=3, ext1=0, ext2=0
08-26 12:01:17.805: V/MediaPlayer(4800): buffering 0
08-26 12:01:17.805: V/MediaPlayer(4800): callback application
08-26 12:01:17.805: V/MediaPlayer(4800): back from callback
08-26 12:01:18.805: V/MediaPlayer(4800): message received msg=3, ext1=0, ext2=0
08-26 12:01:18.805: V/MediaPlayer(4800): buffering 0
08-26 12:01:18.805: V/MediaPlayer(4800): callback application
08-26 12:01:18.805: V/MediaPlayer(4800): back from callback
08-26 12:01:19.805: V/MediaPlayer(4800): message received msg=3, ext1=0, ext2=0
08-26 12:01:19.805: V/MediaPlayer(4800): buffering 0
08-26 12:01:19.805: V/MediaPlayer(4800): callback application
08-26 12:01:19.805: V/MediaPlayer(4800): back from callback
08-26 12:01:20.805: V/MediaPlayer(4800): message received msg=3, ext1=0, ext2=0
08-26 12:01:20.805: V/MediaPlayer(4800): buffering 0
08-26 12:01:20.805: V/MediaPlayer(4800): callback application
08-26 12:01:20.805: V/MediaPlayer(4800): back from callback
08-26 12:01:21.805: V/MediaPlayer(4800): message received msg=3, ext1=0, ext2=0
08-26 12:01:21.805: V/MediaPlayer(4800): buffering 0
08-26 12:01:21.805: V/MediaPlayer(4800): callback application
08-26 12:01:21.805: V/MediaPlayer(4800): back from callback
08-26 12:01:22.805: V/MediaPlayer(4800): message received msg=3, ext1=0, ext2=0
08-26 12:01:22.805: V/MediaPlayer(4800): buffering 0
08-26 12:01:22.805: V/MediaPlayer(4800): callback application
08-26 12:01:22.805: V/MediaPlayer(4800): back from callback
08-26 12:01:23.805: V/MediaPlayer(4800): message received msg=3, ext1=0, ext2=0
08-26 12:01:23.805: V/MediaPlayer(4800): buffering 0
08-26 12:01:23.805: V/MediaPlayer(4800): callback application
08-26 12:01:23.805: V/MediaPlayer(4800): back from callback
08-26 12:01:24.810: V/MediaPlayer(4800): message received msg=3, ext1=0, ext2=0
08-26 12:01:24.810: V/MediaPlayer(4800): buffering 0
08-26 12:01:24.810: V/MediaPlayer(4800): callback application
08-26 12:01:24.810: V/MediaPlayer(4800): back from callback
08-26 12:01:24.810: V/MediaPlayer(4800): message received msg=200, ext1=703, ext2=224
08-26 12:01:24.810: W/MediaPlayer(4800): info/warning (703, 224)
08-26 12:01:24.810: V/MediaPlayer(4800): callback application
08-26 12:01:24.810: V/MediaPlayer(4800): back from callback
08-26 12:01:24.810: V/MediaPlayer(4800): message received msg=200, ext1=701, ext2=0
08-26 12:01:24.810: W/MediaPlayer(4800): info/warning (701, 0)
08-26 12:01:24.810: I/MediaPlayer(4800): Info (703,224)
08-26 12:01:24.810: V/MediaPlayer(4800): callback application
08-26 12:01:24.810: V/MediaPlayer(4800): back from callback
08-26 12:01:24.810: I/MediaPlayer(4800): Info (701,0)
08-26 12:01:25.810: V/MediaPlayer(4800): message received msg=3, ext1=0, ext2=0
08-26 12:01:25.810: V/MediaPlayer(4800): buffering 0
08-26 12:01:25.810: V/MediaPlayer(4800): callback application
08-26 12:01:25.810: V/MediaPlayer(4800): back from callback
08-26 12:01:26.810: V/MediaPlayer(4800): message received msg=3, ext1=0, ext2=0
08-26 12:01:26.810: V/MediaPlayer(4800): buffering 0
08-26 12:01:26.810: V/MediaPlayer(4800): callback application
08-26 12:01:26.810: V/MediaPlayer(4800): back from callback
08-26 12:01:27.810: V/MediaPlayer(4800): message received msg=3, ext1=0, ext2=0
08-26 12:01:27.810: V/MediaPlayer(4800): buffering 0
08-26 12:01:27.810: V/MediaPlayer(4800): callback application
08-26 12:01:27.810: V/MediaPlayer(4800): back from callback
08-26 12:01:28.810: V/MediaPlayer(4800): message received msg=3, ext1=0, ext2=0
08-26 12:01:28.810: V/MediaPlayer(4800): buffering 0
08-26 12:01:28.810: V/MediaPlayer(4800): callback application
08-26 12:01:28.810: V/MediaPlayer(4800): back from callback
08-26 12:01:29.810: V/MediaPlayer(4800): message received msg=3, ext1=0, ext2=0
08-26 12:01:29.810: V/MediaPlayer(4800): buffering 0
08-26 12:01:29.810: V/MediaPlayer(4800): callback application
08-26 12:01:29.810: V/MediaPlayer(4800): back from callback
08-26 12:01:30.815: V/MediaPlayer(4800): message received msg=3, ext1=0, ext2=0
08-26 12:01:30.815: V/MediaPlayer(4800): buffering 0
08-26 12:01:30.815: V/MediaPlayer(4800): callback application
08-26 12:01:30.815: V/MediaPlayer(4800): back from callback
08-26 12:01:31.820: V/MediaPlayer(4800): message received msg=3, ext1=0, ext2=0
08-26 12:01:31.820: V/MediaPlayer(4800): buffering 0
08-26 12:01:31.820: V/MediaPlayer(4800): callback application
08-26 12:01:31.820: V/MediaPlayer(4800): back from callback
08-26 12:01:32.820: V/MediaPlayer(4800): message received msg=3, ext1=0, ext2=0
08-26 12:01:32.820: V/MediaPlayer(4800): buffering 0
08-26 12:01:32.820: V/MediaPlayer(4800): callback application
08-26 12:01:32.825: V/MediaPlayer(4800): back from callback
08-26 12:01:33.825: V/MediaPlayer(4800): message received msg=3, ext1=0, ext2=0
08-26 12:01:33.825: V/MediaPlayer(4800): buffering 0
08-26 12:01:33.825: V/MediaPlayer(4800): callback application
08-26 12:01:33.825: V/MediaPlayer(4800): back from callback
08-26 12:01:34.825: V/MediaPlayer(4800): message received msg=3, ext1=0, ext2=0
08-26 12:01:34.825: V/MediaPlayer(4800): buffering 0
08-26 12:01:34.825: V/MediaPlayer(4800): callback application
08-26 12:01:34.825: V/MediaPlayer(4800): back from callback
08-26 12:01:35.825: V/MediaPlayer(4800): message received msg=3, ext1=0, ext2=0
08-26 12:01:35.825: V/MediaPlayer(4800): buffering 0
08-26 12:01:35.825: V/MediaPlayer(4800): callback application
08-26 12:01:35.825: V/MediaPlayer(4800): back from callback
08-26 12:01:36.830: V/MediaPlayer(4800): message received msg=3, ext1=0, ext2=0
08-26 12:01:36.830: V/MediaPlayer(4800): buffering 0
08-26 12:01:36.830: V/MediaPlayer(4800): callback application
08-26 12:01:36.830: V/MediaPlayer(4800): back from callback
08-26 12:01:37.830: V/MediaPlayer(4800): message received msg=3, ext1=0, ext2=0
08-26 12:01:37.830: V/MediaPlayer(4800): buffering 0
08-26 12:01:37.830: V/MediaPlayer(4800): callback application
08-26 12:01:37.830: V/MediaPlayer(4800): back from callback
08-26 12:01:39.170: V/MediaPlayer(4800): message received msg=200, ext1=702, ext2=0
08-26 12:01:39.170: W/MediaPlayer(4800): info/warning (702, 0)
08-26 12:01:39.170: V/MediaPlayer(4800): callback application
08-26 12:01:39.170: V/MediaPlayer(4800): back from callback
08-26 12:01:39.295: I/MediaPlayer(4800): Info (702,0)
08-26 12:01:40.980: V/MediaPlayer(4800): message received msg=2, ext1=0, ext2=0
08-26 12:01:40.980: V/MediaPlayer(4800): playback complete
08-26 12:01:40.980: V/MediaPlayer(4800): callback application
08-26 12:01:40.980: V/MediaPlayer(4800): back from callback
08-26 12:01:49.390: V/MediaPlayer-JNI(4800): stop
08-26 12:01:49.395: V/MediaPlayer(4800): stop
答案 0 :(得分:1)
您必须在服务中实现onStartCommand方法。
这个方法由android调用(不要直接调用它)。如果您实施它并设置为返回值Service.START_STICKY,则服务将保持“开始”状态。州。如果它被终止,它将自动重启:它处于启动状态,因此如果运行正在运行,android将调用onStartCommand(Intent,int,int)。
无论如何,如果返回START_STICKY,传递给onStartCommand方法的Intent数据将为null,您应该检查它。
docs的代码:
// This is the old onStart method that will be called on the pre-2.0
// platform. On 2.0 or later we override onStartCommand() so this
// method will not be called.
@Override
public void onStart(Intent intent, int startId) {
handleCommand(intent);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
handleCommand(intent);
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}
返回Service.START_REDELIVER_INTENT会做同样的事情,但服务的原始Intent重新发送到onStartCommand方法(这样它就不会为null)