我怎么能在一周的某些日子发出警报

时间:2016-09-02 18:57:01

标签: android notifications alarmmanager

您好朋友,我开发了一个应用程序创建课程。我希望在时间过程中发出通知。

所以,如果你星期一上午8点钟开课。每周上课时间为8小时。我想创建一个通知。我是通过闹钟管理员和通知来做这件事的。我可以给你一次性警报。但不知怎的,我无法重复。代码如下。我不会尝试它,因为它是服务的一部分。

private void alarmla() {
    SQLiteDatabase db=activity.openOrCreateDatabase("SINIFD",activity.MODE_PRIVATE,null);
    Cursor c = db.rawQuery("select * from ders", null);
    if(c.getCount()>0){
        c.moveToFirst();

            do {
                AlarmManager alarmManager = (AlarmManager) activity.getSystemService(Context.ALARM_SERVICE);
                Toast.makeText(activity, c.getString(3).split(":")[0] + " " + c.getString(3).split(":")[1], Toast.LENGTH_LONG).show();
                Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");

                notificationIntent.addCategory("android.intent.category.DEFAULT");
                notificationIntent.putExtra("message", c.getString(1) + " dersi zamanı geldi. Kazanımlara ulaşmak için tıklayınız. İyi dersler.");
                notificationIntent.putExtra("gizli", c.getString(6));

                PendingIntent broadcast = PendingIntent.getBroadcast(activity, c.getInt(0), notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

                Calendar timeOff = Calendar.getInstance();
                int days = Calendar.FRIDAY + (7 - timeOff.get(Calendar.DAY_OF_WEEK)); // how many days until Sunday

                timeOff.add(Calendar.DATE, days);
                timeOff.set(Calendar.HOUR, Integer.parseInt(c.getString(3).split(":")[0]));  //buraya 22 geliyor databaseden
                timeOff.set(Calendar.MINUTE,Integer.parseInt(c.getString(3).split(":")[1]));  //buraya 10 geliyor mesala
                timeOff.set(Calendar.SECOND, 0);
                timeOff.set(Calendar.MILLISECOND, 0);
                alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, timeOff.getTimeInMillis(),AlarmManager.INTERVAL_DAY * 7, broadcast);
        } while (c.moveToNext());
        c.close();

    }
    db.close();
    Toast.makeText(activity,"Vay be ayar ",Toast.LENGTH_LONG).show();
}

调整数据库没有问题时。

1 个答案:

答案 0 :(得分:0)

重启移动设备后,请不要忘记重新启动闹钟管理器!

使用广播接收器来捕捉此动作

有关重启的更多信息:Trying to start a service on boot on Android