AlarmManager 实现闹钟的基本功能

先上效果图

AlarmManager 实现闹钟的基本功能AlarmManager 实现闹钟的基本功能

这是一个利用AlarmManager做的最简单的闹钟!迟点再把重复响铃(例如星期一,星期三,重复响铃)

1.MainActivity

package com.example.domereflect;

import java.util.Calendar;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener { private Button add_alarm; //添加闹钟按钮 private int sqlHour;//记录时间,小时
private int sqlMinute;//记录时间,分钟
private Calendar c; //时间 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); createView();
} private void createView(){
add_alarm = (Button)findViewById(R.id.add_alarm_btn);
add_alarm.setOnClickListener(this);
} @Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.add_alarm_btn:
int i = ; //PendingIntent.getBroadcast的第二个,四个参数。
AddAlarm.getAlarmDialog(this); //添加闹钟的dialog(主要实现保存时间的功能)
sqlHour = MyApplication.getInstance().getAlarmHour(); //获取时间,小时
sqlMinute = MyApplication.getInstance().getAlarmMin();//获取时间,分钟
Toast.makeText(this, sqlHour+":"+sqlMinute, ).show();
openAlarm(i++);//发出广播,在指定时间响铃。
break; default:
break;
}
} private void openAlarm(int id){
//获得AlarmManager类
AlarmManager manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE); //跳转,发送广播
Intent intent = new Intent(this, CallAlarm.class);
PendingIntent sender = PendingIntent.getBroadcast(this, id, intent, id); //时间的处理
c = Calendar.getInstance();
c.setTimeInMillis(System.currentTimeMillis());
c.set(Calendar.HOUR_OF_DAY, sqlHour);
c.set(Calendar.MINUTE, sqlMinute);
c.set(Calendar.SECOND, );
c.set(Calendar.MILLISECOND, ); //添加闹钟
manager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), sender); } }

2.AddAlarm类

package com.example.domereflect;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView.FindListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker; public class AddAlarm { public AddAlarm(){ } public static Dialog getAlarmDialog(Context context){ final Dialog dialog = new Dialog(context);
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.alarmdialog_layout, null);
dialog.setContentView(view);
final TimePicker picker = (TimePicker)view.findViewById(R.id.pic_time);
Button save = (Button)view.findViewById(R.id.save_time); save.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
int hour= picker.getCurrentHour();
int min = picker.getCurrentMinute();
MyApplication.getInstance().setAlarmHour(hour);
MyApplication.getInstance().setAlarmMin(min);
dialog.dismiss();
}
});
dialog.show();
return dialog;
}
}

3.还有其他几个类

package com.example.domereflect;

import android.app.Activity;
import android.os.Bundle; public class AlarmAlertDialog extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
package com.example.domereflect;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent; public class CallAlarm extends BroadcastReceiver { @Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
intent.setClass(context, AlarmAlertDialog.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
} }
package com.example.domereflect;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences; public class MyApplication extends Application {
private static MyApplication instance;
private static SharedPreferences settings; @Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
settings = getSharedPreferences("AlarmShare", Context.MODE_PRIVATE);
}
public static MyApplication getInstance(){
if(null == instance){
instance = new MyApplication();
}
return instance;
} public void setAlarmHour(int hour){
settings.edit().putInt("Hour", hour).commit();
}
public int getAlarmHour(){
return settings.getInt("Hour", );
}
public void setAlarmMin(int min){
settings.edit().putInt("Min", min).commit();
}
public int getAlarmMin(){
return settings.getInt("Min", );
} }
上一篇:安卓电量优化之AlarmManager使用全部解析


下一篇:003_生成器(generator)内部解析