我想在我的应用中添加通知。 我希望它每天在特定时间显示。 我试图用.setWhen做到这一点,但它不起作用。 我还想设置振动和声音,但这是行不通的。 通知立即显示。
public class Activity_addMedicine extends AppCompatActivity implements TimePickerDialog.OnTimeSetListener {
private NotificationManagerCompat notificationManager;
public static long time;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_medicine);
notificationManager = NotificationManagerCompat.from(this);
Button buttonOK = (Button) findViewById(R.id.button_OK2);
buttonOK.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment timePicker = new TimePicker();
timePicker.show(getSupportFragmentManager(), "time picker");
}
});
}
@Override
public void onTimeSet(android.widget.TimePicker view, int hourOfDay, int minute) {
Calendar c = getInstance();
c.set(Calendar.HOUR_OF_DAY, hourOfDay);
c.set(Calendar.MINUTE, minute);
c.set(Calendar.SECOND, 0);
time = c.getTimeInMillis();
}
public void sendOnChannel1(View view) {
Notification notification = new NotificationCompat.Builder(this, App.CHANNEL_1_ID)
.setSmallIcon(R.drawable.icon)
.setContentTitle("POWIADOMIENIE - TEST")
.setContentText("Przypomnienie o wzieciu leku")
.setWhen(time)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setSound(
RingtoneManager.getDefaultUri(
RingtoneManager.TYPE_NOTIFICATION))
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
.setCategory(NotificationCompat.CATEGORY_ALARM)
.build();
notificationManager.notify(1, notification);
}
public void sendOnChannel2(View view) {
Notification notification = new NotificationCompat.Builder(this, App.CHANNEL_2_ID)
.setSmallIcon(R.drawable.icon)
.setWhen(time)
.setContentTitle("POWIADOMIENIE - TEST [2]")
.setContentText("Przypomnienie o wzieciu leku " + time)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setSound(
RingtoneManager.getDefaultUri(
RingtoneManager.TYPE_NOTIFICATION))
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
.setCategory(NotificationCompat.CATEGORY_ALARM)
.build();
notificationManager.notify(2, notification);
}}
这是App.java类,在其中创建通道:
public class App extends Application {
public static final String CHANNEL_1_ID = "channel1";
public static final String CHANNEL_2_ID = "channel2";
@Override
public void onCreate() {
super.onCreate();
createNotificationChannels();
}
private void createNotificationChannels() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel1 = new NotificationChannel(
CHANNEL_1_ID,
"Channel 1",
NotificationManager.IMPORTANCE_HIGH
);
channel1.setDescription("This is Channel 1");
NotificationChannel channel2 = new NotificationChannel(
CHANNEL_2_ID,
"Channel 2",
NotificationManager.IMPORTANCE_HIGH
);
channel2.setDescription("This is Channel 2");
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel1);
manager.createNotificationChannel(channel2);
}
}
}
我还在AndroidManifest.xlm中添加了两个权限:
使用权限android:name =“ android.permission.SET_ALARM”
Uses-permission android:name =“ android.permission.VIBRATE”