gpt4 book ai didi

java - 关于如何在通知接收器中更改 setLatestEventInfo 的替代方法

转载 作者:行者123 更新时间:2023-11-29 03:02:25 25 4
gpt4 key购买 nike

您好,我在这一点上有库存,其中“setLatesEventInfo”有错误。我知道 setLatestEventInfo 不会在 API 23 及更高版本上运行。有人可以帮助我如何运行这段代码吗?我的意思是替代方式,功能相同但编码不同。这是一个接收函数。我正在对我的 Mainactivity 进行通知。这里唯一的错误是接收器,我尝试了构建器通知,但由于这是接收器,构建器无法申请

接收器.java

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

/**
* Created by my pc on 12/1/2015.
*/
public class Receiver extends BroadcastReceiver {

private static final Object ACTION = "MyNotification";

@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ACTION.equals(action)) {
//do what you want here
Variables.numMessages++;
generateNotification(context,"MyNotification");
}
}

private void generateNotification(Context context, String message) {
long when = System.currentTimeMillis();
int icon = R.drawable.ic_od_icon_24dp;
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
// String subTitle = context.getString(R.string.app_name);
String subTitle = "some text";
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.putExtra("content", message);
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);

notification.setLatestEventInfo(context, title, subTitle, intent);
//To play the default sound with your notification:
//notification.defaults |= Notification.DEFAULT_SOUND;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}

}

主 Activity .java

public class MainActivity extends Activity {

int NOTIFICATION_ID = 1;
int LED_ON_MS = 200;
int LED_OFF_MS = 600;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onGenNoti(View v) {

// Check readCount
Variables.readCount = Variables.numMessages;

PendingIntent pIntent =
PendingIntent.getActivity(MainActivity.this, 0, new Intent(MainActivity.this, MainActivity.class), 0);

if (Variables.numMessages > 0) {
Notification noti = new Notification.Builder(MainActivity.this)
.setTicker("You Have " + Variables.numMessages + " message")
.setContentTitle("test")
.setContentText("")
.setSmallIcon(R.drawable.ic_od_icon_24dp)
.setContentIntent(pIntent).getNotification();
noti.defaults |= Notification.DEFAULT_SOUND;
noti.defaults |= Notification.DEFAULT_VIBRATE;
noti.ledARGB = Color.BLUE;
noti.ledOnMS = LED_ON_MS;
noti.ledOffMS = LED_OFF_MS;
noti.flags = Notification.FLAG_SHOW_LIGHTS;
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.notify(NOTIFICATION_ID, noti);
}
}
}

最佳答案

现在你必须像这样生成你的通知

    NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(context);

// set intent so it does not start a new activity
Notification notification = builder
.setContentIntent(yourPendingIntent)
.setSmallIcon(icon)
.setWhen( System.currentTimeMillis();)
.setContentTitle(title)
.setContentText(message).build();

notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(notifId, notification);

关于java - 关于如何在通知接收器中更改 setLatestEventInfo 的替代方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34012544/

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