2012-07-04 115 views
1

我創建了一個它被按在我的主要活動保存按鈕如下調用方法的廣播:Alarmmanager不播或廣播接收機無法接收

public void scheduleAlarms(Context ctxt) { 
     String [] calvalue = (MessageDelay.getSelectedItem().toString()).split(" ", 3); 
     int H = Integer.parseInt(calvalue[1]); 
     Log.v("Timer","Timer"+H); 

     switch (H){ 

     case 9: 
       H=9; 
       break; 
     case 12: 
       H=12; 
       break; 

     case 3: 
       H=15; 
       break; 
     case 5: 
       H=17; 
       break; 
     case 6: 
       H=18; 
       break; 
     case 8: 
       H=20; 
       break; 

     } 
     Calendar cal = Calendar.getInstance(); 
     cal.set(Calendar.HOUR_OF_DAY, H); 
     cal.set(Calendar.MINUTE, 0); 
     cal.set(Calendar.SECOND, 0); 
     cal.set(Calendar.MILLISECOND, 0); 

      AlarmManager mgr=(AlarmManager)getSystemService(Context.ALARM_SERVICE); 
      Intent i=new Intent(getApplicationContext(), AlarmReceiver.class); 
      PendingIntent pi=PendingIntent.getService(getApplicationContext(), 0, i, 0); 

      mgr.setRepeating(AlarmManager.RTC,cal.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pi); 
      Log.v("Timer","Timer"); 
     } 

然後我加入一行表現爲如下:

<receiver android:process=":remote" android:name=".AlarmReceiver"></receiver> 

然後創建了一個接收器類,如下所示:

public class AlarmReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     try { 
      Toast.makeText(context, "broadcast received", Toast.LENGTH_SHORT).show(); 
      // Set up new intent 
      Intent newIntent = new Intent(context, SendMessage.class); 

      // Start new intent 
      context.startService(newIntent); 
     } catch (Exception e) { 
      Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show(); 
      e.printStackTrace(); 
     } 
    } 
} 

但由於某種原因,當我在模擬器上測試它時,我無法接收任何廣播:(

任何人都可以請指教我我做錯了什麼?

提前

回答

2

非常感謝您正在使用的服務的PendingIntent,但預計接收廣播。嘗試使用PendingIntent.getBroadcast()

+0

老兄你是天才,它的工作。我無法相信我在那裏犯了一個愚蠢的錯誤。非常感謝 :) –