#java #android #alarmmanager #alarm
#java #Android #alarmmanager #тревога
Вопрос:
Я создаю приложение-напоминание, но не могу разрешить метод setLatestEventInfo
метода.
public class NotifyService extends Service {
public class ServiceBinder extends Binder {
NotifyService getService() {
return NotifyService.this;
}
}
private static final int NOTIFICATION = 123;
public static final String INTENT_NOTIFY = "com.example.seng.healthyapp.INTENT_NOTIFY";
private NotificationManager mNM;
@Override
public void onCreate() {
Log.i("NotifyService", "onCreate()");
mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("LocalService", "Received start id " startId ": " intent);
if (intent.getBooleanExtra(INTENT_NOTIFY, false))
showNotification();
return START_NOT_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
private final IBinder mBinder = new ServiceBinder();
private void showNotification() {
CharSequence title = "Alarm!!";
int icon = R.drawable.ic_dialog_alert;
CharSequence text = "Your notification time is upon us.";
long time = System.currentTimeMillis();
Notification notification = new Notification(icon, text, time);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, SecondActivity.class), 0);
notification.setLatestEventInfo(this, title, text, contentIntent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
mNM.notify(NOTIFICATION, notification);
stopSelf();
}
}
Редактировать
public class NotifyService extends Service {
public class ServiceBinder extends Binder {
NotifyService getService() {
return NotifyService.this;
}
}
private static final int NOTIFICATION = 123;
public static final String INTENT_NOTIFY = "com.example.seng.healthyapp.INTENT_NOTIFY";
private NotificationManager mNM;
@Override
public void onCreate() {
Log.i("NotifyService", "onCreate()");
mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("LocalService", "Received start id " startId ": " intent);
if(intent.getBooleanExtra(INTENT_NOTIFY, false))
showNotification();
return START_NOT_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
private final IBinder mBinder = new ServiceBinder();
private void showNotification() {
CharSequence title = "Alarm!!";
int icon = R.drawable.ic_dialog_alert;
CharSequence text = "Your notification time is upon us.";
long time = System.currentTimeMillis();
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, SecondActivity.class), 0);
Notification.Builder builder = new Notification.Builder(this)
.setSmallIcon(icon)
.setContentTitle(title)
.setContentText(text)
.setContentIntent(contentIntent);
Notification notification = builder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
mNM.notify(NOTIFICATION, notification);
stopSelf();
}
}
Ответ №1:
‘setLatestInfo` был удален в API 23: https://developer.android.com/sdk/api_diff/23/changes/android.app.Notification.html
Вместо этого вы должны использовать Notification.Builder
: https://developer.android.com/reference/android/app/Notification .Builder.html
Здесь есть пример.
Я предполагаю, что вы используете устаревший учебник по Android. Попробуйте использовать те, из https://developer.android.com . Они самые надежные.
Комментарии:
1. хе, спасибо за всю информацию. Я отредактировал свой код на основе приведенного вами примера. Можете ли вы помочь мне проверить, правильно ли я их реализовал?
2.
long
время никогда не используется3. Для чего вы хотите его использовать?
4. Я не уверен, что это из-за
time
. Я следую всему из этого руководства , но я не получал никаких уведомлений.5. Как я уже сказал, попробуйте вместо этого использовать учебник, на который я вас ссылал, а затем выполнить поиск на веб-сайте по любым другим возникающим у вас вопросам, вместо использования устаревшего примера.