在Android Studio中无法解决setLatestEventInfo()问题

4
我正在使用Android Studio工作,尝试在特定日期和时间生成通知。所有事情都往好的方向发展,但是,在我的Service类中,setLatestEventInfo()方法无法解决。 我已经在eclipse中完成了相同的演示,没有任何问题。 我不想在任何按钮点击或任何手动事件生成上生成通知,而是在我指定的特定日期和时间生成通知。
Service类的代码如下:
public class MyRemiderService extends Service {
private NotificationManager mManager;

@Override
public IBinder onBind(Intent intent) {

    return null;
}

@Override
public void onCreate() {

    super.onCreate();
}

@Override
@Deprecated
public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);
    mManager = (NotificationManager) getApplicationContext()
            .getSystemService(getApplicationContext().NOTIFICATION_SERVICE);
    Intent intent1 = new Intent(this.getApplicationContext(),
            HomeActivity.class);
    Notification notification = new Notification(R.drawable.notification_template_icon_bg,
            "This is a test message!", System.currentTimeMillis());
    intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
            | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingNotificationIntent = PendingIntent.getActivity(
            this.getApplicationContext(), 0, intent1,
            PendingIntent.FLAG_UPDATE_CURRENT);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.setLatestEventInfo(this.getApplicationContext(),
            "AlarmManagerDemo", "This is a test message!",
            pendingNotificationIntent);


    mManager.notify(0, notification);
}

@Override
public void onDestroy() {

    super.onDestroy();
}

请让我提供关于它的解决方案。谢谢。


哦哦..有没有其他的替代方案? - Jaimin Modi
2个回答

10

如下所示setLatestEventInfo

从Notification类中删除了setLatestEventInfo方法。

要创建Notification,请使用Notification.Builder类:

Notification.Builder builder = new Notification.Builder(MyRemiderService.this);
.....
builder.setSmallIcon(R.drawable. notification_template_icon_bg)
       .setContentTitle("ContentTitle")
       .....
       .setContentIntent(pendingNotificationIntent);

Notification notification = builder.getNotification();
notificationManager.notify(R.drawable.notification_template_icon_bg, notification);

2
Notification.Builder builder = new Notification.Builder(MainActivity.this);
Intent notificationIntent = new Intent(this,MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,notificationIntent, 0);
builder.setSmallIcon(R.drawable. notification_template_icon_bg)
        .setContentTitle("Music player")
        .setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = builder.getNotification();
notificationManager.notify(R.drawable.notification_template_icon_bg, notification);

1
请检查此内容是否适用于23+ SDK版本。 - pradip bhuva

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接