As a result of the recent participation in a contest to write the smart plug-in assistant needs to use the timing trigger function, so a similar alarm function, storage alarm information with the Android's own database
Source Address Https://github.com/waterCode/SaveEner.git
Project name is Saveenergy
Considering that some people do not use Git, because the code is still perfect and modify the place, and so I write all the code after I have written in the csdn file storage upload code, but there are many advantages of learning GitHub, backup code is a great advantage, no need to compress files to the cloud disk, haha
The model named Saveenergy in the project is the source code for my project.
We all progress together, persist, work hard, and someday we can all be the great gods of his population.
What's the problem can leave a message, I will regularly read the blog drops
(a) Alarm information Display section, a total of 3 parts Oh (⊙o⊙)
First, here the main use of the LinearLayout layout, there is a ListView and ImageButton, first said that the personal feel to pay attention to the points
1.imageButton comes with a gray background, the textcolor should be set to transparent.
2. This I use when the green background, the ListView use the item layout, TextView font white, in order to facilitate debugging, with tools:textcolor= "@color/black", will facilitate debugging
3. When no alarm is added, the ListView has a top and bottom split line, with the LinearLayout split line attribute
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:divider="@drawable/my_divider"
android:showDividers="beginning|end">
As for the layout of the ListView and item specific code in my GitHub inside down to see
What we need to do in terms of activity is
1. Initialize the layout, get the instance of each view,
2. Get the pointer to the database, set the ListView adapter, here is Simplecursoradapter, about the Simplecursoradapter constructor the last parameter flag needs attention, The end of this page is Simplecursoradapter settings, because this is mainly and data class interaction class, if you want to exercise yourself can use Baseadapter, your own package class, see your preferences
3 Set the monitor for the ListView and button, here when the ListView is refreshed
.notifyDataSetChanged();
(二)接下来当然时闹钟编辑页面啦,加油继续往下看
Small two on the big picture.
Layout section:
Here the above with simple TextView and ImageButton, then Timepicker, then a ListView, a little play through, the ListView with the Simpleadapter, After clicking will magically appear a switch to select the dialog, and open and close the dialog, wow haha here with Alertdialog
Here are some notes to be aware of
1.timepicker, possible when Android system causes, timepicker default
But the program in my Huawei P7 inside is like that, layout contrast is very big, a start let me this rookie unprepared, asked the "Comrades" learn Android also no one knows, finally in the magical Stayoverflow find the answer, the following sentence can let him change back to noodles, haha
android:timePickerMode="spinner"
Code section:
1. Initialize each component to get the instance
2. Set the data for the ListView, using the Simpleadapter
3, set each listener, and
4. Put the information into the database at the end and set the alarm in the system
Here to notice when pendingintent the second parameter, in order to be able to set a number of parameters, so I use the time to save the alarm information in the database ID
int Id=dboperator. Getalarmid(time);//Get the ID of the newly added clock in the databaseIntent intent=new Intent (This, alarmreciever. Class);Intent. PutExtra("_id", id);Pendingintent pendingintent = pendingintent. Getbroadcast(This,id,intent,0);Alarmmanager Alarmmanager = (alarmmanager) getsystemservice (Context. ALARM_service);Alarmmanager. Set(Alarmmanager. RTC_wakeup,alarm. Getalarmtime(). Gettimeinmillis(), pendingintent);
(三)就是删除闹钟部分,最后一部分,坚持看下去
Here when the alarm long press ENTER, just a ListView, with or Simplecursoradapter, here to notice the rewrite BindView, because when you click the Delete button to be able to listen to the code
@Override Public void BindView(View view,FinalContext Context,FinalCursor cursor) {Super. BindView (view, context, cursor);Final intid = cursor.getint (0); ImageButton ImageButton = (ImageButton) View.findviewbyid (R.id.alarm_delete_button); Imagebutton.setonclicklistener (NewView.onclicklistener () {@Override Public void OnClick(View view) {Databaseoperator operator =NewDatabaseoperator (context); Operator.delete (ID); Cursor.requery (); Adapter.notifydatasetchanged (); } }); }
And the database part I didn't say, here is omitted, can see my Code
Basically the note is this, I only posted the key part of the code I think, and I met some of the pits I will be in the form of attention, as the source code at the beginning of my article to provide a GitHub address to download down to see, because this is part of the timing part of my project, In order to prevent friends from downloading and do not know which part of my project, here give the design of the code file pictures, convenient for friends to find
About the data crying class
Android, alarm timer function, implementation process