gpt4 book ai didi

android - 如何避免 `getNotification` 被弃用?

转载 作者:太空狗 更新时间:2023-10-29 15:59:46 25 4
gpt4 key购买 nike

我正在制作 Alarm 应用程序并在 MyAlarmService 中包含以下代码。

public class MyAlarmService extends Service
{
private NotificationManager mManager;

@Override
public IBinder onBind(Intent arg0)
{
// TODO Auto-generated method stub
return null;
}

@Override
public void onCreate()
{
// TODO Auto-generated method stub
super.onCreate();
}

@SuppressWarnings("static-access")
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
Notification.Builder notification = new Notification.Builder(MyAlarmService.this);
Intent intent1 = new Intent(this.getApplicationContext(),ReminderPage.class);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity( this.getApplicationContext(),0, intent1,0);
notification.setSmallIcon(R.drawable. notification_template_icon_bg)
.setContentTitle("Music player")
.setContentIntent(pendingNotificationIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification1 = notification.getNotification();
notificationManager.notify(R.drawable.notification_template_icon_bg, notification1);
mManager.notify(0, notification1);
return super.onStartCommand(intent,flags, startId);
}

@Override
public void onDestroy()
{
// TODO Auto-generated method stub
super.onDestroy();
}
  1. 在 onStart 方法中,它总是有一个删除线。 google 后,我将 onStart 方法更改为 onStartCommand(),但不确定这是不是正确的修改方法。
  2. getNotification 已弃用。

AndroidMainfest

 <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="10" />

谢谢。

最佳答案

你能试试这个吗,

NotificationCompat.Builder builder = new NotificationCompat.Builder(
context);
notification = builder.setContentIntent(intent)
.setSmallIcon(R.drawable.push_icon).setTicker(title).setWhen(when)
.setAutoCancel(true).setContentTitle(title)
.setContentText(message).build();
notificationManager.notify(requestID, notification);

关于android - 如何避免 `getNotification` 被弃用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39858078/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com