Android---Android簡單鬧鍾程序


Android簡單鬧鍾程序

  • 來源:iteye
  • 作者:edison_cool911
  • 字體:【  
  • 時間:2011-10-14
  • 點擊:3929
  • 收藏本文
 

這個應用實現了簡單的鬧鍾功能,關閉程序的狀態中也可以進行鬧鍾的提醒。

這里遺留了一個問題: 如果我通過應用本身關閉程序,是可以實現我上述所說的功能,但是如果我在手機進程管理中實現應用程序的關閉操作,這個提醒就不起作用了,我很疑惑的是,我通過應用程序本身關閉了后,在進程中也是查看不到這個應用了的。所以哪位知道的幫留個言指點下,謝謝。

ClockDemo.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package   com.example.clock;
 
import   java.util.Calendar;
import   android.app.Activity;
import   android.app.AlertDialog;
import   android.app.TimePickerDialog;
import   android.content.DialogInterface;
import   android.os.Bundle;
import   android.view.KeyEvent;
import   android.view.View;
import   android.widget.Button;
import   android.widget.TimePicker;
 
public   class   ClockDemo  extends   Activity {
 
     private   Button mSet;
     Calendar mCalendar = Calendar.getInstance();
 
     /** Called when the activity is first created. */
     @Override
     public   void   onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.main);
         ObjectPool.mAlarmHelper =  new   AlarmHelper( this );
         mSet = (Button) findViewById(R.id.mSet);
         setListener();
     }
 
     public   void   setListener() {
         mSet.setOnClickListener( new   View.OnClickListener() {
             public   void   onClick(View v) {
                 mCalendar.setTimeInMillis(System.currentTimeMillis());
                 int   mHour = mCalendar.get(Calendar.HOUR_OF_DAY);
                 int   mMinute = mCalendar.get(Calendar.MINUTE);
                 new   TimePickerDialog(ClockDemo. this ,
                         new   TimePickerDialog.OnTimeSetListener() {
                             public   void   onTimeSet(TimePicker view,
                                     int   hourOfDay,  int   minute) {
                                 mCalendar.setTimeInMillis(System
                                         .currentTimeMillis());
                                 mCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
                                 mCalendar.set(Calendar.MINUTE, minute);
                                 mCalendar.set(Calendar.SECOND,  0 );
                                 mCalendar.set(Calendar.MILLISECOND,  0 );
                                 ObjectPool.mAlarmHelper.openAlarm( 32 ,  "ddd" ,
                                         "ffff" , mCalendar.getTimeInMillis());
                             }
                         }, mHour, mMinute,  true ).show();
             }
         });
     }
 
     @Override
     public   boolean   onKeyDown( int   keyCode, KeyEvent event) {
         if   (keyCode == KeyEvent.KEYCODE_BACK) {
             showBackDialog();
             return   true ;
         }
         return   super .onKeyDown(keyCode, event);
     }
 
     /** Give the tip when exit the application. */
     public   void   showBackDialog() {
         final   AlertDialog.Builder builder =  new   AlertDialog.Builder( this );
         builder.setTitle( "提示" )
                 .setIcon(R.drawable.icon)
                 .setMessage( "是否退出?" )
                 .setPositiveButton( "sure" ,
                         new   DialogInterface.OnClickListener() {
                             public   void   onClick(DialogInterface dialog,
                                     int   which) {
                                 System.exit( 0 );
                                 android.os.Process
                                         .killProcess(android.os.Process.myPid());
 
                                 dialog.dismiss();
                             }
                         })
                 .setNegativeButton( "cancel" ,
                         new   DialogInterface.OnClickListener() {
                             public   void   onClick(DialogInterface dialog,
                                     int   which) {
                                 dialog.dismiss();
                             }
                         });
         AlertDialog ad = builder.create();
         ad.show();
     }
 
}

AlarmHelper.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package   com.example.clock;
 
import   android.app.AlarmManager;
import   android.app.PendingIntent;
import   android.content.Context;
import   android.content.Intent;
 
public   class   AlarmHelper {
 
