Android警报未触发

时间:2012-12-06 14:03:57

标签: java android alarmmanager

我正在尝试用android触发警报,但它不起作用。我的代码没有给出Toast或“ALARMOFF”消息。我有这个BroadcastReceiver:

public class AlarmReceiver extends BroadcastReceiver {

    public static void addAlarm(Context context){
        Log.i("infodb","ADDEDALARM");
        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context,AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context,0,intent,0);
        am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5000, pendingIntent);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
        wl.acquire();
        Toast.makeText(context, "Alarm !!", Toast.LENGTH_SHORT).show(); // For example
        Log.i("infodb","ALARMOFF");
        wl.release();
    }

}

在清单中,我有<receiver android:name=".AlarmReceiver" />以及<uses-permission android:name="android.permission.WAKE_LOCK" />

在ListView中打开开关时会触发警报。这个代码在扩展的SimpleCursorAdapter类的bindView方法中:

@Override
    public void bindView(View v, Context context, Cursor c) {

        String name = c.getString(c.getColumnIndex("name"));
        final long time = c.getLong(c.getColumnIndex("time"));

        TextView time_text = (TextView) v.findViewById(R.id.time);
        if (time_text != null) {
            time_text.setText(date_format.format(new Date(time)));
        }

        TextView name_text = (TextView) v.findViewById(R.id.name);
        if (name_text != null) {
            name_text.setText(name);
        }

        // Do on/off button alarm code

        Switch onOff = (Switch)v.findViewById(R.id.alarm_switch);
        onOff.setChecked(c.getInt(c.getColumnIndex("onOff")) == 1);
        final Context contextl = context;
        final long id = c.getInt(c.getColumnIndex("_id"));
        onOff.setOnCheckedChangeListener(new OnCheckedChangeListener(){

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                AlarmDatabase database = new AlarmDatabase(contextl);
                SQLiteDatabase write = database.getWritableDatabase();
                ContentValues values = new ContentValues(1);
                if (isChecked){
                    // Alarm has been turned on
                    values.put("onOff",1);
                    // Add system alarm
                    AlarmReceiver.addAlarm(contextl);
                }else{
                    // Alarm has been switched off
                    values.put("onOff",0);
                    // Remove system alarm

                }
                write.update(AlarmDatabase.ALARMS_TABLE, values, "_id = " + id, null);
                write.close();
                database.close();
            }

        });
    }

当开关打开时,LogCat显示以下内容:

12-06 13:50:12.365: I/infodb(18968): ADDEDALARM
12-06 13:50:12.370: W/AlarmManager(2120): FACTORY_ON= 0
12-06 13:50:13.785: W/PowerManagerService(2120): Timer 0x7->0x3|0x0
12-06 13:50:13.785: I/PowerManagerService(2120): Ulight 0x7->0x3|0x0
12-06 13:50:13.785: D/lights(2120): button : 2 +
12-06 13:50:13.785: D/lights(2120): button : 2 -
12-06 13:50:13.785: D/PowerManagerService(2120): setLightBrightness : mButtonLight : 0
12-06 13:50:13.865: W/AlarmManager(2120): FACTORY_ON= 0
12-06 13:50:13.865: W/AlarmManager(2120): FACTORY_ON= 0
12-06 13:50:17.370: V/AlarmManager(2120): waitForAlarm result :1
12-06 13:50:17.375: V/AlarmManager(2120): trigger WAKEUP Alarm{41ec8190 type 0 XXX.XXXXXXXXXXXXXXX.XXXXXXX}
12-06 13:50:18.835: V/AlarmManager(2120): waitForAlarm result :1
12-06 13:50:18.870: V/AlarmManager(2120): trigger WAKEUP Alarm{41dc01e0 type 0 XXX.XXXXXXX.XXXXXXX}

我在不同的地方寻找解决方案,但我只能确定我正在做的一切正确,但显然我不是。

感谢您的帮助。

1 个答案:

答案 0 :(得分:5)

如果你有不同的包,你应该正确使用包名

接收器android:name =“XXX.XXXXXXXXXXXXXXX.AlarmReceiver”/&gt;