重启后警报通知不起作用

时间:2014-09-05 09:21:57

标签: android notifications alarm

我正在处理警报通知功能,并希望在设备重启后保留警报。在我重启设备之前它工作正常。重新启动后,它会弹出一个错误(我无法记录错误以进行调试......所以我不知道错误究竟是什么)。这个错误真让我头疼......

已编辑:重新启动部分已更正,我在sqlite查询中删除了NOW()然后它可以正常工作~~~但我仍然想知道如下问题的答案。谢谢:)

此外,我还有几个问题,比如如何在警报到期时删除警报以及为什么通知不会在我的应用程序中振动?感谢。

这是我的代码:

RebootReceiver

public class RebootReceiver extends BroadcastReceiver{
    private String TAG = "RebootReceiver";
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Log.d(TAG, "action: "+intent.getAction());
        SQLiteDatabase db = (new SupremeDB(context)).getReadableDatabase();

        String sql = "SELECT * "+
                    " FROM "+SupremeDB.ALARM_TABLE+
                    " WHERE 1";

        Cursor alarmCursor = db.rawQuery(sql, null);
        while (alarmCursor.moveToNext()){
            String eventType = "";
            switch(alarmCursor.getString(alarmCursor.getColumnIndex(SupremeDB.TABLE_NAME))){
            case SupremeDB.ONE_TO_ONE_TABLE:
                eventType = "One To One";
                break;
            case SupremeDB.DOOR_HOST_TABLE:
                eventType = "Door Host";
                break;
            case SupremeDB.TRAINING_TABLE:
                eventType = "Training";
                break;
            case SupremeDB.LONG_PRESENT_TABLE:
                eventType = "Long Present";
                break;
            }
            this.setAlarm(context, 
                    alarmCursor.getInt(alarmCursor.getColumnIndex(SupremeDB.ALARM_ID)), 
                    alarmCursor.getString(alarmCursor.getColumnIndex(SupremeDB.ALARM_DATE)), 
                    "You have a "+
                    eventType+
                    " on "+alarmCursor.getString(alarmCursor.getColumnIndex(SupremeDB.ALARM_DATE))
                    );
        }
    }

    public Calendar str2Calendar(String datetime){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = format.parse(datetime);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            Log.e(TAG, e.toString());
            SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd");
            try {
                date = format2.parse(datetime);
            } catch (ParseException e1) {
                // TODO Auto-generated catch block
                Log.e(TAG, e1.toString());
            }
        }

        Calendar dateCalendar = Calendar.getInstance();
        dateCalendar.setTime(date);

        return dateCalendar;
    }

    public void setAlarm(Context context, int alarmId, String datetime, String message){
        Intent intent = new Intent(context, NotificationReceiver.class);
        intent.putExtra("message", message);
        intent.putExtra("alarm_id", alarmId);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, alarmId, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        Calendar dateCalendar = this.str2Calendar(datetime);
        dateCalendar.set(Calendar.HOUR_OF_DAY, dateCalendar.get(Calendar.HOUR_OF_DAY)-2);

        AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        am.set(AlarmManager.RTC, dateCalendar.getTimeInMillis(), pendingIntent);
        Log.d(TAG, "alarm set: "+dateCalendar.getTimeZone().getDisplayName()+","+dateCalendar.getTimeInMillis()+", id:"+alarmId);
    }
}

NotificationReceiver

public class NotificationReceiver extends BroadcastReceiver{
    private String TAG = this.getClass().getName();
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Log.d(TAG, "action:"+intent.getAction());
        String message = intent.getStringExtra("message");
        int alarmId = intent.getIntExtra("alarm_id", 0);
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(context)
                .setVibrate(new long[]{500, 1000})
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle(context.getResources().getString(R.string.app_name))
                .setContentText(message);

        NotificationManager notifyMgr = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        notifyMgr.notify(context.getResources().getString(R.string.app_name), alarmId, mBuilder.build());
    }

}

AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

    <receiver android:name="NotificationReceiver">

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

1 个答案:

答案 0 :(得分:1)

创建一个扩展BroadcastReceiver的类并将AlarmManager代码放在其onReceive()中,然后您可以在清单文件中重新注册BroadcastReceiver,如下所示。

<receiver android:name=".MyReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> </receiver> 还要将权限添加到清单文件。

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

您也可以refer this example

编辑: Delete您应用程序的警报。

您可以编写振动代码:

Vibrator vibrator = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(500});