Rus*_*abh 15 notifications android android-notifications
我已经推荐了很多网站,但我仍然无法创建通知(提醒或警报)我不确切知道如何创建和使用它.它用于通知/提醒用户任务并向用户提供每日提示..我很乐意帮助您这样做以及如何编写代码...
问候:) Thanxs提前为您提供帮助.
iTu*_*rki 39
你需要两件事:
这是一个基本的例子:
在您的活动中:
Intent myIntent = new Intent(this , NotifyService.class);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.HOUR, 0);
calendar.set(Calendar.AM_PM, Calendar.AM);
calendar.add(Calendar.DAY_OF_MONTH, 1);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000*60*60*24 , pendingIntent);
Run Code Online (Sandbox Code Playgroud)
这将在午夜(上午12点)每天触发警报.如果需要,您可以更改它.
现在,创建一个服务NotifyService并将此代码放入其中onCreate():
@Override
public void onCreate() {
NotificationManager mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.notification_icon, "Notify Alarm strart", System.currentTimeMillis());
Intent myIntent = new Intent(this , MyActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
notification.setLatestEventInfo(this, "Notify label", "Notify text", contentIntent);
mNM.notify(NOTIFICATION, notification);
}
Run Code Online (Sandbox Code Playgroud)
此代码将在收到警报时显示通知.
祝好运!
这是一个关于每日通知的YouTube 视频教程。您可以在说明中找到源代码。
这个视频不是我自己做的。但我认为这是一个快速的帮助。虽然我建议进行一些更改,因为 Notification.Builder 已弃用:
1.
import android.support.v4.app.NotificationCompat;
Run Code Online (Sandbox Code Playgroud)
2.
// Change: Notification mNotify = new Notification.Builder(this) to
Notification mNotify = new NotificationCompat.Builder(this)
Run Code Online (Sandbox Code Playgroud)
玩得开心!
| 归档时间: |
|
| 查看次数: |
44324 次 |
| 最近记录: |