gpt4 book ai didi

java - 如何在 API 23+ 中解析 setLatestEventInfo

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

我正在创建一个模拟推送通知应用程序。它从用户那里获取输入并在设备上显示本地推送通知。据我所知,setLatestEventInfo 方法在新的 API (23+) 级别中已停用。我想知道代码的可能补丁是什么。下面是代码:

public class MainActivity extends ActionBarActivity {
EditText ed1,ed2,ed3;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ed1=(EditText)findViewById(R.id.editText);
ed2=(EditText)findViewById(R.id.editText2);
ed3=(EditText)findViewById(R.id.editText3);
Button b1=(Button)findViewById(R.id.button);

b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String tittle=ed1.getText().toString().trim();
String subject=ed2.getText().toString().trim();
String body=ed3.getText().toString().trim();

NotificationManager notif=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Notification notify=new Notification(R.drawable.icon,tittle,System.currentTimeMillis());
PendingIntent pending= PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0);

notify.setLatestEventInfo(getApplicationContext(),subject,body,pending); //cannot resolve 'setLatestEventInfo' method
notif.notify(0, notify);
}
});
}

谢谢你:)

最佳答案

使用 NotificationCompat.Builder,以及 setSmallIcon()setTicker()setContentTitle()setContentText()setContentIntent()

例如,这个方法来自this sample project来自 this book使用以上所有内容:

  private void raiseNotification(String mimeType, File output,
Exception e) {
NotificationCompat.Builder b=new NotificationCompat.Builder(this);

b.setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL);

if (e == null) {
b.setContentTitle(getString(R.string.download_complete))
.setContentText(getString(R.string.fun))
.setSmallIcon(android.R.drawable.stat_sys_download_done)
.setTicker(getString(R.string.download_complete));

Intent outbound=new Intent(Intent.ACTION_VIEW);

outbound.setDataAndType(Uri.fromFile(output), mimeType);

b.setContentIntent(PendingIntent.getActivity(this, 0, outbound, 0));
}
else {
b.setContentTitle(getString(R.string.exception))
.setContentText(e.getMessage())
.setSmallIcon(android.R.drawable.stat_notify_error)
.setTicker(getString(R.string.exception));
}

NotificationManager mgr=
(NotificationManager)getSystemService(NOTIFICATION_SERVICE);

mgr.notify(NOTIFY_ID, b.build());
}
}

关于java - 如何在 API 23+ 中解析 setLatestEventInfo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33323450/

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