android studio定时发通知,android - 如何在Android Studio(java)中设置通知时间? - 堆栈内存溢出...

我想在我的应用中添加通知。 我希望它每天在特定时间显示。 我试图用.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”

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值