
为什么在我的活动结束时会调用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,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; } }}解决方法 Why does the onStartCommand get called when my activity ends?
要么在“活动结束”时调用startService()(无论这意味着什么),要么AndroID因为您使用START_STICKY而因某种原因重新启动服务.
就个人而言,对于音乐播放器,我认为START_NOT_STICKY是正确的答案.如果您的服务因任何原因而停止,用户应该负责再次启动它.
总结以上是内存溢出为你收集整理的android – 一旦活动结束,就会调用onStartCommand服务全部内容,希望文章能够帮你解决android – 一旦活动结束,就会调用onStartCommand服务所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)