博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android闹钟开发与展示Demo
阅读量:5973 次
发布时间:2019-06-19

本文共 11896 字,大约阅读时间需要 39 分钟。

前言:

   看过了不少安卓闹钟开发的例子,都是点到为止,都不完整,这次整一个看看。

一、闹钟的设置不需要数据库,但是展示闹钟列表的时候需要,所以需要数据库:

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
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
clockList = new ArrayList
(); 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
> getClockList(){ MySQLiteOpenHelper db = new MySQLiteOpenHelper(this); List
> list = new ArrayList
>(); Map
map = null; List
clocks = ClockController.getClockList(null, null, db); for (MyClock myClock : clocks) { map = new HashMap
(); map.put("clock_time", myClock.getClock_time()); list.add(map); } return list; }}

1、布局文件:

View Code

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;    }
DbUtils.java

  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的布局文件:

     另外,适配器还有第二种实现:以下是一个例子,与此代码无关

public class MyAdapter extends BaseAdapter{        public List list;                public Context context;                public WearthAdapter(List 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;        }            }
MyAdapter.java

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);            }}
AlarmReceiver.java

结束语:

    有待改进!

转载于:https://www.cnblogs.com/liangblog/p/5882064.html

你可能感兴趣的文章
Python中使用ElementTree解析xml
查看>>
jquery 操作iframe、frameset
查看>>
解决vim中不能使用小键盘
查看>>
Eclipse Java @Override 报错
查看>>
linux的日志服务器关于屏蔽一些关键字的方法
查看>>
mysql多实例实例化数据库
查看>>
javascript 操作DOM元素样式
查看>>
HBase 笔记3
查看>>
【Linux】Linux 在线安装yum
查看>>
Atom 编辑器系列视频课程
查看>>
[原][osgearth]osgearthviewer读取earth文件,代码解析(earth文件读取的一帧)
查看>>
mybatis update返回值的意义
查看>>
expdp 详解及实例
查看>>
通过IP判断登录地址
查看>>
深入浅出JavaScript (五) 详解Document.write()方法
查看>>
Beta冲刺——day6
查看>>
在一个程序中调用另一个程序并且传输数据到选择屏幕执行这个程序
查看>>
代码生成工具Database2Sharp中增加视图的代码生成以及主从表界面生成功能
查看>>
关于在VS2005中编写DLL遇到 C4251 警告的解决办法
查看>>
提高信息安全意识对网络勒索病毒说不
查看>>