Android - 从SQLite数据库重置手机重启时的警报

时间:2014-01-29 02:18:47

标签: android notifications alarmmanager alarm

我刚开始学习Android编程,目前正在创建一个简单的提醒应用程序。通过一些介绍性书籍,在线博客,当然还有很多Stack溢出页面,我已经把它变得非常好了。除了能够在手机重新启动时重置闹钟以外的所有内容。

我有一个BroadcastReceiver类,它当前处理我的警报,并在我的清单中给它正确的权限来处理'RECEIVE_BOOT_COMPLETED'。然而,当我重置手机时,无论何时设置该闹钟,它都会在启动后直接触发,并且详细信息为空。

我找到了诸如thisthis之类的页面,但我不确定如何获取日历对象并使用每个警报的原始值对其进行实例化,然后将其传递给我的接收器以设置警报再次......

这是我到目前为止所拥有的。

setAlarm方法用于首先根据日历值创建一次性警报,日历值可以通过日期和时间选择器设置,也可以通过选择旋转器(10分钟,明天等)预先设置

public void setAlarm(String alarmTitle, String alarmNotes, int alarmID) {
    // Launch a new intent to save the notification with the title and notes
    // passed from onSave
    Intent alarmIntent = new Intent(this, AlarmReceiver.class);
    alarmIntent.putExtra("Title", alarmTitle);
    alarmIntent.putExtra("Notes", alarmNotes);
    alarmIntent.putExtra("alarmID", alarmID);

    PendingIntent sender = PendingIntent.getBroadcast(this, alarmID,
            alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT | Intent.FILL_IN_DATA);

    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);

    // Pass alarm calendar to alarm manager
    am.set(AlarmManager.RTC_WAKEUP, aCal.getTimeInMillis(), sender);
}

AlarmReceiver

public class AlarmReceiver extends BroadcastReceiver {

@Override
public void onReceive(final Context context, final Intent intent) {

    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
    // Get a wake lock
    wl.acquire();

    // Get passed values
    Bundle extras = intent.getExtras();

    // Set values for use in this class
    String nTitle = extras.getString("Title");
    String nNotes = extras.getString("Notes");
    int uniqueID = extras.getInt("alarmID");

    // Set the intent to launch when the notification is clicked and allow the notifications to stack in the one row
    Intent resultIntent = new Intent(context, Main.class);
    PendingIntent resultPendingIntent = PendingIntent.getActivity(context, uniqueID, resultIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    long[] quickBurstsExtended = new long[]{0, 100, 70, 100, 70, 100, 70, 100, 70, 100, 70, 100, 70, 100, 70, 100, 70,
            100, 70, 100, 70, 100, 70, 100, 70, 100, 70, 100, 70, 100, 70, 100, 70};

    // Notification settings and alert time
    Notification builder = new NotificationCompat.Builder(context)
            .setContentTitle(nTitle) // Set the title
            .setContentText(nNotes) // Set notes
            .setSmallIcon(R.drawable.ic_launcher) // Set icon
            .setTicker("Did you remember: " + nTitle) // Marquee text (only briefly shown)
            .setContentIntent(resultPendingIntent) // Activty that is launched on click
            .setWhen(System.currentTimeMillis()) // Fire at the indicated time
            .setVibrate(quickBurstsExtended) // Allocate the above vibrate pattern
            .setLights(Color.CYAN, 500, 100) // LED lights control
            .build();

    builder.defaults = Notification.DEFAULT_SOUND;
    builder.flags = Notification.FLAG_SHOW_LIGHTS | Notification.FLAG_ONLY_ALERT_ONCE | Notification.FLAG_AUTO_CANCEL;

    NotificationManager notiManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    // Notify the manager when it is time
    notiManager.notify(uniqueID, builder);

    // Release the wakelock
    wl.release();
}

我将这些提醒存储在SQLite数据库中,并包含一个报警列,用于存储日历时间的长值,例如:20140129021559。

我见过其他人使用游标在重新启动时“排队”他们的警报,这些警报按时间排序并重置所有内容,但没有一个人有足够的信息让我真正实现它。

这是我的SQLiteAdapter类中的queueAll()方法,用于在主电源屏幕上使用我当前的所有提醒填充列表视图,并根据日期(按升序)对它们进行排序 - 这是否也可用于在电话重启时发出警报?

public Cursor queueAll() {
    String[] columns = new String[]{KEY_ID, KEY_TITLE, KEY_NOTES, KEY_TIME, KEY_DATE, KEY_ALARM_ID, KEY_ALARM};
    return db.query(DB_TABLE, columns, null, null, null, null, KEY_ALARM);
} 

非常感谢任何帮助。正如我所说,我只是刚开始所有这一切,如果你知道一个更好的方法,请告诉我。

很抱歉有任何重复,但我发现的类似页面并不具体到我的方案,以帮助我克服困难。

问候。

编辑:我相信我现在已经解决了这个问题。

对于其他任何感兴趣的人都是我所做的。

我将我的AlarmService类更改为清单中的接收者,而不是action.BOOT_COMPLETED接收器,并创建了一个名为AutoStart的单独类,它只在启动时处理对此标志的检查,然后从SQLite中排队所有行数据库并再次设置警报。

<receiver android:name=".AlarmReceiver"></receiver>                                                                                                      
<receiver android:name=".AutoStart">                                         
    <intent-filter>                                                          
        <action android:name="android.intent.action.BOOT_COMPLETED"></action>
    </intent-filter>                                                         
</receiver> 

以下是AutoStart现在的代码,它会检查数据库并再次设置警报。

public class AutoStart extends BroadcastReceiver {
SQLiteAdapter db;

    @Override
    public void onReceive(Context context, Intent intent) {
        db = new SQLiteAdapter(context);

        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
            Cursor cursor = SQLiteAdapter.queueAll();
            while (cursor.moveToNext()) {
                    Date alarmDate = new Date(cursor.getLong(cursor.getColumnIndex(SQLiteAdapter.getKeyAlarm())));
                    Calendar aCal = Calendar.getInstance();
                    aCal.setTime(alarmDate);
                    String title = cursor.getString(cursor.getColumnIndex(SQLiteAdapter.getKeyTitle()));
                    String notes = cursor.getString(cursor.getColumnIndex(SQLiteAdapter.getKeyNotes()));
                    int alarmID = cursor.getInt(cursor.getColumnIndex(SQLiteAdapter.getAlarmID()));
                    AlarmUtils.setAlarm(title, notes, alarmID, context, aCal);
            }
        }
    }
}

我的应用现在正在重启电话时重置闹钟并在正确的时间触发它们。

干杯!

0 个答案:

没有答案