我创建了一个在指定时间发出警报的应用程序。 但我也想向它发送带有预定义文本的通知。
这当前包含在MainActivity.java中:
package com.example.reminder;
import androidx.appcompat.app.AppCompatActivity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.TimePicker;
import android.widget.Toast;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
TimePicker timePicker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timePicker=(TimePicker) findViewById(R.id.timePicker);
timePicker.setIs24HourView(true);
findViewById(R.id.buttonSetAlarm).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Calendar calendar = Calendar.getInstance();
if(Build.VERSION.SDK_INT > 23) {
calendar.set(
calendar.get(Calendar.YEAR),
calendar.get(calendar.MONTH),
calendar.get(calendar.DAY_OF_MONTH),
timePicker.getHour(),
timePicker.getMinute(),
0
);
}else {
calendar.set(
calendar.get(Calendar.YEAR),
calendar.get(calendar.MONTH),
calendar.get(calendar.DAY_OF_MONTH),
timePicker.getCurrentHour(),
timePicker.getCurrentMinute(),
0
);
}
setAlarm(calendar.getTimeInMillis());
}
});
}
private void setAlarm(long timeInMillis) {
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, MyAlarm.class);
PendingIntent pendingIntent= PendingIntent.getBroadcast(this, 0, intent, 0);
alarmManager.setRepeating(AlarmManager.RTC, timeInMillis, AlarmManager.INTERVAL_DAY, pendingIntent);
Toast.makeText(this, "Emlekezteto beallitva", Toast.LENGTH_SHORT).show();
}
我该如何编写以发送带有预定义文本的通知并使用声音?
当前,我在一个单独的类(MyAlarm)中编写了使用系统铃声的信息。 MyAlarm.java:
package com.example.reminder;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.provider.Settings;
public class MyAlarm extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
MediaPlayer mediaPlayer = MediaPlayer.create(context, Settings.System.DEFAULT_RINGTONE_URI);
mediaPlayer.start();
}
}
有人可以帮我吗?
谢谢。
答案 0 :(得分:0)
我在项目中使用了此功能,效果很好。希望对您有帮助
private void notificationDialog() {
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = "notification01";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
@SuppressLint("WrongConstant") NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_MAX);
// Configure the notification channel.
notificationChannel.setDescription("Description");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
notificationBuilder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setTicker("some text")
//.setPriority(Notification.PRIORITY_MAX)
.setContentTitle("ContentTitle")
.setContentText("ContentText")
.setContentInfo("Information");
notificationManager.notify(1, notificationBuilder.build());
}
在您的用于发送通知的项目中,像这样更改您的MyAlarm类
package com.example.reminder;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.provider.Settings;
public class MyAlarm extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
notificationDialog();
}
private void notificationDialog() {
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = "notification01";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
@SuppressLint("WrongConstant") NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_MAX);
// Configure the notification channel.
notificationChannel.setDescription("Description");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
notificationBuilder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setTicker("some text")
//.setPriority(Notification.PRIORITY_MAX)
.setContentTitle("ContentTitle")
.setContentText("ContentText")
.setContentInfo("Information");
notificationManager.notify(1, notificationBuilder.build());
}
}
答案 1 :(得分:0)
浏览此Medium Article可以更好地了解通知渠道。