Service是一直在后台自动运行的,而广播是需要一些特定条件才执行的。
准备一张名为smile的图片。
在main.xml中:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
android:gravity="center_horizontal"
android:orientation="vertical">
<TimePicker
android:id="@+id/time"
android:layout_marginTop="8dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/msg"
android:layout_marginTop="16dp"
android:gravity="center_horizontal"
android:layout_width="fill_parent"
android:textColor="#ffffff"
android:layout_height="wrap_content"
android:text="当前没有设置闹钟"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="16dp"
android:gravity="center_horizontal">
<Button
android:id="@+id/set"
android:layout_width="100dp"
android:layout_height="40dp"
android:background="#3399ff"
android:textColor="#ffffff"
android:text="设置闹钟"/>
<Button
android:id="@+id/delete"
android:layout_marginLeft="30dp"
android:layout_width="100dp"
android:layout_height="40dp"
android:background="#3399ff"
android:textColor="#ffffff"
android:text="删除闹钟"/>
</LinearLayout>
</LinearLayout>
在AlarmMessage.java中:
package com.li.alarmmanager;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
public class AlarmMessage extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new AlertDialog.Builder(this)
.setIcon(R.drawable.smile)
.setTitle("闹钟时间已到!")
.setMessage(
"闹钟响起,现在时间:"
+ new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒")
.format(new Date()))
.setPositiveButton("关闭", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
AlarmMessage.this.finish();
}
}).show();
}
}
在MyAlarmManagerDemo.java中:
package com.li.alarmmanager;
import java.util.Calendar;
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;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;
import android.widget.Toast;
public class MyAlarmManagerDemo extends Activity {
private AlarmManager alarm = null; // 闹钟服务
private Button set = null;
private Button delete = null;
private TextView msg = null;
private TimePicker time = null;
private int hourOfDay = 0 ;
private int minute = 0;
private Calendar calendar = Calendar.getInstance() ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.set = (Button) super.findViewById(R.id.set);
this.delete = (Button) super.findViewById(R.id.delete);
this.msg = (TextView) super.findViewById(R.id.msg);
this.time = (TimePicker) super.findViewById(R.id.time);
this.alarm = (AlarmManager) super.getSystemService(Context.ALARM_SERVICE) ;
this.set.setOnClickListener(new SetOnClickListener()) ;
this.delete.setOnClickListener(new DeleteOnClickListener()) ;
this.time.setIs24HourView(true) ;
this.time.setOnTimeChangedListener(new OnTimeChangedListenerImpl()) ;
}
private class OnTimeChangedListenerImpl implements OnTimeChangedListener{
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
MyAlarmManagerDemo.this.calendar.setTimeInMillis(System.currentTimeMillis()) ;
MyAlarmManagerDemo.this.calendar.set(Calendar.HOUR_OF_DAY, hourOfDay) ;
MyAlarmManagerDemo.this.calendar.set(Calendar.MINUTE, minute) ;
MyAlarmManagerDemo.this.calendar.set(Calendar.SECOND, 0) ;
MyAlarmManagerDemo.this.calendar.set(Calendar.MILLISECOND, 0) ;
MyAlarmManagerDemo.this.hourOfDay = hourOfDay ;
MyAlarmManagerDemo.this.minute = minute ;
}
}
private class SetOnClickListener implements OnClickListener{
public void onClick(View v) {
Intent intent = new Intent(MyAlarmManagerDemo.this,
MyAlarmReceiver.class);
intent.setAction("com.li.action.setalarm") ;
PendingIntent sender = PendingIntent.getBroadcast(
MyAlarmManagerDemo.this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
MyAlarmManagerDemo.this.alarm.set(AlarmManager.RTC_WAKEUP,
MyAlarmManagerDemo.this.calendar.getTimeInMillis(), sender);
MyAlarmManagerDemo.this.msg.setText("闹钟响起的时间是:"
+ MyAlarmManagerDemo.this.hourOfDay + "时"
+ MyAlarmManagerDemo.this.minute + "分。");
Toast.makeText(MyAlarmManagerDemo.this, "闹钟设置成功!",
Toast.LENGTH_LONG).show();
}
}
private class DeleteOnClickListener implements OnClickListener{
public void onClick(View v) {
if (MyAlarmManagerDemo.this.alarm != null) {
Intent intent = new Intent(MyAlarmManagerDemo.this,
MyAlarmReceiver.class);
intent.setAction("com.li.action.setalarm") ;
PendingIntent sender = PendingIntent.getBroadcast(
MyAlarmManagerDemo.this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
MyAlarmManagerDemo.this.alarm.cancel(sender) ; // 取消
MyAlarmManagerDemo.this.msg.setText("当前没有设置闹钟。") ;
Toast.makeText(MyAlarmManagerDemo.this, "闹钟删除成功!",
Toast.LENGTH_LONG).show();
}
}
}
}
在MyAlarmReceiver.java中:
package com.li.alarmmanager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyAlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent it = new Intent(context, AlarmMessage.class);
it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(it);
}
}
修改AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.li.alarmmanager"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MyAlarmManagerDemo"
android:label="@string/title_activity_my_alarm_manager_demo" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".AlarmMessage" />
<receiver android:name="MyAlarmReceiver" android:enabled="true"
android:process=":remote">
<intent-filter>
<action android:name="com.li.action.setalarm" />
</intent-filter>
</receiver>
</application>
</manifest>