【移動應(yīng)用開發(fā)技術(shù)】Android實(shí)現(xiàn)鬧鐘小程序_第1頁
【移動應(yīng)用開發(fā)技術(shù)】Android實(shí)現(xiàn)鬧鐘小程序_第2頁
【移動應(yīng)用開發(fā)技術(shù)】Android實(shí)現(xiàn)鬧鐘小程序_第3頁
【移動應(yīng)用開發(fā)技術(shù)】Android實(shí)現(xiàn)鬧鐘小程序_第4頁
【移動應(yīng)用開發(fā)技術(shù)】Android實(shí)現(xiàn)鬧鐘小程序_第5頁
已閱讀5頁,還剩24頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

【移動應(yīng)用開發(fā)技術(shù)】Android實(shí)現(xiàn)鬧鐘小程序

這篇文章主要介紹Android實(shí)現(xiàn)鬧鐘小程序,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!Android是一種基于Linux內(nèi)核的自由及開放源代碼的操作系統(tǒng),主要使用于移動設(shè)備,如智能手機(jī)和平板電腦,由美國Google公司和開放手機(jī)聯(lián)盟領(lǐng)導(dǎo)及開發(fā)。最近寫了個(gè)鬧鐘的程序,看到SharedPreferences在一個(gè)程序中可以共享數(shù)據(jù),SharedPreferences是一個(gè)輕量級的鍵值存儲機(jī)制,只可以存儲基本數(shù)據(jù)類型。我就拿來用用,沒想到SharedPreferences太好了,真是輕量級的保存數(shù)據(jù)的好的工具,比sqlite好用多了!以后我又多了一種編程思想了,呵呵,所以現(xiàn)在分享給大家,特別注意這點(diǎn):這個(gè)無法直接在多個(gè)程序間共享Preferences數(shù)據(jù)。程序關(guān)閉再打開時(shí)間仍然保留你上次設(shè)置的時(shí)間。這就是Preferences的作用!

程序歡迎界面:點(diǎn)擊設(shè)置鬧鐘界面:

點(diǎn)擊鬧鐘設(shè)置中的設(shè)置后的界面:

鬧鐘時(shí)間到了彈出dialog:

設(shè)置重復(fù)想起鬧鐘后的界面:

點(diǎn)擊返回鍵彈出的提示:下面請看代碼:一、MainActivity中的代碼:package

.daming;

import

java.util.Calendar;

import

android.app.Activity;

import

android.app.AlarmManager;

import

android.app.AlertDialog;

import

android.app.PendingIntent;

import

android.app.TimePickerDialog;

import

android.content.DialogInterface;

import

android.content.Intent;

import

android.content.SharedPreferences;

import

android.os.Bundle;

import

android.view.KeyEvent;

import

android.view.LayoutInflater;

import

android.view.MotionEvent;

import

android.view.View;

import

android.widget.Button;

import

android.widget.EditText;

import

android.widget.TextView;

import

android.widget.TimePicker;

import

android.widget.Toast;

public

class

MainActivity

extends

Activity