     private   Context c;
     private   AlarmManager mAlarmManager;
 
     public   AlarmHelper(Context c) {
         this .c = c;
         mAlarmManager = (AlarmManager) c
                 .getSystemService(Context.ALARM_SERVICE);
     }
 
     public   void   openAlarm( int   id, String title, String content,  long   time) {
         Intent intent =  new   Intent();
         intent.putExtra( "_id" , id);
         intent.putExtra( "title" , title);
         intent.putExtra( "content" , content);
         intent.setClass(c, CallAlarm. class );
         PendingIntent pi = PendingIntent.getBroadcast(c, id, intent,
                 PendingIntent.FLAG_UPDATE_CURRENT);
         mAlarmManager.set(AlarmManager.RTC_WAKEUP, time, pi);
     }
 
     public   void   closeAlarm( int   id, String title, String content) {
         Intent intent =  new   Intent();
         intent.putExtra( "_id" , id);
         intent.putExtra( "title" , title);
         intent.putExtra( "content" , content);
         intent.setClass(c, CallAlarm. class );
         PendingIntent pi = PendingIntent.getBroadcast(c, id, intent,  0 );
         mAlarmManager.cancel(pi);
     }
}

CallAlarm.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package   com.example.clock;
 
import   android.content.BroadcastReceiver;
import   android.content.Context;
import   android.content.Intent;
 
public   class   CallAlarm  extends   BroadcastReceiver {
     @Override
     public   void   onReceive(Context context, Intent intent) {
         intent.setClass(context, AlarmAlert. class );
         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         context.startActivity(intent);
     }
}

AlarmAlert.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package   com.example.clock;
 
import   android.app.Activity;
import   android.app.AlertDialog;
import   android.content.DialogInterface;
import   android.os.Bundle;
 
public   class   AlarmAlert  extends   Activity {
     @Override
     protected   void   onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         new   AlertDialog.Builder(AlarmAlert. this )
                 .setIcon(R.drawable.clock)
                 .setTitle( "ddd" )
                 .setMessage( "fff" )
                 .setPositiveButton( "ddd" ,
                         new   DialogInterface.OnClickListener() {
                             public   void   onClick(DialogInterface dialog,
                                     int   whichButton) {
                                 System.exit( 0 );
                                 android.os.Process
                                         .killProcess(android.os.Process.myPid());
                             }
                         }).show();
     }
}

ObjectPool.java

?
1
2
3
4
5
6
public   class   ObjectPool {
 
     /** The alarm object. */
     public   static   AlarmHelper mAlarmHelper;
 
}

main.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<? 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" >
     < DigitalClock
         android:id = "@+id/dClock"
         android:layout_width = "wrap_content"
         android:layout_height = "wrap_content"
         android:textSize = "40sp"
         android:textColor = "@drawable/blue"   />
     < Button
         android:id = "@+id/mSet"
         android:layout_width = "100dp"
         android:layout_height = "wrap_content"
         android:text = "@string/str_button1"
         android:textColor = "@drawable/red"
         android:textSize = "18sp"   />
</ LinearLayout >

最后貼個配置文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<? xml   version = "1.0"   encoding = "utf-8" ?>
< manifest   xmlns:android = "http://schemas.android.com/apk/res/android"
     package = "com.example.clock"   android:versionCode = "1"
     android:versionName = "1.0" >
     < uses-sdk   android:minSdkVersion = "7"   />
     < application   android:icon = "@drawable/icon"   android:label = "@string/app_name" >
         < activity   android:name = ".ClockDemo"   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 = ".CallAlarm"   />
         < activity   android:name = ".AlarmAlert"   android:label = "@string/app_name"   />
     </ application >
</ manifest >

看了那么多,估計某些人又嫌煩了,我只貼上代碼的目的是希望大家能看代碼,而不是電腦上下了一個又一個的源文件,只知道用而不關心其中的技術點,這樣就不能舉一番三了。

為了不同的同學,還是貼上源文件吧在。

源碼下載:ClockDemo.rar


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM