无法解析setLatestEventInfo方法

时间:2017-07-11 18:54:15

标签: java android android-notifications

我最近开发了一款应用,但Android Studio不再支持setLatestEventInfo方法,因此,我尝试将此方法更改为Notification.Builder方法。但是,我很挣扎,因此我希望有人支持我将此方法更改为Notification.Builder方法。

String ns = Context.NOTIFICATION_SERVICE;
        mNotificationManager = (NotificationManager) getSystemService(ns);

        int icon = R.drawable.ic_launcher;
        CharSequence tickerText = "Fall Detector";
        long when = System.currentTimeMillis();
        notification = new Notification(icon, tickerText, when);


        mContext = getApplicationContext();
        CharSequence contentTitle = "Fall Detector";
        CharSequence contentText = "Running";
        Intent notificationIntent = new Intent(this,StartActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                notificationIntent, 0);
        notification.setLatestEventInfo(mContext, contentTitle, contentText,
                contentIntent);


        mNotificationManager.notify(1, notification);

我已尝试过以下内容,但是,它说它无法解析符号contentintent。

String ns = Context.NOTIFICATION_SERVICE;
        int icon = R.drawable.ic_launcher;
        CharSequence tickerText = "Fall Detector";
        long when = System.currentTimeMillis();
        notification = new Notification(icon, tickerText, when);
        mNotificationManager = (NotificationManager) getSystemService(ns);

        Notification.Builder builder = new Notification.Builder(mContext)

                .setContentTitle("Fall Detector")
                .setContentText("Running")
                .setContentIntent(contentIntent);
        builder.build();
        Notification notification = builder.getNotification();

        mNotificationManager.notify(1, notification);

1 个答案:

答案 0 :(得分:0)

  

我已尝试过以下内容,但是,它说它无法解析符号contentintent。

这是因为您未能将原始代码中的某些行复制到修订后的代码中:

    Intent notificationIntent = new Intent(this,StartActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            notificationIntent, 0);

在开始配置Notification.Builder之前,请在修订后的代码中添加。