全局定时器AlarmManager(3)

8.3.5  全局定时器AlarmManager(3)

本例使用BroadcastReceiver来处理定时提醒任务。BroadcastReceiver类的代码如下:
package net.blogjava.mobile; 

import java.util.Calendar; 
import android.app.Activity; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.media.MediaPlayer; 

public class AlarmReceiver extends BroadcastReceiver 

    @Override 
    public void onReceive(Context context, Intent intent) 
    { 
        SharedPreferences sharedPreferences =
context.getSharedPreferences( 
                "alarm_record", Activity.MODE_PRIVATE); 
        String hour = String.valueOf(Calendar.
getInstance().get(Calendar.HOUR_OF_DAY)); 
        String minute = String.valueOf(Calendar.
getInstance().get(Calendar.MINUTE)); 
        //  从XML文件中获得描述当前时间点的value 
        String time = sharedPreferences.
getString(hour + ":" + minute, null); 
        if (time != null) 
        {             
            //  播放声音 
            MediaPlayer mediaPlayer =
MediaPlayer.create(context, R.raw.ring); 
            mediaPlayer.start(); 
        } 
    } 
}

配置AlarmReceiver类的代码如下:
<receiver android:name=".AlarmReceiver" android:enabled="true" />

在主程序中每添加一个时间点,就会在XML文件中保存所添加的时间点,代码如下:
package net.blogjava.mobile; 

import android.app.Activity; 
import android.app.AlarmManager; 
import android.app.AlertDialog; 
import android.app.PendingIntent; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.TimePicker; 

public class Main extends Activity implements OnClickListener 

    private TextView tvAlarmRecord; 
    private SharedPreferences sharedPreferences; 
    @Override 
    public void onClick(View v) 
    { 
        View view = getLayoutInflater().inflate(R.layout.alarm, null); 
        final TimePicker timePicker = (TimePicker)
view.findViewById(R.id.timepicker); 
        timePicker.setIs24HourView(true); 
        //  显示设置时间点的对话框 
        new AlertDialog.Builder(this).setTitle("设置提醒时间").setView(view) 
                .setPositiveButton("确定", new
DialogInterface.OnClickListener() 
                { 
                    @Override 
                    public void onClick(DialogInterface dialog, int which) 
                    { 
                        String timeStr = String.valueOf(timePicker 
                                .getCurrentHour()) + ":" 
                                + String.valueOf
(timePicker.getCurrentMinute()); 
                        //  将时间点添加到TextView组件中 
                        tvAlarmRecord.setText
(tvAlarmRecord.getText().toString() + "\n" + timeStr); 
                        //  保存时间点 
                        sharedPreferences.edit().
putString(timeStr, timeStr).commit(); 
                    } 
                }).setNegativeButton("取消", null).show(); 
    } 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        Button btnAddAlarm = (Button) findViewById(R.id.btnAddAlarm); 
        tvAlarmRecord = (TextView) findViewById(R.id.tvAlarmRecord); 
        btnAddAlarm.setOnClickListener(this); 
        sharedPreferences = getSharedPreferences("alarm_record", 
                Activity.MODE_PRIVATE); 
        AlarmManager alarmManager = (AlarmManager)
getSystemService(Context.ALARM_SERVICE); 
        Intent intent = new Intent(this, AlarmReceiver.class); 
        //  创建封装BroadcastReceiver的pendingIntent对象 
        PendingIntent pendingIntent = PendingIntent.
getBroadcast(this, 0,intent, 0); 
        //  开始定时器,每1分钟执行一次 
        alarmManager.setRepeating(AlarmManager.RTC,
0, 60 * 1000, pendingIntent); 
    } 
}

在使用本例添加若干个时间点后,会在alarm_record.xml文件中看到类似下面的内容:
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<string name="18:52">18:52</string>
<string name="20:16">20:16</string>
<string name="19:11">19:11</string>
<string name="19:58">19:58</string>
<string name="22:51">22:51</string>
<string name="22:10">22:10</string>
<string name="22:11">22:11</string>
<string name="20:10">20:10</string>
</map>

上面每个<string>元素都是一个时间点,定时器将每隔1分钟查一次alarm_record.xml文件。

你可能感兴趣的:(xml,android,.net,OS,mobile)