START_STICKY
ध्वज के साथ एक अग्रभूमि सेवा बनाने के लिए लिंक भी है।
@Override
public int onStartCommand(Intent startIntent, int flags, int startId) {
if (startIntent != null) {
String action = startIntent.getAction();
String command = startIntent.getStringExtra(CMD_NAME);
if (ACTION_CMD.equals(action)) {
if (CMD_PAUSE.equals(command)) {
if (mPlayback != null && mPlayback.isPlaying()) {
handlePauseRequest();
}
} else if (CMD_PLAY.equals(command)) {
ArrayList<Track> queue = new ArrayList<>();
for (Parcelable input : startIntent.getParcelableArrayListExtra(ARG_QUEUE)) {
queue.add((Track) Parcels.unwrap(input));
}
int index = startIntent.getIntExtra(ARG_INDEX, 0);
playWithQueue(queue, index);
}
}
}
return START_STICKY;
}
यह तो आप bindService का उपयोग करके सेवा बाध्य कर सकते हैं और सेवा ठहराव बनाने/संगत गतिविधि जीवन चक्र तरीकों से रोकने के लिए कुछ संगीत
Intent intent = new Intent(MusicService.ACTION_CMD, fileUrlToPlay, activity, MusicService::class.java)
intent.putParcelableArrayListExtra(MusicService.ARG_QUEUE, tracks)
intent.putExtra(MusicService.ARG_INDEX, position)
intent.putExtra(MusicService.CMD_NAME, MusicService.CMD_PLAY)
activity.startService(intent)
खेलने के लिए किसी भी गतिविधि से कहा जा सकता है।
यहाँ एक अच्छा ट्यूटोरियल के बारे में Playing music in the background on Android
स्रोत
2016-11-08 15:07:37
धन्यवाद है, कि वास्तव में मुझे मदद की! :) – rohit
आपने सेवा को प्राथमिकता क्यों दी है जिसे नियंत्रित करना है। ? – rohit
धन्यवाद! ठीक काम करता है लेकिन मुझे इस code.in के साथ कोई समस्या है। मेरे आवेदन में मेरे पास कई गतिविधियां हैं लेकिन मैंने ऑन() में फिनिश() को घोषित किया है।मैं इरादों के माध्यम से बीजीएम खेलना चाहता हूं लेकिन इरादा कॉल पर रोक लगाता है और सेवा बंद हो जाती है (ऑनपोज़ विधि में सीज़ फिनिश विधि)। क्या आपके पास कोई समाधान है? – Hassy31