Android Broadcast调用Service做的一个定时器
2010-08-20 02:13:00 来源:WEB开发网Java代码
1. public class AlarmActivity extends Activity implements OnClickListener {
2. private Button b_call_service, b_exit_service;
3. private Intent intent;
4. private PendingIntent p_intent;
5.
6. public void onCreate(Bundle icicle) {
7. super.onCreate(icicle);
8. setContentView(R.layout.main);
9. b_call_service = (Button) findViewById(R.id.call_alarm_service);
10. b_call_service.setOnClickListener(this);
11. b_exit_service = (Button) findViewById(R.id.exit);
12. b_exit_service.setOnClickListener(this);
13.
14. intent = new Intent(this, AlarmReceiver.class);
15. p_intent = PendingIntent.getBroadcast(this, 0, intent, 0);
16. }
17.
18. public void onClick(View arg0) {
19. if (arg0 == b_call_service) {
20. setTitle("Waiting... 5s后Alarm启动");
21.
22. Calendar calendar = Calendar.getInstance();
23. calendar.setTimeInMillis(System.currentTimeMillis());
24. calendar.add(Calendar.SECOND, 5);
25.
26. AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
27. am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), p_intent);
28. }
29.
30. if (arg0 == b_exit_service) {
31. setTitle("Alarm关闭");
32. AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
33. am.cancel(p_intent);
34. }
35. }
36. }
public class AlarmActivity extends Activity implements OnClickListener {
private Button b_call_service, b_exit_service;
private Intent intent;
private PendingIntent p_intent;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
- ››Android 当修改一些代码时,使用什么编译命令可以最...
- ››Android 如何添加一个apk使模拟器和真机都编译进去...
- ››Android 修改Camera拍照的默认保存路径
- ››Android 如何修改默认输入法
- ››android开发中finish()和System.exit(0)的区别
- ››Android手势识别简单封装类
- ››android中查看项目数字证书的两种方法
- ››Android中获取IMEI码的办法
- ››android 相机报错 setParameters failed
- ››Android重启运用程序的代码
- ››Android为ListView的Item设置不同的布局
- ››android bitmap与base64字符串的互相转换
更多精彩
赞助商链接