{

TextView

setTime1;

TextView

setTime2;

TextView

setTime3;

Button

mButton1;

Button

mButton2;

Button

mButton3;

Button

mButton4;

Button

mButton5;

Button

mButton6;

String

time1String

=

null;

String

time2String

=

null;

String

time3String

=

null;

String

defalutString

=

"目前無設(shè)置";

AlertDialog

builder

=

null;

Calendar

c=Calendar.getInstance();

@Override

public

void

onCreate(Bundle

savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

//取得活動的Preferences對象

SharedPreferences

settings

=

getPreferences(Activity.MODE_PRIVATE);

time1String

=

settings.getString("TIME1",

defalutString);

time2String

=

settings.getString("TIME2",

defalutString);

time3String

=

settings.getString("TIME3",

defalutString);

InitButton1();

InitButton2();

InitButton3();

InitButton4();

InitButton5();

InitButton6();

setTime1.setText(time1String);

setTime3.setText(time2String);

setTime2.setText(time3String);

}

public

void

InitButton1()

{

setTime1=(TextView)

findViewById(R.id.setTime1);

mButton1=(Button)findViewById(R.id.mButton1);

mButton1.setOnClickListener(new

View.OnClickListener()

{

public

void

onClick(View

v)

{

c.setTimeInMillis(System.currentTimeMillis());

int

mHour=c.get(Calendar.HOUR_OF_DAY);

int

mMinute=c.get(Calendar.MINUTE);

new

TimePickerDialog(MainActivity.this,

new

TimePickerDialog.OnTimeSetListener()

{

public

void

onTimeSet(TimePicker

view,int

hourOfDay,

int

minute)

{

c.setTimeInMillis(System.currentTimeMillis());

c.set(Calendar.HOUR_OF_DAY,hourOfDay);

c.set(Calendar.MINUTE,minute);

c.set(Calendar.SECOND,0);

c.set(Calendar.MILLISECOND,0);

Intent

intent

=

new

Intent(MainActivity.this,

CallAlarm.class);

PendingIntent

sender=PendingIntent.getBroadcast(

MainActivity.this,0,

intent,

0);

AlarmManager

am;

am

=

(AlarmManager)getSystemService(ALARM_SERVICE);

am.set(AlarmManager.RTC_WAKEUP,

c.getTimeInMillis(),

sender

);

String

tmpS=format(hourOfDay)+":"+format(minute);

setTime1.setText(tmpS);

//SharedPreferences保存數(shù)據(jù),并提交

SharedPreferences

time1Share

=

getPreferences(0);

SharedPreferences.Editor

editor

=

time1Share.edit();

editor.putString("TIME1",

tmpS);

mit();

Toast.makeText(MainActivity.this,"設(shè)置大明鬧鐘時(shí)間為"+tmpS,

Toast.LENGTH_SHORT)

.show();

}

},mHour,mMinute,true).show();

}

});

}

public

void

InitButton2()

{

mButton2=(Button)

findViewById(R.id.mButton2);

mButton2.setOnClickListener(new

View.OnClickListener()

{

public

void

onClick(View

v)

{

Intent

intent

=

new

Intent(MainActivity.this,

CallAlarm.class);

PendingIntent

sender=PendingIntent.getBroadcast(

MainActivity.this,0,

intent,

0);

AlarmManager

am;

am

=(AlarmManager)getSystemService(ALARM_SERVICE);

am.cancel(sender);

Toast.makeText(MainActivity.this,"大明鬧鐘時(shí)間刪除",

Toast.LENGTH_SHORT).show();

setTime1.setText("目前無設(shè)置");

SharedPreferences

time1Share

=

getPreferences(0);

SharedPreferences.Editor

editor

=

time1Share.edit();

editor.putString("TIME1",

"目前無設(shè)置");

mit();

}

});

}

public

void

InitButton3()

{

setTime3=(TextView)

findViewById(R.id.setTime5);

mButton3=(Button)findViewById(R.id.mButton5);

mButton3.setOnClickListener(new

View.OnClickListener()

{

public

void

onClick(View

v)

{

c.setTimeInMillis(System.currentTimeMillis());

int

mHour=c.get(Calendar.HOUR_OF_DAY);

int

mMinute=c.get(Calendar.MINUTE);

new

TimePickerDialog(MainActivity.this,

new

TimePickerDialog.OnTimeSetListener()

{

public

void

onTimeSet(TimePicker

view,int

hourOfDay,

int

minute)

{

c.setTimeInMillis(System.currentTimeMillis());

c.set(Calendar.HOUR_OF_DAY,hourOfDay);

c.set(Calendar.MINUTE,minute);

c.set(Calendar.SECOND,0);

c.set(Calendar.MILLISECOND,0);

Intent

intent

=

new

Intent(MainActivity.this,

CallAlarm.class);

PendingIntent

sender=PendingIntent.getBroadcast(

MainActivity.this,1,

intent,

0);

AlarmManager

am;

am

=

(AlarmManager)getSystemService(ALARM_SERVICE);

am.set(AlarmManager.RTC_WAKEUP,

c.getTimeInMillis(),

sender

);

String

tmpS=format(hourOfDay)+":"+format(minute);

setTime3.setText(tmpS);

//SharedPreferences保存數(shù)據(jù),并提交

SharedPreferences

time2Share

=

getPreferences(1);

SharedPreferences.Editor

editor

=

time2Share.edit();

editor.putString("TIME2",

tmpS);

mit();

Toast.makeText(MainActivity.this,"設(shè)置大明鬧鐘時(shí)間為"+tmpS,

Toast.LENGTH_SHORT)

.show();

}

},mHour,mMinute,true).show();

}

});

}

public

void

InitButton4()

{

mButton4=(Button)

findViewById(R.id.mButton6);

mButton4.setOnClickListener(new

View.OnClickListener()

{

public

void

onClick(View

v)

{

Intent

intent

=

new

Intent(MainActivity.this,

CallAlarm.class);

PendingIntent

sender=PendingIntent.getBroadcast(

MainActivity.this,0,

intent,

0);

AlarmManager

am;

am

=(AlarmManager)getSystemService(ALARM_SERVICE);

am.cancel(sender);

Toast.makeText(MainActivity.this,"大明鬧鐘時(shí)間刪除",

Toast.LENGTH_SHORT).show();

setTime3.setText("目前無設(shè)置");

//SharedPreferences保存數(shù)據(jù),并提交

SharedPreferences

time2Share

=

getPreferences(1);

SharedPreferences.Editor

editor

=

time2Share.edit();

editor.putString("TIME2",

"目前無設(shè)置");

mit();

}

});

}

public

void

InitButton5()

{

setTime2=(TextView)

findViewById(R.id.setTime2);

LayoutInflater

factory

=

LayoutInflater.from(this);

final

View

setView

=

factory.inflate(R.layout.timeset,null);

final

TimePicker

tPicker=(TimePicker)setView

.findViewById(R.id.tPicker);

tPicker.setIs24HourView(true);

final

AlertDialog

di=new

AlertDialog.Builder(MainActivity.this)

.setIcon(R.drawable.clock)

.setTitle("設(shè)置")

.setView(setView)

.setPositiveButton("確定",

new

DialogInterface.OnClickListener()

{

public

void

onClick(DialogInterface

dialog,

int

which)

{

EditText

ed=(EditText)setView.findViewById(R.id.mEdit);

int

times=Integer.parseInt(ed.getText().toString())

*1000;

c.setTimeInMillis(System.currentTimeMillis());

c.set(Calendar.HOUR_OF_DAY,tPicker.getCurrentHour());

c.set(Calendar.MINUTE,tPicker.getCurrentMinute());

c.set(Calendar.SECOND,0);

c.set(Calendar.MILLISECOND,0);

Intent

intent

=

new

Intent(MainActivity.this,

CallAlarm.class);

PendingIntent

sender

=

PendingIntent.getBroadcast(

MainActivity.this,1,

intent,

0);

AlarmManager

am;

am

=

(AlarmManager)getSystemService(ALARM_SERVICE);

am.setRepeating(AlarmManager.RTC_WAKEUP,

c.getTimeInMillis(),times,sender);

String

tmpS=format(tPicker.getCurrentHour())+":"+

format(tPicker.getCurrentMinute());

String

subStr

=

"設(shè)置大明鬧鐘時(shí)間為"+tmpS+

"開始,重復(fù)間隔為"+times/1000+"秒";

setTime2.setText("設(shè)置大明鬧鐘時(shí)間為"+tmpS+

"開始,重復(fù)間隔為"+times/1000+"秒");

//SharedPreferences保存數(shù)據(jù),并提交

SharedPreferences

time3Share

=

getPreferences(2);

SharedPreferences.Editor

editor

=

time3Share.edit();

editor.putString("TIME3",

subStr);

mit();

Toast.makeText(MainActivity.this,"設(shè)置大明鬧鐘為"+tmpS+

"開始,重復(fù)間隔為"+times/1000+"秒",

Toast.LENGTH_SHORT).show();

}

})

.setNegativeButton("取消",

new

DialogInterface.OnClickListener()

{

public

void

onClick(DialogInterface

dialog,

int

which)

{

}

}).create();

mButton5=(Button)

findViewById(R.id.mButton3);

mButton5.setOnClickListener(new

View.OnClickListener()

{

public

void

onClick(View

v)

{

c.setTimeInMillis(System.currentTimeMillis());

tPicker.setCurrentHour(c.get(Calendar.HOUR_OF_DAY));

tPicker.setCurrentMinute(c.get(Calendar.MINUTE));

di.show();

}

});

}

public

void

InitButton6()

{

mButton6=(Button)

findViewById(R.id.mButton4);

mButton6.setOnClickListener(new

View.OnClickListener()

{

public

void

onClick(View

v)

{

Intent

intent

=

new

Intent(MainActivity.this,

CallAlarm.class);

PendingIntent

sender

=

PendingIntent.getBroadcast(

MainActivity.this,1,

intent,

0);

AlarmManager

am;

am

=

(AlarmManager)getSystemService(ALARM_SERVICE);

am.cancel(sender);

Toast.makeText(MainActivity.this,"鬧鐘時(shí)間刪除",

Toast.LENGTH_SHORT).show();

setTime2.setText("目前無設(shè)置");

//SharedPreferences保存數(shù)據(jù),并提交

SharedPreferences

time3Share

=

getPreferences(2);

SharedPreferences.Editor

editor

=

time3Share.edit();

editor.putString("TIME3",

"目前無設(shè)置");

mit();

}

});

}

@Override

public

boolean

onKeyUp(int

keyCode,

KeyEvent

event)

{

if(keyCode

==

KeyEvent.KEYCODE_BACK){

builder

=

new

AlertDialog.Builder(MainActivity.this)

.setIcon(R.drawable.clock)

.setTitle("溫馨提示:")

.setMessage("您是否要退出大明鬧鐘程序!!!")

.setPositiveButton("確定",

new

DialogInterface.OnClickListener()

{

public

void

onClick(DialogInterface

dialog,

int

whichButton)

{

MainActivity.this.finish();

}

})

.setNegativeButton("取消",

new

DialogInterface.OnClickListener()

{

public

void

onClick(DialogInterface

dialog,

int

whichButton)

{

builder.dismiss();

}

}).show();

}

return

true;

}

private

String

format(int

x)

{

String

s=""+x;

if(s.length()==1)

s="0"+s;

return

s;

}

}二、CallAlarm中的代碼:package

