前言:
看过了不少安卓闹钟开发的例子,都是点到为止,都不完整,这次整一个看看。
一、闹钟的设置不需要数据库,但是展示闹钟列表的时候需要,所以需要数据库:
public class MySQLiteOpenHelper extends SQLiteOpenHelper{ public SQLiteDatabase sqlitedb; @SuppressLint("SdCardPath") public static String dbPath = "/sdcard/my.db"; public SQLiteDatabase init(){ Log.i("SD卡路径", SdCardUtil.getSdPath()); File file = new File(dbPath); if(file.exists()){ Log.i("MySQLiteOpenHelper", "数据库已存在"); } //调用此方法时,判断数据库是否存在,不存在则创建 调用OnCreate方法,存在则不调,直接放回数据库对象 sqlitedb = this.getWritableDatabase(); return sqlitedb; } public MySQLiteOpenHelper(Context context) { super(context, dbPath, null, 1); } @Override public void onCreate(SQLiteDatabase sqLiteDatabase){ }
二、闹钟列表需要有增删查询,改的问题再说:
public class ClockController { public static void createTable(SQLiteDatabase db){ db.execSQL("CREATE TABLE my_clock (" + " id varchar(16) primary key," + " clock_time varchar(16)," + " repeat_everyday varchar(2)," + " update_time varchar(16))"); } @SuppressLint("SimpleDateFormat") public static String addClock(String dateTime, MySQLiteOpenHelper dbOpenHelper) { SQLiteDatabase db = dbOpenHelper.getWritableDatabase(); ContentValues values = new ContentValues(); String id = System.currentTimeMillis()+""; values.put("id", id); values.put("clock_time", dateTime); values.put("repeat_everyday","NO"); values.put("update_time", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); db.insert("my_clock", null, values); return id; } public static boolean deleteClock(Integer id,MySQLiteOpenHelper dbOpenHelper) { SQLiteDatabase db = dbOpenHelper.getWritableDatabase(); db.delete("my_clock", "id=?", new String[] { id.toString() }); return true; } public static boolean updateClock(Integer id,String isRapeat,MySQLiteOpenHelper dbOpenHelper) { SQLiteDatabase db = dbOpenHelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("repeat_everyday", isRapeat); db.update("my_clock", values,"id=?", new String[] { id.toString() }); return true; } public static List<MyClock> getClockList(String queryStr,String[] queryValues, MySQLiteOpenHelper dbOpenHelper){ SQLiteDatabase db = dbOpenHelper.getReadableDatabase(); Cursor cursor = null; if(queryStr==null){ cursor = db.query("my_clock", null, null, null, null, null, null); } List<MyClock> clockList = new ArrayList<MyClock>(); while (cursor.moveToNext()) { clockList.add(new MyClock(cursor.getString(cursor.getColumnIndex("id")), cursor.getString(cursor.getColumnIndex("clock_time")), cursor.getString(cursor.getColumnIndex("repeat_everyday")), cursor.getString(cursor.getColumnIndex("update_time")))); } return clockList; } }
三、闹钟的增加即设置闹钟:
public class ClockActivity extends Activity{ AlarmManager alarmManager = null; Calendar calendar = Calendar.getInstance(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); MyApplication.getInstance().addActivity(this); setContentView(R.layout.activity_clock); alarmManager=(AlarmManager)getSystemService(Context.ALARM_SERVICE); /* setRepeating(int type,long startTime,long intervalTime,PendingIntent pi); 该方法用于设置重复闹钟,第一个参数表示闹钟类型,第二个参数表示闹钟首次执行时间,第三个参数表示闹钟两次执行的间隔时间,第三个参数表示闹钟响应动作。 */ queryMyClock(null); } public void setClock(View v){ queryMyClock(null); Toast.makeText(WeatherClockActivity.this, "设置闹钟", Toast.LENGTH_SHORT).show(); Dialog dialog = new TimePickerDialog(WeatherClockActivity.this, new OnTimeSetListener() { @Override public void onTimeSet(TimePicker timePicker, int hourOfDay, int minute) { Calendar c=Calendar.getInstance();//获取日期对象 c.setTimeInMillis(System.currentTimeMillis()); //设置Calendar对象 c.set(Calendar.HOUR_OF_DAY, hourOfDay); //设置闹钟小时数 c.set(Calendar.MINUTE, minute); //设置闹钟的分钟数 c.set(Calendar.SECOND, 0); //设置闹钟的秒数 c.set(Calendar.MILLISECOND, 0); //设置闹钟的毫秒数 MySQLiteOpenHelper sqLiteOpenHelper = new MySQLiteOpenHelper(getApplicationContext()); SQLiteDatabase database = sqLiteOpenHelper.init(); if(!DbUtils.tabIsExist("my_clock", sqLiteOpenHelper)){ ClockController.createTable(database); } //String id = ClockController.addClock(new SimpleDateFormat("HH:mm:dd").format(c.getTime()), sqLiteOpenHelper); Intent intent = new Intent(WeatherClockActivity.this, AlarmReceiver.class); //创建Intent对象 // intent.setFlags(Integer.parseInt(id));//作为取消时候的标识 PendingIntent pi = PendingIntent.getBroadcast(WeatherClockActivity.this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); //创建PendingIntent //设置一次性闹钟,第一个参数表示闹钟类型,第二个参数表示闹钟执行时间,第三个参数表示闹钟响应动作。 if(c.getTimeInMillis() < System.currentTimeMillis()){ Log.i("clock", "设置时间要推迟24小时,不然立刻会响"); alarmManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis()+24*60*60*1000, pi); }else{ alarmManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi); //设置闹钟,当前时间就唤醒 } queryMyClock(null); Toast.makeText(WeatherClockActivity.this, "闹钟设置成功", Toast.LENGTH_LONG).show();//提示用户 } }, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), false); dialog.show(); } public void queryMyClock(View v){ MySQLiteOpenHelper sqLiteOpenHelper = new MySQLiteOpenHelper(getApplicationContext()); SQLiteDatabase database = sqLiteOpenHelper.init(); if(!DbUtils.tabIsExist("my_clock", sqLiteOpenHelper)){ ClockController.createTable(database); } ListView listView = (ListView)findViewById(R.id.clocklist); SimpleAdapter adapter = new SimpleAdapter(getApplicationContext(), getClockList(), R.layout.my_clock_list, new String[]{"clock_time",}, new int[]{R.id.my_clock}); listView.setAdapter(adapter); } public List<? extends Map<String, ?>> getClockList(){ MySQLiteOpenHelper db = new MySQLiteOpenHelper(this); List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); Map<String, Object> map = null; List<MyClock> clocks = ClockController.getClockList(null, null, db); for (MyClock myClock : clocks) { map = new HashMap<String, Object>(); map.put("clock_time", myClock.getClock_time()); list.add(map); } return list; } }
1、布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/setclock" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/setclock" android:onClick="setClock" android:layout_weight="1" /> </LinearLayout> <ListView android:id="@+id/clocklist" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="2dp" > </ListView> </LinearLayout>
2、首页展示已设置过的闹钟列表:
1、首先需要判断要查询的表是否存在,不存在则需要创建,判断表是否存在的方法如下:
public class DbUtils {
/**
* 判断某张表是否存在
* @param tabName 表名
* @return
*/
public static boolean tabIsExist(String tabName,MySQLiteOpenHelper dbHelper){
boolean result = false;
if(tabName == null){
return false;
}
SQLiteDatabase db = null;
Cursor cursor = null;
try {
db = dbHelper.getReadableDatabase();//此this是继承SQLiteOpenHelper类得到的
String sql = "select count(*) as c from sqlite_master where type ='table' and name ='"+tabName.trim()+"'";
cursor = db.rawQuery(sql, null);
if(cursor.moveToNext()){
int count = cursor.getInt(0);
if(count>0){
result = true;
}
}
} catch (Exception e) {
LogUtil.initData("判断表是否存在出现异常", "log.txt");
}
return result;
}
2、列表的展示选择使用适配器,以上代码使用第一种方法:
SimpleAdapter adapter = new SimpleAdapter(getApplicationContext(), getClockList(), R.layout.my_clock_list, new String[]{"clock_time",}, new int[]{R.id.my_clock});
getClockList方法返回的是一个List类型,将一个Map类型放置其中,通过key值去填充不同view。具体看代码实现!
适配ListView的布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <TextView android:id="@+id/my_clock" android:layout_height="wrap_content" android:layout_width="match_parent" /> </LinearLayout>
另外,适配器还有第二种实现:以下是一个例子,与此代码无关
public class MyAdapter extends BaseAdapter{ public List<Object> list; public Context context; public WearthAdapter(List<Object> list,Context context) { this.list = list; this.context = context; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View view, ViewGroup parent) { TextView t1 = null,t2=null,t3=null,t4=null,t5=null; if(view==null){ view = LayoutInflater.from(context).inflate(R.layout.weather_day, parent); t1 = (TextView)findViewById(R.id.w_day); t2 = (TextView)findViewById(R.id.w_cond); t3 = (TextView)findViewById(R.id.w_wind); t4 = (TextView)findViewById(R.id.max_tmp); t5 = (TextView)findViewById(R.id.min_tmp); } t1.setText("1"); t2.setText("2"); t3.setText("3"); t4.setText("4"); t5.setText("5"); return view; } }
3、闹钟的设置:
调用TimePickerDialog实现,这是一个时间选择器,通过监听其选择的时间进行闹钟设置;
闹钟设置的主要代码如下:
1.获取系统服务:
alarmManager=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
2. 创建PendingIntent,其中AlarmReceiver.class是闹钟触发的实现动作。
Intent intent = new Intent(ClockActivity.this, AlarmReceiver.class); //创建Intent对象
PendingIntent pi = PendingIntent.getBroadcast(WeatherClockActivity.this, 0,
intent, PendingIntent.FLAG_CANCEL_CURRENT);
3、 //设置闹钟,当前时间就唤醒
alarmManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi);
4、闹钟触发是震动和响铃,在 AlarmReceiver中实现:
import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.media.AudioManager; import android.os.Vibrator; import android.util.Log; public class AlarmReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { Log.i("clock", "闹钟响了........"); Vibrator vibrator = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE); vibrator.vibrate(10000); AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE); audioManager.setStreamVolume(AudioManager.RINGER_MODE_NORMAL, 5, 0); } }
结束语:
有待改进!