Android Programming weather Alarm Clock Start Service settings interface load--4

Source: Internet
Author: User

the whole application talk about already almost, after this article, I will upload source to resources, like can download and learn, today, the alarm clock start setting, set page loading

In Android, the alarm and wake functions are controlled and managed by the Alarm Manager service. We are familiar with the RTC alarm clock and timer have a great relationship with it. For your convenience, I often call this service a alms.

In addition, ALMS provides a Alarmmanager helper class. In the actual code, the application generally uses this helper class to deal with alms. As far as the code is concerned, the auxiliary class simply passes some logical semantics to the ALMS server, and exactly how to do it depends on the implementation code of the ALMS.

Alms's implementation code is not too complicated, it's mostly just managing "logical alarms". It divides the logical alarms into several large categories, each recorded in a different list. Then alms in a dedicated thread to wait for the alarm to be fired, once the time is up, the "callback" logic alarm corresponding to the action, commonly known as timers

In our application, extends Broadcastreceiver I was inheriting the broadcast service, creating an alarm timer service, sharedpreferences.getstring ("Op", "5 * 60 * 1000") The interval period is stored in the form of a preference item, 5*60*1000 is 5 minutes

List Registration Broadcast service:

        <receiver android:name= "Com.newer.myweather.BootReceiver" >            <intent-filter>                <action Android:name= "Android.intent.action.BOOT_COMPLETED"/>            </intent-filter>        </receiver>

Checklist Registration Settings Activity interface:

<activity android:name= "com.newer.myweather.SettingsActivity" >        </activity>

The Setup interface loads the layout file:

<?xml version= "1.0" encoding= "Utf-8"? ><preferencescreen xmlns:android= "http://schemas.android.com/apk/ Res/android "> <preferencecategory android:title=" Category One "> <checkboxpreference android:key= "NET" android:summaryoff= "android:summaryon=" Turn on "android:title=" Notifications/> <             Edittextpreference android:dependency= "NET" android:dialogtitle= "configuration Notification" android:key= "WIFI" Android:negativebuttontext= "Cancel" android:positivebuttontext= "OK" android:title= "Notification Settings"/&G    T </PreferenceCategory> <preferencecategory android:title= "category II" > <listpreference Androi            d:dialogtitle= "Set update Time" android:entries= "@array/op" android:entryvalues= "@array/op_value" Android:key= "Op" android:negativebuttontext= "Cancel" android:title= "Update Time"/> <multiselectli Stpreference AndroId:dialogtitle= "Supported Systems" android:entries= "@array/os" android:entryvalues= "@array/os" Androi d:key= "OS" android:negativebuttontext= "Cancel" android:positivebuttontext= "OK" android:title= "Supported Systems"/> <switchpreference android:title= "display mode" android:key= "switch" android:switchtexton= "on" Android: switchtextoff= "Off" android:summaryoff= "daytime" android:summaryon= "Night"/> <preferencescreen android:title= "Sensor settings" Android:summary= "GPS, gravity sensor, direction sensor" > <checkboxpreference android:key= "GPs" android:title= "GPs" android:summary on= "Turn on" android:summaryoff= "Off"/> <preference android:title= "Gravity sensor"/> </preferencescreen></pre Ferencecategory> </PreferenceScreen>

Set the activity code:

Package com.newer.myweather;/** * Set activity * @author engineer-jsp * @date 2014.10.27 * */import android.os.bundle;import Andro Id.preference.preferenceactivity;public class Settingsactivity extends preferenceactivity {@Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Addpreferencesfromresource ( r.xml.settings);}}

Timer Alarm Code:

Package com.newer.myweather;/** * Timer Alarm * @author engineer-jsp * @date 2014.10.27 * */import Android.app.alarmmanager;impor T Android.app.pendingintent;import android.content.broadcastreceiver;import Android.content.context;import Android.content.intent;import Android.content.sharedpreferences;import Android.preference.PreferenceManager; Import Android.util.log;public class Bootreceiver extends Broadcastreceiver {private static final String TAG = "Bootreceiv Er-xxxxxxxxxx "; @Overridepublic void OnReceive (context context, Intent Intent) {Alarmmanager Alarmmanager = ( Alarmmanager) Context.getsystemservice (Context.alarm_service); Pendingintent operation = Pendingintent.getservice (context, 0,new Intent (context, Weatherservice.class), Pendingintent.flag_update_current); Sharedpreferences sharedpreferences = preferencemanager.getdefaultsharedpreferences (context); String op = sharedpreferences.getstring ("Op", "5 * 60 * 1000"); LOG.D (TAG, "Sharedpreferences-op" + op); long time = Long.parselong (OP) * 60* 1000;alarmmanager.setrepeating (ALARMMANAGER.RTC, 0, time, operation); LOG.D (TAG, "sharedpreferences-time" + time);//Alarmmanager.cancel (operation);}}

Manifest file:

<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/        Android "package=" Com.newer.myweather "android:versioncode=" 1 "android:versionname=" 1.0.0 "> <uses-sdk android:minsdkversion= "android:targetsdkversion="/> <uses-permission android:name= "ANDROID.P" Ermission. INTERNET "/> <uses-permission android:name=" Android.permission.ACCESS_NETWORK_STATE "/> <uses-permissio n android:name= "Android.permission.RECEIVE_BOOT_COMPLETED"/> <!--GPS, network-to-<uses-permission Android : Name= "Android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name= "Android.permission.ACCESS _coarse_location "/> <application android:allowbackup=" true "android:icon=" @drawable/ic_action_name            "Android:label=" @string/app_name "Android:theme=" @android: Style/theme.holo "> <activity Android:name= "Com.newer.myweathEr. Splashactivity "android:label=" @string/app_name "Android:theme=" @android: Style/theme.holo.light "            ; <!--@android: Style/theme.notitlebar--<intent-filter> <action android:name= "an            Droid.intent.action.MAIN "/> <category android:name=" Android.intent.category.LAUNCHER "/> </intent-filter> </activity> <service android:name= "Com.newer.myweather.WeatherService" &G        T                 </service> <receiver android:name= "Com.newer.myweather.BootReceiver" > <intent-filter>        <action android:name= "Android.intent.action.BOOT_COMPLETED"/> </intent-filter> </receiver> <activity android:name= "com.newer.myweather.SettingsActivity" > </activity&gt        ; <activity android:name= "com.newer.myweather.LocationActivity" > </activity> <activity android:name= "com.newer.myweather.MainActivity" > </activity> </application></manifest>

Program Structure:



Layout structure Composition diagram:



Source: http://download.csdn.net/detail/jspping/8085313

Android Programming weather Alarm Clock Start Service settings interface load--4

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.