.daming;

import

android.content.Context;

import

android.content.Intent;

import

android.content.BroadcastReceiver;

import

android.os.Bundle;

public

class

CallAlarm

extends

BroadcastReceiver

{

@Override

public

void

onReceive(Context

context,

Intent

intent)

{

Intent

i

=

new

Intent(context,

AlarmAlert.class);

Bundle

bundleRet

=

new

Bundle();

bundleRet.putString("STR_CALLER",

"");

i.putExtras(bundleRet);

i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

context.startActivity(i);

}

}三、AlarmAlert中的代碼:package

.daming;

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("大明鬧鐘響了!!")

.setMessage("快完成你制定的計(jì)劃吧!!!")

.setPositiveButton("關(guān)掉它",

new

DialogInterface.OnClickListener()

{

public

void

onClick(DialogInterface

dialog,

int

whichButton)

{

AlarmAlert.this.finish();

}

})

.show();

}

}四、main.xml布局文件的代碼:<?xml

version="1.0"

encoding="utf-8"?>

<AbsoluteLayout

xmlns:android="/apk/res/android"

android:id="@+id/layout1"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background="@drawable/other"

>

<DigitalClock

android:id="@+id/dClock"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textSize="40sp"

android:textColor="@drawable/blue"

android:layout_x="70px"

