1. 一定时长后启动 Service (AlarmService) once.
>> main page layout (main.xml)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/startalarm" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Start" /> <Button android:id="@+id/cancelalarm" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Cancel" /> </LinearLayout>
>> main activity (MyAlarm.java)
package com.exercise.myalarm; import java.util.Calendar; import android.app.Activity; import android.os.Bundle; import android.app.PendingIntent; import android.widget.Button; import android.widget.Toast; import android.view.View.OnClickListener; import android.view.View; import android.content.Intent; import android.app.AlarmManager; public class MyAlarm extends Activity { /** Called when the activity is first created. */ private PendingIntent pendingIntent; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btnStart = (Button) findViewById(R.id.startalarm); Button btnCancel = (Button) findViewById(R.id.cancelalarm); btnStart.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MyAlarm.this, MyAlarmService.class); pendingIntent = PendingIntent.getService(MyAlarm.this, 0, intent, 0); AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); calendar.add(Calendar.SECOND, 10); alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); Toast.makeText(MyAlarm.this, "Start Alarm", Toast.LENGTH_LONG).show(); } }); btnCancel.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); alarmManager.cancel(pendingIntent); Toast.makeText(MyAlarm.this, "Cancel Alram", Toast.LENGTH_LONG).show(); } }); } }
>> create service (MyAlarmService.java)
package com.exercise.myalarm; import android.app.Service; import android.widget.Toast; import android.content.Intent; import android.os.IBinder; public class MyAlarmService extends Service { @Override public void onCreate() { Toast.makeText(this, "MyAlarmService.onCreate()", Toast.LENGTH_LONG).show(); } @Override public IBinder onBind(Intent intent) { Toast.makeText(this, "MyAlarmService.onBind()", Toast.LENGTH_LONG).show(); return null; } @Override public void onDestroy() { super.onDestroy(); Toast.makeText(this, "MyAlarmService.onDestry()", Toast.LENGTH_LONG).show(); } @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG).show(); } @Override public boolean onUnbind(Intent intent) { Toast.makeText(this, "MyAlarmService.onUnbind()", Toast.LENGTH_LONG).show(); return super.onUnbind(intent); } }
>> AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.exercise.myalarm" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MyAlarm" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".MyAlarmService" /> </application> </manifest>
>> screenshot
2. 可设置闹钟时间, 时间一到可周期性的发送 broadcast, 按 “取消“ 键可取消
>> Main page layout (main.xml)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/tv_status" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/setupalarm" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="设定闹钟" /> <Button android:id="@+id/cancelalarm" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="取消闹钟" /> </LinearLayout>
>> Main activity (MyAlarm2.java)
package com.exercise.myalarm2; import android.app.Activity; import android.app.PendingIntent; import android.os.Bundle; import android.app.AlarmManager; import android.widget.TextView; import android.widget.Button; import android.widget.TimePicker; import java.util.Calendar; import android.view.View.OnClickListener; import android.view.View; import android.app.TimePickerDialog; import android.widget.TimePicker; import android.content.Intent; public class MyAlarm2 extends Activity { /** Called when the activity is first created. */ private TextView tvStatus; private Button btnSetup; private Button btnCancel; private Calendar calendar; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tvStatus = (TextView) findViewById(R.id.tv_status); btnSetup = (Button) findViewById(R.id.setupalarm); btnCancel = (Button) findViewById(R.id.cancelalarm); calendar = Calendar.getInstance(); btnSetup.setOnClickListener(new OnClickListener() { public void onClick(View v) { calendar.setTimeInMillis(System.currentTimeMillis()); int hour = calendar.get(Calendar.HOUR_OF_DAY); int minute = calendar.get(Calendar.MINUTE); new TimePickerDialog(MyAlarm2.this, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { // TODO Auto-generated method stub calendar.setTimeInMillis(System.currentTimeMillis()); calendar.set(Calendar.HOUR_OF_DAY, hourOfDay); calendar.set(Calendar.MINUTE, minute); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); Intent intent = new Intent(MyAlarm2.this, MyAlarmReceiver.class); PendingIntent pi = PendingIntent.getBroadcast(MyAlarm2.this, 0, intent, 0); AlarmManager am = (AlarmManager) getSystemService(Activity.ALARM_SERVICE); am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pi); am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), (10*1000), pi); tvStatus.setText("设置闹钟时间为::> " + hourOfDay + ":" + minute); } }, hour, minute, true).show(); } }); btnCancel.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent intent = new Intent(MyAlarm2.this, MyAlarmReceiver.class); PendingIntent pi = PendingIntent.getBroadcast(MyAlarm2.this, 0, intent, 0); AlarmManager am = (AlarmManager) getSystemService(Activity.ALARM_SERVICE); am.cancel(pi); tvStatus.setText("取消闹钟!"); } }); } }
>> Create a broadcast receiver (MyAlarmReceiver.java)
package com.exercise.myalarm2; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; public class MyAlarmReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "闹钟时间到", Toast.LENGTH_LONG).show(); } }
>> AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.exercise.myalarm2" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MyAlarm2" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".MyAlarmReceiver" android:process=":remote" /> </application> </manifest>
The tag of "android:process" values ":remote" means set "remote" as the name of the receiver in process running list
see below screenshot
>> screenshot