2010-10-19 14 views
24

जब भी मेरा एप्लिकेशन चल रहा है, तब भी मैं स्टेटस बार में एक आइकन रखना चाहता हूं, जिसमें पृष्ठभूमि में चलने पर भी शामिल है। मैं यह कैसे कर सकता हूँ?पृष्ठभूमि में शामिल होने पर, एप्लिकेशन बार चलने पर स्टेटस बार में आइकन कैसे दिखाया जाए?

उत्तर

18

आप अधिसूचना और NotificationManager के साथ ऐसा करने में सक्षम होना चाहिए। हालांकि यह जानने के लिए एक गारंटीकृत तरीका प्राप्त करना कि आपका एप्लिकेशन कब नहीं चल रहा है वह कठिन हिस्सा है।

Notification notification = new Notification(R.drawable.your_app_icon, 
              R.string.name_of_your_app, 
              System.currentTimeMillis()); 
notification.flags |= Notification.FLAG_NO_CLEAR 
        | Notification.FLAG_ONGOING_EVENT; 
NotificationManager notifier = (NotificationManager) 
    context.getSystemService(Context.NOTIFICATION_SERVICE); 
notifier.notify(1, notification); 

इस कोड को कहीं न कहीं होना चाहिए जहां आप वाकई निकाल दिया जब आपके आवेदन शुरू होता है हो जाएगा रहे हैं:

आप क्या आप की तरह कुछ कर रही द्वारा इच्छा कर रहे हैं की आधारभूत कार्यक्षमता प्राप्त कर सकते हैं। संभवतः आपके एप्लिकेशन के कस्टम एप्लिकेशन ऑब्जेक्ट की ऑनक्रेट() विधि में।

हालांकि उन चीजों के बाद मुश्किल है। आवेदन की हत्या किसी भी समय हो सकती है। तो आप एप्लिकेशन क्लास के ऑन टर्मिन() में कुछ डालने का प्रयास कर सकते हैं, लेकिन इसे कॉल करने की गारंटी नहीं है।

((NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE)).cancel(1); 

आइकन को हटाने के लिए आवश्यक होगा।

+5

'अधिसूचना' को एपीआई स्तर 11 में बहिष्कृत किया गया था 11. इसके बजाय अधिसूचना का उपयोग करें। बिल्डर का उपयोग करें। –

+0

