如何在每天不同时间在android studio中设置每日重复通知? [英] How to set daily repeating notification in android studio at different times each day?

查看:76
本文介绍了如何在每天不同时间在android studio中设置每日重复通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前每天都有重复的通知,每天下午 6 点重复.我想做的是在活动即将开始时显示通知而不是下午 6 点.我有一个事件数组列表,它包含日期和时间,这可能吗?以下是我目前显示通知的方式.

I currently have daily repeating notifications which repeat at 6pm each day. What I want to do is instead of 6pm, show the notification when a event is about to start. I have a events arraylist, and it contains dates and times, would this be possible. Below is how I am currently showing the notifications.

这是我的主要活动

Calendar calendar = Calendar.getInstance();
 calendar.set(Calendar.HOUR_OF_DAY, 18);
 calendar.set(Calendar.MINUTE, 30);
 calendar.set(Calendar.SECOND, 0);
 Intent intent1 = new Intent(MainActivity.this, AlarmReceiver.class);
 PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0,intent1, PendingIntent.FLAG_UPDATE_CURRENT);
 AlarmManager am = (AlarmManager) MainActivity.this.getSystemService(MainActivity.this.ALARM_SERVICE);
 am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

这是在我的 broadcast_reciever 类中

and this is in my broadcast_reciever class

公共类 AlarmReceiver 扩展了 BroadcastReceiver{

public class AlarmReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

        long when = System.currentTimeMillis();
        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);

        Intent notificationIntent = new Intent(context, EVentsPerform.class);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
                notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);


        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
                context).setSmallIcon(R.drawable.applogo)
                .setContentTitle("Alarm Fired")
                .setContentText("Events To be PErformed").setSound(alarmSound)
                .setAutoCancel(true).setWhen(when)
                .setContentIntent(pendingIntent)
                .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
        notificationManager.notify(MID, mNotifyBuilder.build());
        MID++;

    }

}

我不知道如何设置时间,所以它会在有事件发生时重复.任何指针和帮助表示赞赏.谢谢

I am not sure how to set the time so it repeats when and if there is a event. Any pointers and help with be appreciated. Thanks

推荐答案

public class DatabaseHandler extends SQLiteOpenHelper {
    private static final String DATABASE_PATH = "/data/data/YOUR_PACKAGE_HERE/databases/";
    private static final String DATABASE_NAME = "YOUR_DATABASE.db";
    private static final int DATABASE_VERSION = 1;
    private SQLiteDatabase myDataBase = null;

    private final Context myContext;
    Context context = null;

    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        this.myContext = context;
        // TODO Auto-generated constructor stub
        myDataBase = this.getWritableDatabase();
        myDataBase.close();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL("CREATE TABLE IF NOT EXISTS INFO (" +
                "id INTEGER PRIMARY KEY AUTOINCREMENT," +
                "alarmTime INTEGER," +
                "name TEXT)");
    }

    public void addInfo(long time, String name) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put("alarmTime", time);
        values.put("name", name);

        db.insert("INFO", null, values);
        db.close();
    }

    public ArrayList<NotificationInfo> getInfoAll() {
        String selectQuery;
        SQLiteDatabase db;
        Cursor cursor;
        NotificationInfo ni = new NotificationInfo();

        ArrayList<NotificationInfo> result = new ArrayList<NotificationInfo>();
        String myPath = DATABASE_PATH + DATABASE_NAME;

        db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
        selectQuery = "SELECT * FROM INFO";
        cursor = db.rawQuery(selectQuery, null);

        if (cursor.moveToFirst()) {
            ni.setAlarmTime(cursor.getLong(cursor.getColumnIndex("alarmTime"));
            ni.setName(cursor.getLong(cursor.getColumnIndex("name"));
            result.add(new NotificationInfo(ni));
            while(cursor.moveToNext()){
                ni.setAlarmTime(cursor.getLong(cursor.getColumnIndex("alarmTime"));
                ni.setName(cursor.getLong(cursor.getColumnIndex("name"));
                result.add(new NotificationInfo(ni));
            }
        }
        cursor.close();
        db.close();

        return result;
    }
}

public class AlarmActivity extends Activity{
    //standard code for activity goes here
    private void buttonClicked(){
        Calendar c = Calendar.getInstance();
        DatabaseHandler db = new Databasehandler(AlarmActivity.this);
        ArrayList<NotificationInfo> alarms = db.getInfoAll();
        db.close();

        for(NotificationInfo ni : alarms){
            c.setTimeInMillis(ni.getAlarmTime());
            c.add(Calendar.MINUTE, -30);
            setAlarm(c);
        }
    }

    private void setAlarm(Long dateInMillis) {
        AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Intent alarmIntent = new Intent(this, AlarmReceiver.class);
        PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        if (Build.VERSION.SDK_INT >= 23) {
            am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, dateInMillis, pi);
        } else if (Build.VERSION.SDK_INT >= 19) {
            am.setExact(AlarmManager.RTC_WAKEUP, dateInMillis, pi);
        } else {
            am.set(AlarmManager.RTC_WAKEUP, dateInMillis, pi);
        }
    }       
}

public class NotificationInformation{
    Long alarmTime;
    String name;
    public void NotificationInformation(){

    }
    public void NotificationInformation(NotificationInformation ni){
        this.name = ni.getName();
        this.alarmTime = ni.getAlarmTime();
    }
    //getters and setters
    public void setAlarmTime(Long alarmTime){
        this.alarmTime = alarmTime;
    }
    public void setName(String name){
        this.nime = nime;
    }
    public Long getAlarmTime(){
        return alarmTime;
    }
    public String getName(){
        return name;
    }
}

这篇关于如何在每天不同时间在android studio中设置每日重复通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