真个application讲的已经差不多了,在说完这一篇之后,我会上传源码到资源,喜欢的可以下载和学习,今天主讲闹钟启动设置,设置页面的加载
在Android系统中,闹钟和唤醒功能都是由Alarm Manager Service控制并管理的。我们所熟悉的RTC闹钟以及定时器都和它有莫大的关系。为了便于称呼,我常常也把这个service简称为ALMS。
另外,ALMS还提供了一个AlarmManager辅助类。在实际的代码中,应用程序一般都是通过这个辅助类来和ALMS打交道的。就代码而言,辅助类只不过是把一些逻辑语义传递给ALMS服务端而已,具体怎么做则完全要看ALMS的实现代码了。
ALMS的实现代码并不算太复杂,主要只是在管理“逻辑闹钟”。它把逻辑闹钟分成几个大类,分别记录在不同的列表中。然后ALMS会在一个专门的线程中循环等待闹钟的激发,一旦时机到了,就“回调”逻辑闹钟对应的动作,俗称定时器
在我们的application中,extends BroadcastReceiver 我是继承了广播服务,创建了一个闹钟定时服务,sharedPreferences.getString("op", "5 * 60 * 1000") 以首选项的形式存储了间隔时间段,5*60*1000 即5分钟
清单注册广播服务:
<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>
设置界面加载布局文件:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="分类一" >
<CheckBoxPreference
android:key="net"
android:summaryOff="关闭"
android:summaryOn="开启"
android:title="通知" />
<EditTextPreference
android:dependency="net"
android:dialogTitle="配置通知"
android:key="WIFI"
android:negativeButtonText="取消"
android:positiveButtonText="确定"
android:title="通知设置" />
</PreferenceCategory>
<PreferenceCategory android:title="分类二" >
<ListPreference
android:dialogTitle="设置更新时间"
android:entries="@array/op"
android:entryValues="@array/op_value"
android:key="op"
android:negativeButtonText="取消"
android:title="更新时间" />
<MultiSelectListPreference
android:dialogTitle="支持的系统"
android:entries="@array/os"
android:entryValues="@array/os"
android:key="os"
android:negativeButtonText="取消"
android:positiveButtonText="确定"
android:title="支持的系统" />
<SwitchPreference android:title="显示模式" android:key="switch" android:switchTextOn="开" android:switchTextOff="关" android:summaryOff="白天" android:summaryOn="夜间"/>
<PreferenceScreen android:title="传感器设置" android:summary="GPS、重力传感器、方向传感器">
<CheckBoxPreference android:key="gps" android:title="GPS" android:summaryOn="开启" android:summaryOff="关闭"/>
<Preference android:title="重力传感器"/>
</PreferenceScreen>
</PreferenceCategory>
</PreferenceScreen>
设置活动代码:
package com.newer.myweather;
/**
* 设置活动
* @author Engineer-Jsp
* @date 2014.10.27
* */
import android.os.Bundle;
import android.preference.PreferenceActivity;
public class SettingsActivity extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
}
定时闹钟代码:
package com.newer.myweather;
/**
* 定时闹钟
* @author Engineer-Jsp
* @date 2014.10.27
* */
import android.app.AlarmManager;
import 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 = "BootReceiver-XXXXXXXXXX";
@Override
public 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);
}
}
清单文件:
<?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="14"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<!-- GPS 、网络 -->
<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="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="com.newer.myweather.WeatherService" >
</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>
<activity android:name="com.newer.myweather.LocationActivity" >
</activity>
<activity android:name="com.newer.myweather.MainActivity" >
</activity>
</application>
</manifest>
程序结构:
布局结构组成图: