2014-04-15 60 views
0

在鬧鐘時間達到了我想要打開一個自定義警告對話框如何在達到鬧鐘時間時打開自定義對話框?

/* main class*/ 
public class MainActivity extends Activity { 

TimePicker myTimePicker; 
Button buttonstartSetDialog; 
TextView textAlarmPrompt; 

TimePickerDialog timePickerDialog; 

final static int RQS_1 = 1; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    textAlarmPrompt = (TextView)findViewById(R.id.alarmprompt); 

    buttonstartSetDialog = (Button)findViewById(R.id.startSetDialog); 
    buttonstartSetDialog.setOnClickListener(new OnClickListener(){ 

@Override 
public void onClick(View v) { 
textAlarmPrompt.setText(""); 
openTimePickerDialog(false); 

}}); 

} 

private void openTimePickerDialog(boolean is24r){ 
Calendar calendar = Calendar.getInstance(); 

timePickerDialog = new TimePickerDialog(
MainActivity.this, 
onTimeSetListener, 
calendar.get(Calendar.HOUR_OF_DAY), 
calendar.get(Calendar.MINUTE), 
is24r); 
timePickerDialog.setTitle("Set Alarm Time"); 

timePickerDialog.show(); 

} 

OnTimeSetListener onTimeSetListener 
= new OnTimeSetListener(){ 

@Override 
public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 

Calendar calNow = Calendar.getInstance(); 
Calendar calSet = (Calendar) calNow.clone(); 

calSet.set(Calendar.HOUR_OF_DAY, hourOfDay); 
calSet.set(Calendar.MINUTE, minute); 
calSet.set(Calendar.SECOND, 0); 
calSet.set(Calendar.MILLISECOND, 0); 

if(calSet.compareTo(calNow) <= 0){ 
//Today Set time passed, count to tomorrow 
calSet.add(Calendar.DATE, 1); 
} 

setAlarm(calSet); 
}}; 

private void setAlarm(Calendar targetCal){ 

textAlarmPrompt.setText(
"\n\n***\n" 
+ "Alarm is [email protected] " + targetCal.getTime() + "\n" 
+ "***\n"); 

Intent intent = new Intent(getBaseContext(), AlarmReceiver.class); 
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0); 
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE); 
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent); 

} 

} 

/*recieving class*/ 
public class AlarmReceiver extends BroadcastReceiver { 

private static final String TAG = "my activity"; 

@Override 
public void onReceive(Context arg0, Intent arg1) { 
Toast.makeText(arg0, "Alarm received!", Toast.LENGTH_LONG).show(); 
Log.v(TAG,"my"+arg0); 
final Dialog dialog = new Dialog(arg0); 
dialog.setContentView(R.layout.alarm); 
dialog.show(); 
} 

} 

回答

0

您必須打開在你AlarmReceiver類的onReceive方法您的自定義對話框。如何實現您的自定義對話框:

相關問題