android:layout_y="32px"

>

</DigitalClock>

<TextView

android:id="@+id/text1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/str_title3"

android:textSize="20sp"

android:textColor="@drawable/black"

android:layout_x="10px"

android:layout_y="104px"

>

</TextView>

<Button

android:id="@+id/mButton1"

android:layout_width="wrap_content"

android:layout_height="40px"

android:text="@string/str_button1"

android:textColor="@drawable/black"

android:textSize="18sp"

android:layout_x="190px"

android:layout_y="102px"

>

</Button>

<TextView

android:id="@+id/setTime1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/str_default"

android:textColor="@drawable/red"

android:textSize="16sp"

android:layout_x="10px"

android:layout_y="149px"

>

</TextView>

<Button

android:id="@+id/mButton2"

android:layout_width="wrap_content"

android:layout_height="40px"

android:text="@string/str_button2"

android:textColor="@drawable/black"

android:textSize="18sp"

android:layout_x="190px"

android:layout_y="142px"

>

</Button>

<TextView

android:id="@+id/text5"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/str_title4"

android:textSize="20sp"

android:textColor="@drawable/black"

android:layout_x="10px"

android:layout_y="216px"

>

</TextView>

<Button

android:id="@+id/mButton5"

android:layout_width="wrap_content"

android:layout_height="40px"