यदि आप 'सेवा' के अतिरिक्त उपयोग करते हैं, तो आपको नोटिफिकेशन मैन्युअल रूप से हटाने की आवश्यकता नहीं है।जब सेवा बंद हो जाती है, अधिसूचना स्वचालित रूप से हटा दी जाएगी। ("हालांकि, अगर आप अभी भी अग्रभूमि में चल रहे हैं, तो सेवा को रोकते हैं, तो अधिसूचना भी हटा दी जाती है।" Https://developer.android.com/guide/components/services.html#Foreground) –

6

देव गाइड "Creating Status Bar Notifications" पर एक नज़र डालें।

एक तरह से प्रतीक केवल जब आवेदन चल रहा है रखने के लक्ष्य को प्राप्त करने के onCreate() में अधिसूचना को प्रारंभ करने और अपने onPause() विधि केवल यदि isFinishing() रिटर्न सच में cancel(int) कॉल करने के लिए है।

एक उदाहरण:

private static final int NOTIFICATION_EX = 1; 
private NotificationManager notificationManager; 

@Override 
public void onCreate() { 
    super.onCreate(); 

    notificationManager = (NotificationManager) 
     getSystemService(Context.NOTIFICATION_SERVICE); 

    int icon = R.drawable.notification_icon; 
    CharSequence tickerText = "Hello"; 
    long when = System.currentTimeMillis(); 

    Notification notification = new Notification(icon, tickerText, when); 

    Context context = getApplicationContext(); 
    CharSequence contentTitle = "My notification"; 
    CharSequence contentText = "Hello World!"; 
    Intent notificationIntent = new Intent(this, MyClass.class); 
    PendingIntent contentIntent = PendingIntent.getActivity(this, 
     0, notificationIntent, 0); 

    notification.setLatestEventInfo(context, contentTitle, 
     contentText, contentIntent); 

    notificationManager.notify(NOTIFICATION_EX, notification); 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    if (isFinishing()) { 
     notificationManager.cancel(NOTIFICATION_EX); 
    } 
} 
+0

यह आपको केवल तभी दिखाएगा जब कोई दी गई गतिविधि चल रही हो। यदि एप्लिकेशन अभी भी चल रहा है। –

+0

@ ग्रेग: ठीक है, यह उदाहरण कोड केवल एक-गतिविधि अनुप्रयोग को सही तरीके से संभालता है। यह निर्धारित करना मुश्किल है कि सिस्टम ने एप्लिकेशन को कब मार दिया है। जैसा कि आपने बताया है, एक कस्टम 'एप्लिकेशन' कक्षा शायद कोड के लिए सबसे अच्छी जगह है क्योंकि यह एप्लिकेशन के पूरे जीवन में बनी रहती है। अधिसूचना को साफ़ करना मुश्किल होगा क्योंकि सिस्टम मनमाने ढंग से कम स्मृति की शर्तों के तहत ऐप की प्रक्रिया को मारने का फैसला कर सकता है। आप स्थिति की निगरानी करने के लिए एक सेवा बनाने का प्रयास कर सकते हैं क्योंकि सिस्टम बाद में सेवा को पुनरारंभ करने का प्रयास करेगा जब अधिक मेमोरी उपलब्ध हो। – Brian

+0

यदि आप 'नोटिस' से 'अधिसूचना प्रबंधक कैनसेल (NOTIFICATION_EX) हटाते हैं, तो अधिसूचना तब तक अधिसूचना बार में रहेगी जब तक कि एप्लिकेशन पूरी तरह से बंद नहीं हो जाता। –

8

नई एपीआई के लिए आप NotificationCompat.Builder उपयोग कर सकते हैं -

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) 
    .setSmallIcon(R.mipmap.ic_launcher) 
    .setContentTitle("Title"); 
Intent resultIntent = new Intent(this, MyActivity.class); 
PendingIntent resultPendingIntent = PendingIntent.getActivity(
this, 
0, 
resultIntent, 
PendingIntent.FLAG_UPDATE_CURRENT); 
mBuilder.setContentIntent(resultPendingIntent); 
Notification notification = mBuilder.build(); 
notification.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT; 

mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
mNotifyMgr.notify(NOTIFICATION_ID, notification); 

यह जब तक आपके आवेदन चल रहा है और किसी को मैन्युअल रूप से अपने आवेदन बंद कर देता है दिखाएगा। आप हमेशा कॉल करके

mNotifyMgr.cancel(NOTIFICATION_ID); 
+0

क्या होगा MyActivity.class की सामग्री हो? @mjosh –

+0

@Sarahcartenz आप वास्तव में कुछ भी चाहते हैं – mjosh

+0

यदि मैं ऐसी कक्षा को परिभाषित नहीं करता हूं तो क्या होगा? या इसे खाली रखें। उदाहरण के लिए अधिसूचना दबाए जाने के बाद मुझे कोई कार्रवाई नहीं चाहिए। –

3

यह वास्तव में काम करता है। मैं ऊपर के उदाहरण के बाहर एक विधि बनाया:

private void applyStatusBar(String iconTitle, int notificationId) { 
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) 
.setSmallIcon(R.mipmap.ic_launcher) 
.setContentTitle(iconTitle); 
Intent resultIntent = new Intent(this, ActMain.class); 
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
mBuilder.setContentIntent(resultPendingIntent); 
Notification notification = mBuilder.build(); 
notification.flags |= Notification.FLAG_NO_CLEAR|Notification.FLAG_ONGOING_EVENT; 

NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
mNotifyMgr.notify(notificationId, notification);} 

ऐसा लगता है बुलाया जाना चाहिए: ("स्थिति बार टेस्ट", 10) applyStatusBar;

+0

यदि मैं पूछ सकता हूं, ActMain.class की सामग्री क्या होगी? –

संबंधित मुद्दे