【问题标题】:set Latest Event Info设置最新活动信息
【发布时间】:2017-08-29 12:36:59
【问题描述】:

我正在使用 android studio,我正在处理一部分代码,这部分代码来自我将其转换为 android studio 的 eclipse 项目。 问题出在:

notification.setLatestEventInfo(this, text,
        getText(R.string.notification_subtitle), contentIntent);

setLatestEventInfo是红色的,无法启动应用程序,如果解决这个问题怎么办?

/**
 * Show a notification while this service is running.
 */
private void showNotification() {
    CharSequence text = getText(R.string.app_name);
    Notification notification = new Notification(R.drawable.ic_notification, null,
            System.currentTimeMillis());
    notification.flags = Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;
    Intent pedometerIntent = new Intent();
    pedometerIntent.setComponent(new ComponentName(this, Pedometer.class));
    pedometerIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            pedometerIntent, 0);
    notification.setLatestEventInfo(this, text,
            getText(R.string.notification_subtitle), contentIntent);

    mNM.notify(R.string.app_name, notification);
}

这是错误:

Error:(375, 21) error: cannot find symbol method setLatestEventInfo(StepService,CharSequence,CharSequence,PendingIntent)

还有这个:

Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

【问题讨论】:

    标签: java android eclipse android-studio


    【解决方案1】:

    原因是 Google 自 API 23 起已删除此方法。有关更多信息,请阅读此处:Notification

    在将项目从 Eclipse 导入 Android Studio 时,目标必须从 23 以下更改为 23 或以上。因此,此方法将不再可用。

    尝试将您的通知代码更改为:

    Intent pedometerIntent = new Intent();
    pedometerIntent.setComponent(new ComponentName(this, Pedometer.class));
    pedometerIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            pedometerIntent, 0);
    
    CharSequence text = getText(R.string.app_name);
    Notification notification = new Notification.Builder(this)
            .setSmallIcon(R.drawable.ic_notification)
            .setShowWhen(true)
            .setContentTitle(text)
            .setContentText(getText(R.string.notification_subtitle))
            .setContentIntent(contentIntent)
            .build();
    
    notification.flags = Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;
    
    mNM.notify(R.string.app_name, notification);
    

    【讨论】:

    • 感谢您的回答,我不知道为什么,但我收到一个新错误 .setTicker(System.currentTimeMillis())Error:(375, 52) 错误:不兼容的类型:long 无法转换为 CharSequence 和股票代码错误在builder中不能申请
    • 新错误是 .setTicker(System.currentTimeMillis())Error:(375, 52) 错误:不兼容的类型:long 无法转换为 CharSequence 并且在代码错误是在构建器中无法应用
    猜你喜欢
    • 2015-01-26
    • 2020-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-16
    相关资源
    最近更新 更多