gpt4 book ai didi

java - 无法解析方法 `setLatestEventInfo`

转载 作者:行者123 更新时间:2023-12-01 09:26:06 26 4
gpt4 key购买 nike

我制作了提醒应用程序,但无法解析方法 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();
}
}

最佳答案

“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

有一个例子here .

我假设您使用的是过时的 Android 教程。尝试使用https://developer.android.com中的那些。他们是最可靠的。

关于java - 无法解析方法 `setLatestEventInfo`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39819659/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com