闹钟AlarmManager
package com.example.testalam; import java.util.Calendar; import android.app.Activity; import android.app.AlarmManager; import android.app.PendingIntent; import android.app.TimePickerDialog; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.TimePicker; public class MainActivity extends Activity { /** Called when the activity is first created. */ private TextView tv = null; private Button btn_set = null; private Button btn_cel = null; private TextView tv2 = null; private Button btn_set2 = null; private Button btn_cel2 = null; private TextView tv5 = null; private Button btn_set5 = null; private Button btn_cel5 = null; private Calendar c = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView) this.findViewById(R.id.TextView); btn_set = (Button) this.findViewById(R.id.Button01); btn_cel = (Button) this.findViewById(R.id.Button02); tv2 = (TextView) this.findViewById(R.id.TextView2); btn_set2 = (Button) this.findViewById(R.id.Button04); btn_cel2 = (Button) this.findViewById(R.id.Button03); tv5 = (TextView) this.findViewById(R.id.TextView5); btn_set5 = (Button) this.findViewById(R.id.Button05); btn_cel5 = (Button) this.findViewById(R.id.Button06); c = Calendar.getInstance(); btn_set.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub c.setTimeInMillis(System.currentTimeMillis()); int hour = c.get(Calendar.HOUR_OF_DAY); int minute = c.get(Calendar.MINUTE); new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() { public void onTimeSet(TimePicker view, int hourOfDay, int minute) { // TODO Auto-generated method stub c.setTimeInMillis(System.currentTimeMillis()); c.set(Calendar.HOUR_OF_DAY, hourOfDay); c.set(Calendar.MINUTE, minute); c.set(Calendar.SECOND, 0); c.set(Calendar.MILLISECOND, 0); Intent intent = new Intent(); intent.setAction("com.example.testala"); intent.putExtra("name", "1"); // intent.putExtra("name", "1"); PendingIntent pi = PendingIntent.getBroadcast( MainActivity.this, 1, intent, 0); AlarmManager am = (AlarmManager) getSystemService(Activity.ALARM_SERVICE); am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi);// 设置闹钟 // am.setRepeating(AlarmManager.RTC_WAKEUP, // c.getTimeInMillis(), (10 * 10 * 100000 ), pi);// 重复设置 tv.setText("设置的闹钟时间为:" + hourOfDay + ":" + minute); } }, hour, minute, true).show(); } }); btn_cel.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setAction("com.example.testala"); PendingIntent pi = PendingIntent.getBroadcast( MainActivity.this, 1, intent, 0); AlarmManager am = (AlarmManager) getSystemService(Activity.ALARM_SERVICE); am.cancel(pi); tv.setText("闹钟取消"); } }); btn_set2.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub c.setTimeInMillis(System.currentTimeMillis()); int hour = c.get(Calendar.HOUR_OF_DAY); int minute = c.get(Calendar.MINUTE); new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() { public void onTimeSet(TimePicker view, int hourOfDay, int minute) { // TODO Auto-generated method stub c.setTimeInMillis(System.currentTimeMillis()); c.set(Calendar.HOUR_OF_DAY, hourOfDay); c.set(Calendar.MINUTE, minute); c.set(Calendar.SECOND, 0); c.set(Calendar.MILLISECOND, 0); Intent intent = new Intent(); intent.setAction("com.example.testala"); intent.putExtra("name", "2"); // intent .putExtra("name","2"); PendingIntent pi = PendingIntent.getBroadcast( MainActivity.this, 2, intent, 0); AlarmManager am = (AlarmManager) getSystemService(Activity.ALARM_SERVICE); am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi);// 设置闹钟 // am.setRepeating(AlarmManager.RTC_WAKEUP, // c.getTimeInMillis(), (10 * 100000), pi);// 重复设置 tv2.setText("设置的闹钟时间为:" + hourOfDay + ":" + minute); } }, hour, minute, true).show(); } }); btn_cel2.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setAction("com.example.testala"); PendingIntent pi = PendingIntent.getBroadcast( MainActivity.this, 2, intent, 0); AlarmManager am = (AlarmManager) getSystemService(Activity.ALARM_SERVICE); am.cancel(pi); tv2.setText("闹钟取消"); } }); btn_set5.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub c.setTimeInMillis(System.currentTimeMillis()); int hour = c.get(Calendar.HOUR_OF_DAY); int minute = c.get(Calendar.MINUTE); new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() { public void onTimeSet(TimePicker view, int hourOfDay, int minute) { // TODO Auto-generated method stub c.setTimeInMillis(System.currentTimeMillis()); c.set(Calendar.HOUR_OF_DAY, hourOfDay); c.set(Calendar.MINUTE, minute); c.set(Calendar.SECOND, 0); c.set(Calendar.MILLISECOND, 0); Intent intent = new Intent(); intent.setAction("com.example.testala"); intent.putExtra("name", "3"); // intent .putExtra("name","2"); PendingIntent pi = PendingIntent.getBroadcast( MainActivity.this, 3, intent, 0); AlarmManager am = (AlarmManager) getSystemService(Activity.ALARM_SERVICE); am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi);// 设置闹钟 // am.setRepeating(AlarmManager.RTC_WAKEUP, // c.getTimeInMillis(), (10 * 10 * 10000), pi);// 重复设置 tv5.setText("设置的闹钟时间为:" + hourOfDay + ":" + minute); } }, hour, minute, true).show(); } }); btn_cel5.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setAction("com.example.testala"); PendingIntent pi = PendingIntent.getBroadcast( MainActivity.this, 3, intent, 0); AlarmManager am = (AlarmManager) getSystemService(Activity.ALARM_SERVICE); am.cancel(pi); tv5.setText("闹钟取消"); } }); } }
package com.example.testalam; import java.util.Date; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.widget.Toast; public class AlamrReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if(intent.getAction().equals("com.example.testala")) { String name = intent.getStringExtra("name"); if (name.equals("1")) { Log.i("time", "0000000000000"+new Date()); Toast.makeText(context, "闹钟时间到1", 1).show(); } else if(name.equals("2")) { Log.i("time", "11111111"+new Date()); Toast.makeText(context, "闹钟时间到2", 1).show(); } else if(name.equals("3")) { Log.i("time", "22222222222"+new Date()); Toast.makeText(context, "闹钟时间到3", 1).show(); } } }
相关推荐
xfcyhades 2020-11-20
huha 2020-10-16
sgafdsg 2020-11-04
Michael 2020-11-03
fengyeezju 2020-10-14
ziyexiaoxiao 2020-10-14
业余架构师 2020-10-09
OuNuo0 2020-09-29
moses 2020-09-22
Angelia 2020-09-11
qinxu 2020-09-10
刘炳昭 2020-09-10
Nostalgiachild 2020-09-07
Nostalgiachild 2020-08-17
leavesC 2020-08-14
一青年 2020-08-13
AndroidAiStudy 2020-08-07
ydc0 2020-07-30
绿豆饼 2020-07-28