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

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 - 设置最新事件信息,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43205061/

10-09 07:16
查看更多