我是一个初学者,我试图向用户显示一些状态栏通知。我发现API23 +不支持setLatestInfo方法,因此我的编译器将其视为错误。我应该使用其他什么方法代替它来打开通知?
import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;
import androidx.annotation.RequiresApi;
import static android.app.PendingIntent.getActivity;
import static android.content.Context.NOTIFICATION_SERVICE;
public class Settings extends AppCompatActivity {
Switch simpleswitch1;
Switch simpleswitch2;
private Notification notification;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
simpleswitch1 = (Switch) findViewById(R.id.simpleswitch1);
simpleswitch2 = (Switch) findViewById(R.id.simpleswitch2);
simpleswitch1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Notify("Title", "Message");
}
});
simpleswitch2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
notification.defaults |= Notification.DEFAULT_SOUND;
}});}
private void Notify(String notificationTitle, String notificationMessage) {
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.notification, "New message", System.currentTimeMillis());
Intent notificationIntent = new Intent(Settings.this, Settings.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(Settings.this, notificationTitle, notificationMessage, pendingIntent);
notificationManager.notify(9999, notification);
}
}
答案 0 :(得分:0)
g
已被弃用,已在api 23中删除。因此,如果您将SDK版本设置为api 23+,则会出现此问题,应使用 NotificationCompat。 。
一个例子:
setLatestEventInfo
我建议阅读有关通知here的官方文档。