It took me about one weeks to make a little alarm clock and now I'm going to go back and summarize it. Just beginning to contact this case, the overall feel is not too much difficulty, are some of the basic knowledge of the pile, but the reality is more brutal than the ideal, these days down, every day there will be unexpected bugs appear. In the process of debugging, deep understanding of Baidu and Google Google's usefulness, the basic common problems can be here to get tips and even answers. This article first introduces the function of this case, and then uses several times to record the knowledge points used in the article.
Feature Description:
1. The basic function of the alarm clock: As usual alarm clock, the user in the interface to select the time of the alarm to go to the set time to perform the corresponding specified action.
This function is mainly through a timepicker
Public voidOntimechanged (Timepicker view,intHour_of_day,intminutes) { //TODO auto-generated Method StubCalendar.settimeinmillis (System.currenttimemillis ());//converts the current time of Timepciker to a calender objectCalendar.set (Calendar.hour_of_day,hour_of_day);//Set HoursCalendar.set (calendar.minute,minutes);//Set minutesCalendar.set (calendar.second,0);//Set secondsCalendar.set (calendar.millisecond,0);//set millisecondsHour=hour_of_day;//Save settings for hoursMinute=minutes;//minutes to save settings}
and Pendingintent and brocardcastreceiver to achieve.
Pendingintent sender=pendingintent.getbroadcast (mainactivity. this, 0,intent,pendingintent.flag_update_current); // Specify Pendingintent
2. Custom ringtone function: users can browse all audio files on the SD card, and can set it as the ringtone when the alarm rings by clicking on their favorite songs.
This function is mainly through the cursor query Media database to achieve
//querying the Media databaseCursor cursor=mainactivity. This. Getcontentresolver (). Query (MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,NULL,NULL,NULL, MediaStore.Audio.Media.DEFAULT_SORT_ORDER); //traversing the database if(Cursor.movetofirst ()) { while(!Cursor.isafterlast ()) { intId=cursor.getint (Cursor.getcolumnindexorthrow (mediastore.audio.media._id));//Get Song number intTrackid=cursor.getint (Cursor.getcolumnindexorthrow (MediaStore.Audio.Media.ALBUM_ID));//Get song IDString album=cursor.getstring (Cursor.getcolumnindexorthrow (MediaStore.Audio.Media.ALBUM));//get song album nameString artist=cursor.getstring (Cursor.getcolumnindexorthrow (MediaStore.Audio.Media.ARTIST));//get the song singer nameString url=cursor.getstring (Cursor.getcolumnindexorthrow (MediaStore.Audio.Media.DATA));//Get Song PathString duration=cursor.getstring (Cursor.getcolumnindexorthrow (MediaStore.Audio.Media.DURATION));//get the song playback duration intSize=cursor.getint (Cursor.getcolumnindexorthrow (MediaStore.Audio.Media.SIZE));//Get song sizeString disname=cursor.getstring (Cursor.getcolumnindexorthrow (MediaStore.Audio.Media.DISPLAY_NAME));//get the song file display nameItems.Add (disname); Urls.add (URL);//Save Song PathCursor.movetonext (); } cursor.close (); }
3. Recording ringtone function: The user can not only select the SD card existing audio file as the alarm ringtone, but also can record the audio file as the alarm bell.
This feature is implemented primarily by invoking the Mediarecorder class:
mrecorder=New mediarecorder (); // set the audio source to Micphone Mrecorder.setaudiosource (MediaRecorder.AudioSource.MIC); // set the encapsulation format Mrecorder.setoutputformat (MediaRecorder.OutputFormat.THREE_GPP); Mrecorder.setoutputfile (mfilename); // set the encoding format Mrecorder.setaudioencoder (MediaRecorder.AudioEncoder.AMR_NB);
4. Turn off the alarm level: when the alarm goes off, the user wants to turn off the alarm, they must answer a question correctly to turn off the alarm properly.
This function is implemented by randomly displaying array elements through a predefined array of strings:
RM = (int) (Math.random ()); // generate a random number Questionview.settext ("Please answer the following questions:" +question[rm]) ; if (! (Answertext.gettext (). toString () = =null)) // equals compares the contents of two values, = = compares the first addresses of the two values { stopbutton.setenabled (true); }
5. Weibo sharing features: Sina Weibo's microblog sharing feature is integrated, and users can make similar "I got up on time today!" "This information is shared on Weibo.
This feature is implemented in two steps, one of which is the Weibo license authentication feature:
//Create a microblog instance, pass in App_key,redirect_url and other informationMweiboauth=NewWeiboauth ( This, Constants.app_key,constants.redirect_url,constants.scope); //read Accesstoken information from SharedpreferencesMaccesstoken=accesstokenkeeper.readaccesstoken ( This); //if Accesstoken already exists and is available if(Maccesstoken.issessionvalid ()) {Updatetokenview (true); } Else{Mssohandler=NewSsohandler (alarmmessage. This, Mweiboauth); Mssohandler.authorize (NewAuthlistener ()); }
Another is to call the Weibo share API:
Create a microblog Sharing interface instance Mweiboshareapi=weibosharesdk.createweiboapi (this, Constants.app_key); Try {//detects if the user device is microblogging client if (Mweiboshareapi.checkenvironment (True )) {//Note The third party is applied to the Weibo client Mweiboshareapi.registerapp (); If (Mweiboshareapi.isweiboappsupportapi ()) {int supportapi= Mweibosha Reapi.getweiboappsupportapi (); if (supportapi>=10351 ) {//Initialize Weibo sharing information weibomultimessage weibomessage=new weibomultimessage (); weibomessage.textobject= gettextobj ();//Initialize a third-party message request to Weibo sendmultimessagetoweiborequest Request=new Sendmultimessagetoweiborequest (); Uniquely identifies a request with transaction request.transaction= string.valueof (System.currenttimemillis ()); request.multimessage= weibomessage;//Send request to evoke Weibo sharing interface mweiboshareapi.sendrequest (request); Toast.maketext (Alarmmessage.this, "share success! Toast.length_long). Show (); }}}
Finally, attach several applications: