2012-12-06 158 views
0

我試圖用android觸發鬧鐘,但它不起作用。我的代碼不會提供Toast或「ALARMOFF」消息。我有這樣的廣播接收器:Android鬧鐘不觸發

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} 

我已經看在各地尋求解決方案但我只能確定我正在做的一切正確,但顯然我不是。

感謝您的任何幫助。

+2

嗨,你應該正確使用包名,如果你有不同的包, Talha

+0

我的天啊,我把AlarmReceiver放在不同的包裏,不小心!我以前沒有看到...讓我測試它... –

回答

5

你應該正確使用包名,如果你有不同的包,

接收器的android:NAME = 「XXX.XXXXXXXXXXXXXXX.AlarmReceiver」/>

+0

是的,你是完全正確的。 :-)解決方案是簡單地將接收器移動到正確的包裝中。這完全是一個愚蠢的意外,我沒有想到要看包裝。 –

+1

很高興聽到它解決了你的問題。我之前做過這個事故太多了:) :) – Talha