2011-12-10 21 views
0

BroadcastReceiverを使用して「アラーム」を設定できますか?通知ではなくアラームでは?BroadcastReceiverを使用してアラームを設定しますか?

基本的に私がボタンをクリックした場合、今から20分、または変数が何であれ、アラームを設定できます。私はそれを使って現時点から20分のアラームを設定する方法を理解することができますが、私が得た限りトースト通知を行うことができます。私のvarは私のコードでは20秒

public class SimpleSleepActivity extends Activity { 
    /** Called when the activity is first created. */ 

    Button setAlarm, setTimer; 
    int hours = 1, alarmSec = 10; 
    Toast mToast; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     setAlarm = (Button) findViewById(R.id.bSetAlarm); 
     setTimer = (Button) findViewById(R.id.bSetTimer); 
     setTimer.setOnClickListener(mAlarmFromNow); 
    } 

    private OnClickListener mAlarmFromNow = new OnClickListener() { 
     public void onClick(View v) { 

      // When the alarm goes off, broadcast an Intent to the 
      // BroadcastReceiver. This is an Intent with an explicit class 
      // name to have a receiver instantiated and called, and then 
      // create an IntentSender to have the intent executed as a broadcast. 
      Intent intent = new Intent(SimpleSleepActivity.this, 
        AlarmFromNow.class); 
      PendingIntent sender = PendingIntent.getBroadcast(
        SimpleSleepActivity.this, 0, intent, 0); 

      // Finding the current time and setting and alarm for XX seconds 
      // from that time 
      Calendar calendar = Calendar.getInstance(); 
      calendar.setTimeInMillis(System.currentTimeMillis()); 
      calendar.add(Calendar.SECOND, alarmSec); 

      // Scheduling the alarm 
      AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); 
      am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender); 

      if (mToast != null) { 
       mToast.cancel(); 
      } 
      mToast = Toast.makeText(SimpleSleepActivity.this, 
        //Show the user what they imputed 
        "The alarm will go off in " + alarmSec + " Seconds", Toast.LENGTH_LONG); 
      mToast.show(); 
     } 
    }; 
} 

私の他のクラスは、このです

注:おそらくAlarmManagerを使用したい

public class AlarmFromNow extends BroadcastReceiver { 

    public void onReceive(Context context, Intent intent) { 
     //Display a toast after alarmSec is counted 
     Toast.makeText(context, R.string.alarm_from_now, Toast.LENGTH_LONG) 
       .show(); 
    } 
} 
+0

基本的に何が欲しいですか?トーンや何を設定する必要がありますか? –

答えて

関連する問題