java - 设置最新 Activity 信息

标签 java android eclipse android-studio

我正在使用android studio,我正在处理一部分代码,这部分代码来自一个eclipse项目,我将其隐藏到android studio。 问题在于:

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.

最佳答案

原因是 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);

关于java - 设置最新 Activity 信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43205061/

相关文章:

java - 是否可以在 Eclipse 中定义一组断点并仅在给定事件发生后触发它们?

java - 为 Hadoop 生成源代码时出现 Maven 错误

java - docx4j 将 docx 转换为错误的 html 格式

java - JPopupMenu 菜单不出现

android - 在 onBackStackChanged() 中调用时,SDK 25.1.0 在 CommitNow 上崩溃。谷歌错误?

android - 在 Android 的一个 Angular 上将半径边框绘制到 imageview 或 textview

eclipse - 有人知道 APEX 重构工具吗?

java - 将一维数组转换为 9 列的二维数组

Java - 在子类中找不到符号构造函数

php - 我需要在服务器端更改什么才能启用 chunkedMode = true?