android:text="@string/str_button1"

android:textColor="@drawable/black"

android:textSize="18sp"

android:layout_x="190px"

android:layout_y="212px"

>

</Button>

<TextView

android:id="@+id/setTime5"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/str_default"

android:textColor="@drawable/red"

android:textSize="16sp"

android:layout_x="10px"

android:layout_y="259px"

>

</TextView>

<Button

android:id="@+id/mButton6"

android:layout_width="wrap_content"

android:layout_height="40px"

android:text="@string/str_button2"

android:textColor="@drawable/black"

android:textSize="18sp"

android:layout_x="190px"

android:layout_y="252px"

>

</Button>

<TextView

android:id="@+id/text2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/str_title2"

android:textSize="20sp"

android:textColor="@drawable/black"

android:layout_x="10px"

android:layout_y="326px"

>

</TextView>

<Button

android:id="@+id/mButton3"

android:layout_width="wrap_content"

android:layout_height="40px"

android:text="@string/str_button1"

android:textColor="@drawable/black"

android:textSize="18sp"

android:layout_x="190px"

android:layout_y="322px"

>

</Button>

<TextView

android:id="@+id/setTime2"

android:layout_width="170px"

android:layout_height="wrap_content"

android:text="@string/str_default"

android:textColor="@drawable/red"

android:textSize="16sp"

android:layout_x="10px"

android:layout_y="369px"

>

</TextView>

<Button

android:id="@+id/mButton4"

android:layout_width="wrap_content"

android:layout_height="40px"

android:text="@string/str_button2"

android:textColor="@drawable/black"

android:textSize="18sp"

android:layout_x="190px"

android:layout_y="362px"

>

</Button>

</AbsoluteLayout>五、timeset.xml布局文件中的代碼:<?xml

version="1.0"

encoding="utf-8"?>

<AbsoluteLayout

android:id="@+id/layout2"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

xmlns:android="/apk/res/android"

>

<TextView

android:id="@+id/text1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/str_text1"

android:textColor="@drawable/white"

android:textSize="16sp"

android:layout_x="10px"

android:layout_y="32px"

>

</TextView>

<TextView

android:id="@+id/text2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/str_text2"

android:textColor="@drawable/white"

android:textSize="16sp"

android:layout_x="10px"

android:layout_y="172px"

>

</TextView>

<TimePicker

android:id="@+id/tPicker"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_x="100px"

android:layout_y="12px"

>

</TimePicker>

<EditText

android:id="@+id/mEdit"

android:layout_width="52px"

android:layout_height="40px"

android:text="15"

android:textSize="16sp"

android:layout_x="120px"

android:layout_y="162px"

>

</EditText>

<TextView

and

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論