活动结束后只调用一次Service onStartCommand方法

8
当我点击播放按钮来播放一首歌曲并将歌曲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;
        }
    }
}
2个回答

7
为什么我的活动结束时 onStartCommand 会被调用?
要么您在“活动结束”时调用了 startService(),要么由于使用 START_STICKY,Android 正在重新启动您的服务。
就我个人而言,在音乐播放器方面,我认为 START_NOT_STICKY 是正确的答案。如果您的服务因任何原因停止,则用户应该负责再次启动服务。

我会尝试使用START_NOT_STICKY,也许我误解了这个术语。我的意思是,当使用返回按钮关闭应用程序时,音乐停止并且应用程序强制关闭。看起来服务在活动结束后尝试重新启动,并且Bundle.getIntents变为null。 - Nick Nelson
这样解决了我的强制关闭问题,但是当我点击播放,然后点击返回按钮离开应用程序时,音乐停止播放。在服务销毁时消失的通知仍然存在。 - Nick Nelson
我正在使用自定义的ICS ROM Samsung Galaxy S Vibrant ICS Passion。 - Nick Nelson
@NickNelson:当您的活动暂停、停止或销毁时,某些东西正在调用stopService() - CommonsWare
在这种情况下,通知应该会消失,但它没有。它仍然停留在状态栏中。 - Nick Nelson

3
我知道这个答案可能已经不再有用了,但我遇到了同样的问题,并通过查看返回代码以解决onStartCommand的问题。
编辑:
您的onStartCommand应该像这样:
public int onStartCommand(Intent intent, int flags, int startId)
{
    //the same code
    //......
    //then return the new code which I'm pointing to
    return START_REDELIVER_INTENT;
}

以下是有关该返回代码的文档内容:

这是文档中对此返回代码的说明:

public static final int START_REDELIVER_INTENT

onStartCommand(Intent, int, int)的常量返回值:如果这个服务在启动后被杀死(在返回onStartCommand(Intent, int, int)后),它将被安排重新启动,并通过onStartCommand(Intent, int, int)再次传递最后一个投递的Intent。

使用此返回代码将使操作系统在每次调用onStartCommand(不管是什么原因引起的)时重新发送Intent。


您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - Rohit Gupta

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接