设置最新活动信息

时间:2017-04-04 10:47:54

标签: 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.

1 个答案:

答案 0 :(得分:2)

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