android 活动结束,android – 一旦活动结束,就会调用onStartCommand服务

当我点击播放按钮播放歌曲并将歌曲mp3与它捆绑在一起并在onStartCommand中接收时,我开始服务.问题是当我启动服务的活动结束时,我的服务再次调用onStartCommand.当它接收该捆绑时它不存在,因为这次活动没有启动它.因此,我在准备媒体播放器时遇到了非法的例外情况.我没有约束我的服务.

为什么在我的活动结束时会调用onStartCommand?

启动服务:

Intent i = new Intent(SongList.this,MyService.class);

i.putExtra("songURL", user.songurlz);

i.putExtra("songNAME", songplay_name);

startService(i);

服务类:

public class MyService extends Service {

static MediaPlayer mediaPlayer;

static int pauseplay;

static NotificationManager notificationManagerPlay;

static CharSequence tickerText;

static Notification notification4;//Notification variable(for song playing)

TelephonyManager telephonyManager;

PhoneStateListener listener;

static Notification notification;

static NotificationManager mNotificationManager;

String songurl;

String songname;

@Override

public IBinder onBind(Intent intent) {

// TODO Auto-generated method stub

return null;

}

@Override

public void onCreate() {

super.onCreate();

mediaPlayer = new MediaPlayer();

pauseplay = 1;

MainMenu.serviceRunning = 1;

telephonyManager = (TelephonyManager) getSystemService(

Context.TELEPHONY_SERVICE);

// Create a new PhoneStateListener

listener = new PhoneStateListener() {

@Override

public void onCallStateChanged(int state, String incomingNumber) {

switch (state) {

case TelephonyManager.CALL_STATE_IDLE:

break;

case TelephonyManager.CALL_STATE_OFFHOOK:

break;

case TelephonyManager.CALL_STATE_RINGING:

if (mediaPlayer.isPlaying() == true && mediaPlayer != null){

pauseSong();

}

break;

}

}

};

// Register the listener wit the telephony manager

telephonyManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);

}

public int onStartCommand(Intent intent, int flags, int startId) {

super.onStartCommand(intent, flags, startId);

if (intent != null) {

Bundle bundle = intent.getExtras();

//Retrieve your data using the name

songurl = bundle.getString("songURL");

Bundle bundle2 = intent.getExtras();

//Retrieve your data using the name

songname = bundle2.getString("songNAME");

}

Toast toast = Toast.makeText(getApplicationContext(), "Now Playing: "

+ songname, Toast.LENGTH_LONG);

toast.show();

// configure the intent

Intent playIntent = new Intent(MyService.this, SongList.class);

final PendingIntent pendingIntent = PendingIntent.getActivity(

MyService.this, 0, playIntent, 0);

notification = new Notification(R.drawable.playicon, "Buffering...",

System.currentTimeMillis());

notification.contentView = new RemoteViews(getPackageName(),

R.layout.custom_notification2);

notification.contentIntent = pendingIntent;

if (SongList.bitmap != null) {

notification.contentView.setImageViewBitmap(R.id.notifimage,

SongList.bitmap);

} else {

notification.contentView.setImageViewResource(R.id.notifimage,

R.drawable.icon);

}

notification.contentView

.setTextViewText(R.id.notiftitle, "Now Playing");

notification.contentView.setTextViewText(R.id.notiftext, songname);

notification.flags = notification.flags

| Notification.FLAG_ONGOING_EVENT;

mNotificationManager = (NotificationManager) getApplicationContext()

.getSystemService(Context.NOTIFICATION_SERVICE);

mNotificationManager.notify(4, notification);

mediaPlayer.reset();

mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

try {

mediaPlayer.setDataSource(songurl);

} catch (IllegalArgumentException e) {

e.printStackTrace();

} catch (IllegalStateException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

// register an error listener via MediaPlayer's setOnErrorListener

mediaPlayer.setOnErrorListener(new OnErrorListener() {

@Override

public boolean onError(MediaPlayer mp, int what, int extra) {

Log.e("MEDIAPLAYER ERRORS", "what: " + what + " extra: "

+ extra);

mediaPlayer.reset();

mNotificationManager.cancel(4);

return false;

}

});

mediaPlayer.prepareAsync();

mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

@Override

public void onPrepared(MediaPlayer mp) {

mediaPlayer.start();

}

});

mediaPlayer

.setOnCompletionListener(new MediaPlayer.OnCompletionListener(){

public void onCompletion(MediaPlayer mp) {

stopSelf();

}

});

return START_STICKY;

}

@Override

public void onDestroy() {

MainMenu.serviceRunning = 0;

mNotificationManager.cancel(4);

}

public static void pauseSong() {

if (mediaPlayer.isPlaying() == true) {

mediaPlayer.pause();

mNotificationManager.cancel(4);

pauseplay = 0;

}

}

public static void playSong() {

if (pauseplay == 0) {

mNotificationManager.notify(4, notification);

mediaPlayer.start();

pauseplay = 1;

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值