0

以下のコードの場合、ご覧のとおり、1つnew IntentはにbindService()あり、もう1つはにありnew IntentますstartService()。そして、最終的に2つのインテントがあるのだろうか?または、2つのインテントはまだOKですか?

bindService(new Intent(this, MusicPlayerService.class),
        mPlaybackConnection, Context.BIND_AUTO_CREATE);
startService(new Intent(this, MusicPlayerService.class));
4

2 に答える 2

4

このコードは次と同等です。

Intent intent = new Intent(this, MusicPlayerService.class);
bindService(intent, mPlaybackConnection, Context.BIND_AUTO_CREATE);
startService(intent);

Intentあなたが提供したコードでは、毎回同じオブジェクトが作成されます。

このコードは、どちらも同じことを行うという意味で同等です。ただし、オブジェクトは 1 回しか作成されないため、全体で 1 つの Intent を使用すると、非常にわずかに高速になります。それ以外は、どちらも正しく、どちらも同じことを行います。

于 2012-09-22T07:02:30.127 に答える
1

私はこのコードを考えていない、

bindService(new Intent(this, MusicPlayerService.class),
        mPlaybackConnection, Context.BIND_AUTO_CREATE);
startService(new Intent(this, MusicPlayerService.class));

と同等です。

    Intent intent = new Intent(this, MusicPlayerService.class);
bindService(intent, mPlaybackConnection, Context.BIND_AUTO_CREATE);
startService(intent);

最初の例では、2 つの異なるインテントが作成されています。ただし、2 番目のコードではインテントが 1 つしか作成されないため、2 番目のコードを使用することをお勧めします。

于 2012-09-22T07:37:01.403 に答える