首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

大局定时器AlarmManager(2)

2012-09-10 
全局定时器AlarmManager(2)在编写ChangeWallpaperService类时应注意如下3点:为了通过InputStream获得图像

全局定时器AlarmManager(2)
在编写ChangeWallpaperService类时应注意如下3点:

为了通过InputStream获得图像资源,需要将图像文件放在res\raw目录中,而不是res\drawable目录中。

本例采用了循环更换壁纸的方法。也就是说,共有5个图像文件,系统会从第1个图像文件开始更换,更换完第5个文件后,又从第1个文件开始更换。

更换壁纸需要使用Context.setWallpaper方法,该方法需要一个描述图像的InputStream对象。该对象通过getResources().openRawResource(...)方法获得。

在AndroidManifest.xml文件中配置ChangeWallpaperService类,代码如下:
<service android:name=".ChangeWallpaperService" />

最后来看一下本例的主程序(Main类),代码如下:
package net.blogjava.mobile; 

import android.app.Activity; 
import android.app.AlarmManager; 
import android.app.PendingIntent; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 

public class Main extends Activity implements OnClickListener 

    private Button btnStart; 
    private Button btnStop; 
    @Override 
    public void onClick(View view) 
    { 
        AlarmManager alarmManager = (AlarmManager)
getSystemService(Context.ALARM_SERVICE); 
        //  指定ChangeWallpaperService的PendingIntent对象 
        PendingIntent pendingIntent = PendingIntent.getService(this, 0, 
                new Intent(this, ChangeWallpaperService.class), 0); 
        switch (view.getId()) 
        { 
            case R.id.btnStart: 
                //  开始每5秒更换一次壁纸 
                alarmManager.setRepeating(AlarmManager.RTC,
0, 5000, pendingIntent);                 
                btnStart.setEnabled(false); 
                btnStop.setEnabled(true); 
                break; 
            case R.id.btnStop: 
                //  停止更换一次壁纸 
                alarmManager.cancel(pendingIntent); 
                btnStart.setEnabled(true); 
                btnStop.setEnabled(false); 
                break; 
        } 
    } 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        btnStart = (Button) findViewById(R.id.btnStart); 
        btnStop = (Button) findViewById(R.id.btnStop); 
        btnStop.setEnabled(false); 
        btnStart.setOnClickListener(this); 
        btnStop.setOnClickListener(this); 
    } 
}

在编写上面代码时应注意如下3点:

在创建PendingIntent对象时指定了ChangeWallpaperService.class,这说明这个PendingIntent对象与ChangeWallpaperService绑定。AlarmManager在执行任务时会执行ChangeWallpaperService类中的onStart方法。

不要将任务代码写在onCreate方法中,因为onCreate方法只会执行一次,一旦服务被创建,该方法就不会被执行了,而onStart方法在每次访问服务时都会被调用。

获得指定Service的PendingIntent对象需要使用getService方法。在8.3.5节介绍过获得指定Activity的PendingIntent对象应使用getActivity方法。在实例51中将介绍使用getBroadcast方法获得指定BroadcastReceiver的PendingIntent对象。

实例51:多次定时提醒

工程目录:src\ch08\ch08_multialarm

在很多软件中都支持定时提醒功能,也就是说,事先设置未来的某个时间,当到这个时间后,系统会发出声音或进行其他的工作。本例中将实现这个功能。本例不仅可以设置定时提醒功能,而且支持设置多个时间点。运行本例后,单击【添加提醒时间】按钮,会弹出设置时间点的对话框,如图8.22所示。当设置完一系列的时间点后(如图8.23所示),如果到了某个时间点,系统就会播放一个声音文件以提醒用户。


下面先介绍一下定时提醒的原理。在添加时间点后,需要将所添加的时间点保存在文件或数据库中。本例使用SharedPreferences来保存时间点,key和value都是时间点。然后使用AlarmManager每隔1分钟扫描一次,在扫描过程中从文件获得当前时间(时:分)的value。如果成功获得value,则说明当前时间为时间点,需要播放声音文件,否则继续扫描。

热点排行