मैं वर्तमान में एक अधिसूचना के साथ एक अग्रभूमि सेवा बना रहा हूं जो सेवा शुरू होने पर अधिसूचना बार में दिखाई देता है। अगर सेवा बंद हो जाती है, अधिसूचना खारिज कर देती है। मेरा सवाल यह है कि, "सभी अधिसूचनाएं साफ़ करें" या अधिसूचना (स्वाइप) की खारिज होने पर सेवा को रोकने का कोई तरीका है?अधिसूचना (स्वाइप खोलने) का उपयोग करने से सभी अग्रभूमि सेवा को रद्द करने या सभी अधिसूचनाओं को साफ़ करने के लिए कैसे?
public int onStartCommand(Intent intent, int flags, int startId)
{
Log.d(CLASS, "onStartCommand service started.");
if (getString(R.string.service_start_action) == intent.getAction())
{
Intent intentForeground = new Intent(this, ServiceMain.class)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendIntent = PendingIntent.getActivity(getApplicationContext(), 0, intentForeground, 0);
Notification notification;
Notification.Builder builder = new Notification.Builder(getApplicationContext())
.setSmallIcon(android.R.drawable.btn_star)
.setTicker("Service started...")
.setContentIntent(pendIntent)
.setDefaults(Notification.DEFAULT_ALL)
.setOnlyAlertOnce(true)
.setOngoing(false);
notification = builder.build();
notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;
startForeground(SERV_NOTIFY, notification);
player.start();
}
else
{
Log.d(CLASS, "onStartCommand unable to identify acition.");
}
return START_STICKY;
}
Google से इस नमूने को देखें: https://github.com/googlesamples/android-ActiveNotifications। यह दिखाता है कि एक खारिज अधिसूचना के साथ-साथ अधिसूचनाओं के पूरे समूह को कैसे सुनें। –
जहां तक मुझे पता है कि आप अधिसूचना बनाये बिना अग्रभूमि सेवा नहीं बना सकते हैं। एंड्रॉइड की आवश्यकता है क्योंकि वे नहीं चाहते हैं कि आप दृश्य के पीछे सेवाओं को चलाने के बिना उपयोगकर्ता को जानते हों। आपकी गतिविधि नष्ट होने पर अधिसूचना को हटाया जा रहा है और आपकी सेवा अग्रभूमि में चल रही एक अधिसूचना वारंट करती है, आप बस इसे रद्द नहीं कर सकते हैं। अधिसूचना रद्द करना केवल '.stopForeground (true)' के माध्यम से अग्रभूमि सेवा को रद्द करके ही किया जा सकता है, बूलियन पैरामीटर इंगित करता है कि क्या आप अधिसूचना के साथ अग्रभूमि को रोकना चाहते हैं। –