java - 错误 : cannot find symbol method setLatestEventInfo(Context, CharSequence、CharSequence、PendingIntent)

标签 java android

我无法使用 API 26 更新应用。 当我尝试运行项目时出现以下错误

错误:

"ServerRunningNotification.java": error: cannot find symbol method setLatestEventInfo(Context,CharSequence,CharSequence,PendingIntent)

这是我的代码 fragment ,有错误

代码:

public class ServerRunningNotification extends BroadcastReceiver {
    private static final String TAG = ServerRunningNotification.class.getSimpleName();

    private final int NOTIFICATIONID = 7890;
    public String iptext;
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "onReceive broadcast: " + intent.getAction());
        if (intent.getAction().equals(FtpServerService.ACTION_STARTED)) {
            setupNotification(context);
        } else if (intent.getAction().equals(FtpServerService.ACTION_STOPPED)) {
            clearNotification(context);
        }
    }

    @SuppressWarnings("deprecation")
    private void setupNotification(Context context) {
        Log.d(TAG, "Setting up the notification");
        // Get NotificationManager reference
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager nm = (NotificationManager) context.getSystemService(ns);

        // get ip address
        InetAddress address = FtpServerService.getLocalInetAddress();
        if (address == null) {
            Log.w(TAG, "Unable to retreive the local ip address");
            return;
        }
         iptext = "ftp://" + address.getHostAddress() + ":"
                + Settings.getPortNumber() + "/";

        // Instantiate a Notification
        int icon = R.drawable.ftp_icon;
        CharSequence tickerText = String.format(
                context.getString(R.string.notif_server_starting), iptext);
        long when = System.currentTimeMillis();
        Notification notification = new Notification(icon, tickerText, when);

        // Define Notification's message and Intent
        CharSequence contentTitle = context.getString(R.string.notif_title);
        CharSequence contentText = String.format(context.getString(R.string.notif_text),
                iptext);

        Intent notificationIntent = new Intent(context, FTP_Start_Stop.class);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
                notificationIntent, 0);
        notification
                .setLatestEventInfo(context, contentTitle, contentText, contentIntent);
        notification.flags |= Notification.FLAG_ONGOING_EVENT;

        // Pass Notification to NotificationManager
        nm.notify(NOTIFICATIONID, notification);

        Log.d(TAG, "Notication setup done");
    }

    private void clearNotification(Context context) {
        Log.d(TAG, "Clearing the notifications");
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager nm = (NotificationManager) context.getSystemService(ns);
        nm.cancelAll();
        Log.d(TAG, "Cleared notification");
    }
}

最佳答案

根据heresetLatestEventInfo(Context,CharSequence,CharSequence,PendingIntent) 方法已在 Android M (API 23) 中删除。因此它不适用于 23 以上的 API 版本。

关于java - 错误 : cannot find symbol method setLatestEventInfo(Context, CharSequence、CharSequence、PendingIntent),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52713037/

相关文章:

java - 是否可以从 MQ 队列中删除消息而不检索消息或清除队列?

java - JDBC 和 Hive 查询错误 10 或 9

java - 在java中将十进制数转换为ascii

android - 在 MaterialCardView 中更改拐角半径的问题

java - 制作一个迭代的多线程方法

java - 如何使用 while 循环让骰子重新掷骰子,直到它达到 "point"(骰子)?

JAVA展示13套

android - 在 Cards UI ListView 中突出显示整个项目

android - 在 Android Fabric/Crashlytics 的 proguard 构建上禁用映射文件 (deob) 上传

java - 在 Eclipse 中打开一个项目