首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法解决方法“`setLatestEventInfo`”

无法解决方法“`setLatestEventInfo`”
EN

Stack Overflow用户
提问于 2016-10-02 17:27:58
回答 1查看 2.8K关注 0票数 0

我制作了提醒应用程序,但我无法解析方法setLatestEventInfo方法。

代码语言:javascript
复制
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();
    }
}

编辑

代码语言:javascript
复制
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();
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-02 17:37:54

‘'setLatestInfo`’在API 23:diff/23/changes/android.app.Notification.html中被删除

您应该使用Notification.Builderhttps://developer.android.com/reference/android/app/Notification.Builder.html

有一个例子,这里

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39819659

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档