gpt4 book ai didi

android - Android 上的通知管理器问题

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

我正在尝试使用按钮进行通知,但 Notification 和 setLatestEventInfo 均已弃用。

两个错误:

1. 构造函数 Notification(int, CharSequence, long) 被弃用 Notification notify = new Notification(android.R.drawable.stat_notify_more, "Hello all", System.currentTimeMillis());

2.Notification类型的方法setLatestEventInfo(Context, CharSequence, CharSequence, PendingIntent)不适用于参数(Context, CharSequence, CharSequence, Intent) notify.setLatestEventInfo(上下文、标题、详细信息、 Intent );

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NotificationManager ns = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notify = new Notification(android.R.drawable.stat_notify_more, "Hello all", System.currentTimeMillis());
Context context = MainActivity.this;
CharSequence title ="you have be notified";
CharSequence details = "Continue your work";
Intent intent = new Intent(context,MainActivity.class);
PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0);
notify.setLatestEventInfo(context, title, details, intent);
ns.notify(0,notify);


}
});
}

API 级别:

       android:minSdkVersion="11"
android:targetSdkVersion="17"

有什么选择?

最佳答案

<强>1。该构造函数在 API 级别 11 中已弃用。因此您应该使用 Notification.Builder

例如

Notification notification = new Notification.Builder(mContext)
.setContentTitle("New mail from " + sender.toString())
.setContentText(subject)
.setSmallIcon(R.drawable.new_mail)
.setLargeIcon(aBitmap)
.build();

<强>2。在您的代码中,您正在传递 Intent 而不是在 setLatestEventInfo 中挂起

....
Intent intent = new Intent(context,MainActivity.class);
PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0);
notify.setLatestEventInfo(context, title, details, pending);
ns.notify(0,notify);
....

关于android - Android 上的通知管理器问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15493633/

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