2013-11-20 126 views
0

在我的應用程序中,我有TextField和按鈕。點擊後,服務應該從TextField開始讀取值並觸發AlarmManager。數秒後服務應告知BroadcastReceiver時間已完成。我不知道我做錯了什麼。如何使用AlarmManager觸發報警?

活動通知服務和發送價值。我可以看到吐司與正確的第二號人物和烤麪包運行的,但我看不到敬酒: '廣播接收器'

服務:

public class MyService extends Service { 

     @Override 
     public int onStartCommand(Intent intent, int flags, int startId) { 
      String data = (String) intent.getExtras().get("EXTRA_MESSAGE"); 
      int countTime = Integer.parseInt(data); 
      Toast tosty = Toast.makeText(this, data + " sec", Toast.LENGTH_SHORT); 
      tosty.show(); 

      Intent i = new Intent(this, MyReciver.class); 
      PendingIntent pintent = PendingIntent.getService(this.getApplicationContext(), 1000, i, 0); 
      AlarmManager alarm = (AlarmManager) getSystemService(ALARM_SERVICE); 
      alarm.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (countTime * 1000), pintent); 
      startService(i); 
      Toast tost = Toast.makeText(getApplicationContext(),  
       "RUN!", Toast.LENGTH_SHORT); 

      return Service.START_NOT_STICKY; 

     } 

廣播接收器:

public class MyReciver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context arg0, Intent arg1) { 
     Toast tostyy = Toast.makeText(arg0, "BroadcastReciver!", Toast.LENGTH_LONG); 
     tostyy.show(); 
    } 
} 

清單:

<service 
    android:name="com.example.serwis.MyService" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/service_name" > 
</service> 

<receiver 
    android:name="com.example.serwis.MyReciver"> 
</receiver> 

回答

0

嘗試變更:

PendingIntent pintent = PendingIntent.getService(this.getApplicationContext(), 1000, i, 0); 

到:

PendingIntent pintent = PendingIntent.getBroadcast(this.getApplicationContext(), 1000, i, 0);