我创建了一些notification.java 类。我想通过调用另一个类来使其灵活。当我尝试运行它时,它显示一个错误“尝试调用虚拟方法”。
这是我在 MainActivity.java 上的代码
Notification notification;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
notification.createNotificationChannel("Primary", "Timer Notification", "Notif when Timer is done");
} catch (Exception e) {
e.printStackTrace();
}
这是我在 Notification.java 上的代码
public class Notification extends AppCompatActivity {
private static final String PRIMARY_CHANNEL_ID = "primary_notif_channel";
private NotificationManager mNotifyManager;
public void sendNotification(int idNotif, String msgTitle, String msgText) {
// Sets up the pending intent to update the notification.
// Corresponds to a press of the Update Me! button.
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent notificationPendingIntent = PendingIntent.getActivity(this,
idNotif, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// Build the notification with all of the parameters using helper
// method.
NotificationCompat.Builder notifyBuilder = getNotificationBuilder(idNotif, msgTitle, msgText);
// Deliver the notification.
mNotifyManager.notify(idNotif, notifyBuilder.build());
}
public void createNotificationChannel(String idChannel, String nameChannel, String channelDesc) {
// Create a notification manager object.
mNotifyManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Notification channels are only available in OREO and higher.
// So, add a check on SDK version.
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
// Create the NotificationChannel with all the parameters.
NotificationChannel notificationChannel = new NotificationChannel
(idChannel, nameChannel, NotificationManager.IMPORTANCE_HIGH);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setDescription(channelDesc);
mNotifyManager.createNotificationChannel(notificationChannel);
}
}
private NotificationCompat.Builder getNotificationBuilder(int builderID, String builderTitle, String builderText) {
// Set up the pending intent that is delivered when the notification
// is clicked.
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent notificationPendingIntent = PendingIntent.getActivity
(this, builderID, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// Build the notification with all of the parameters.
NotificationCompat.Builder notifyBuilder = new NotificationCompat
.Builder(this, PRIMARY_CHANNEL_ID)
.setContentTitle(builderTitle)
.setContentText(builderText)
.setSmallIcon(R.drawable.ic_bell)
.setAutoCancel(true)
.setContentIntent(notificationPendingIntent)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setDefaults(NotificationCompat.DEFAULT_ALL);
return notifyBuilder;
}
我是 Java 和 Android 新手,所以请告诉我是否有任何代码在通知中出错。