У мене є додаток, я хочу показати значок програмина панелі сповіщень, коли моя програма працює, і я також хочу, коли користувач натискатиме значок мого додатка на панелі сповіщень моє додаток буде відкрито. Як це зробити? Будь ласка, допоможіть!
Відповіді:
14 за відповідь № 1Щоб створити сповіщення про смугу стану, виконайте це за допомогою методу onCreate:
Отримати посилання на NotificationManager:
String ns = Context.NOTIFICATION_SERVICE; NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
Ініціювати сповіщення:
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:
private static final int HELLO_ID = 1; mNotificationManager.notify(HELLO_ID, notification);
Це все. Тепер ваш користувач отримав сповіщення.
7 для відповіді № 2
Прийнята відповідь застаріла. Ось спосіб показати діалогове вікно з Google документація.
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable
.logo_listy).setContentTitle("My notification").setContentText("Hello World!");
Intent resultIntent = new Intent(this, ResultActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(ResultActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
3 для відповіді № 3
Кілька пропозицій:
- якщо ви хочете значок на панелі сповіщень, ви повинні надіслати повідомлення.
- Заяву не можна запустити, натиснувши напіктограма сповіщення Його можна запустити, натиснувши на сповіщення, що буде доступним, якщо на панелі сповіщення випадає користувач. Для цього потрібно створити PendingIntent.
0 для відповіді № 4
Вам потрібно опублікувати повідомлення із очікуваним наміром, який містить намір почати додаток. Подивитися http://developer.android.com/guide/topics/ui/notifiers/notifications.html для того, як це робити взагалі і http://javablogs.com/Jump.action?id=628173 для пастки ви можете впасти.