正文  UI设计 > AlarmManager >

警告服务,使用 AlarmManager

[代码] main.xmlview sourceprint?01<?xml version="1.0" encoding="utf-8"?>02<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"03android:orientation="vertical"04android:lay......

[代码] main.xml

view source
 
print?
01 <?xml version="1.0" encoding="utf-8"?>
02 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03 android:orientation="vertical"
04 android:layout_width="fill_parent"
05 android:layout_height="fill_parent"
06 >
07 <TextView
08 android:layout_width="fill_parent"
09 android:layout_height="wrap_content"
10 android:text="@string/hello"
11 />
12 <Button
13 android:id="@+id/startalarm"
14 android:layout_width="fill_parent"
15 android:layout_height="wrap_content"
16 android:text="Start"
17 />
18 <Button
19 android:id="@+id/cancelalarm"
20 android:layout_width="fill_parent"
21 android:layout_height="wrap_content"
22 android:text="Cancel"
23 />
24 </LinearLayout>

[代码] AndroidAlarmService.java

view source
 
print?
01 package com.exercise.AndroidAlarmService;
02  
03 import java.util.Calendar;
04  
05 import android.app.Activity;
06 import android.app.AlarmManager;
07 import android.app.PendingIntent;
08 import android.content.Intent;
09 import android.os.Bundle;
10 import android.os.SystemClock;
11 import android.view.View;
12 import android.widget.Button;
13 import android.widget.Toast;
14  
15 public class AndroidAlarmService extends Activity {
16  
17 private PendingIntent pendingIntent;
18  
19  
20 /** Called when the activity is first created. */
21 @Override
22 public void onCreate(Bundle savedInstanceState) {
23 super.onCreate(savedInstanceState);
24 setContentView(R.layout.main);
25 Button buttonStart = (Button)findViewById(R.id.startalarm);
26 Button buttonCancel = (Button)findViewById(R.id.cancelalarm);
27  
28 buttonStart.setOnClickListener(new Button.OnClickListener(){
29  
30 @Override
31 public void onClick(View arg0) {
32 // TODO Auto-generated method stub
33  
34 Intent myIntent = new Intent(AndroidAlarmService.this, MyAlarmService.class);
35 pendingIntent = PendingIntent.getService(AndroidAlarmService.this, 0, myIntent, 0);
36  
37 AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
38  
39 Calendar calendar = Calendar.getInstance();
40 calendar.setTimeInMillis(System.currentTimeMillis());
41 calendar.add(Calendar.SECOND, 10);
42 alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
43  
44 Toast.makeText(AndroidAlarmService.this, "Start Alarm", Toast.LENGTH_LONG).show();
45 }});
46  
47 buttonCancel.setOnClickListener(new Button.OnClickListener(){
48  
49 @Override
50 public void onClick(View arg0) {
51 // TODO Auto-generated method stub
52 AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
53 alarmManager.cancel(pendingIntent);
54  
55 // Tell the user about what we did.
56 Toast.makeText(AndroidAlarmService.this, "Cancel!", Toast.LENGTH_LONG).show();
57  
58  
59 }});
60  
61 }
62 }

[代码] MyAlarmService.java

view source
 
print?
01 package com.exercise.AndroidAlarmService;
02  
03 import android.app.Service;
04 import android.content.Intent;
05 import android.os.IBinder;
06 import android.widget.Toast;
07  
08 public class MyAlarmService extends Service {
09  
10 @Override
11 public void onCreate() {
12 // TODO Auto-generated method stub
13 Toast.makeText(this, "MyAlarmService.onCreate()", Toast.LENGTH_LONG).show();
14 }
15  
16 @Override
17 public IBinder onBind(Intent intent) {
18 // TODO Auto-generated method stub
19 Toast.makeText(this, "MyAlarmService.onBind()", Toast.LENGTH_LONG).show();
20 return null;
21 }
22  
23 @Override
24 public void onDestroy() {
25 // TODO Auto-generated method stub
26 super.onDestroy();
27 Toast.makeText(this, "MyAlarmService.onDestroy()", Toast.LENGTH_LONG).show();
28 }
29  
30 @Override
31 public void onStart(Intent intent, int startId) {
32 // TODO Auto-generated method stub
33 super.onStart(intent, startId);
34 Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG).show();
35 }
36  
37 @Override
38 public boolean onUnbind(Intent intent) {
39 // TODO Auto-generated method stub
40 Toast.makeText(this, "MyAlarmService.onUnbind()", Toast.LENGTH_LONG).show();
41 return super.onUnbind(intent);
42 }
43  
44 }

[代码] AndroidManifest.xml

view source
 
print?
01 <?xml version="1.0" encoding="utf-8"?>
02 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
03 package="com.exercise.AndroidAlarmService"
04 android:versionCode="1"
05 android:versionName="1.0">
06 <application android:icon="@drawable/icon" android:label="@string/app_name">
07 <activity android:name=".AndroidAlarmService"
08 android:label="@string/app_name">
09 <intent-filter>
10 <action android:name="android.intent.action.MAIN" />
11 <category android:name="android.intent.category.LAUNCHER" />
12 </intent-filter>
13 </activity>
14 <service android:name=".MyAlarmService" />
15 </application>
16 <uses-sdk android:minSdkVersion="4" />
17  
18 </manifest>