现在讲一下一款简单的闹钟软件,那么就要说一下SharedPreferences,SharedPreferences是Android中最容易理解的数据存储技术,实际上SharedPreferences处理的就是一个key-value(键值对)SharedPreferences常用来存储一些轻量级的数据。
那么看看其布局文件main.xml的代码:
我们用绝对布局的layout_x以及layout_y来排列控件的位置。
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.naozhong.MainActivity"
android:background="@drawable/psb">"
<DigitalClock
android:id="@+id/dClock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="83dp"
android:layout_y="0dp"
android:textColor="@drawable/blue"
android:textSize="40sp"
android:gravity="center"/>
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="10dp"
android:layout_y="62dp"
android:text="@string/str_title3"
android:textColor="@drawable/black"
android:textSize="20sp" />
<Button
android:id="@+id/mButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="150dp"
android:layout_y="50dp"
android:text="@string/str_button1"
android:textColor="@drawable/black"
android:textSize="18sp" />
<TextView
android:id="@+id/setTime1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="29dp"
android:layout_y="120dp"
android:text="@string/str_default"
android:textColor="@drawable/red"
android:textSize="18sp" />
<Button
android:id="@+id/mButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="150dp"
android:layout_y="105dp"
android:text="@string/str_button2"
android:textColor="@drawable/black"
android:textSize="18sp" />
<TextView
android:id="@+id/text5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="10dp"
android:layout_y="170dp"
android:text="@string/str_title4"
android:textColor="@drawable/black"
android:textSize="20sp" />
<Button
android:id="@+id/mButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="150dp"
android:layout_y="160dp"
android:text="@string/str_button1"
android:textColor="@drawable/black"
android:textSize="18sp" />
<TextView
android:id="@+id/setTime5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="29dp"
android:layout_y="229dp"
android:text="@string/str_default"
android:textColor="@drawable/red"
android:textSize="18sp" />
<Button
android:id="@+id/mButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="150dp"
android:layout_y="215dp"
android:text="@string/str_button2"
android:textColor="@drawable/black"
android:textSize="18sp" />
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="14dp"
android:layout_y="280dp"
android:text="@string/str_title2"
android:textColor="@drawable/black"
android:textSize="18sp" />
<Button
android:id="@+id/mButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="150dp"
android:layout_y="270dp"
android:text="@string/str_button1"
android:textColor="@drawable/black"
android:textSize="18sp" />
<TextView
android:id="@+id/setTime2"
android:layout_width="170px"
android:layout_height="wrap_content"
android:layout_x="29dp"
android:layout_y="339dp"
android:text="@string/str_default"
android:textColor="@drawable/red"
android:textSize="18sp" />
<Button
android:id="@+id/mButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="150dp"
android:layout_y="325dp"
android:text="@string/str_button2"
android:textColor="@drawable/black"
android:textSize="18sp" />
</AbsoluteLayout>
然后我们还需要一个.xml文件对重复提醒的闹钟进行设置,我们定义为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="http://schemas.android.com/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
android:id="@+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/str_text3"
android:textColor="@drawable/white"
android:textSize="16sp"
android:layout_x="180px"
android:layout_y="172px"
>
</TextView>
</AbsoluteLayout>
这些东西我就不做过多的解释,对于一些辅助的东西,例如什么string.xml;color.xml;style.xml这些我就仅仅贴一下代码:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">love闹钟</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="str_button1">设置闹钟</string>
<string name="str_button2">删除闹钟</string>
<string name="str_title2">重复响起的闹钟</string>
<string name="str_title3">wq_love闹钟一</string>
<string name="str_title4">wq_love闹钟二</string>
<string name="str_default">目前无设置</string>
<string name="str_text1">开始时间</string>
<string name="str_text2">重复响起的闹钟</string>
<string name="str_text3">秒</string>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<drawable name="white">#FFFFFFFF</drawable>
<drawable name="other">#FF00FFF0</drawable>
<drawable name="black">#000000</drawable>
<drawable name="blue">#0000FF</drawable>
<drawable name="red">#FF0000</drawable>
</resources>
现在我们重点讲的就是.java类中的代码:
在mainActivity.java中,我们首先定义TextView中的三个地址
TextView setTime1;
TextView setTime2;
TextView setTime3;
然后是六个Button按钮地址,这些都是需要声明的,不过声明的方式有很多,可以自行选择:
Button mButton1;
Button mButton2;
Button mButton3;
Button mButton4;
Button mButton5;
Button mButton6;
然后就是可以显示在TextView上的string内容以及一个初始内容:
String time1String=null;
String time2String=null;
String time3String=null;
String defaultString="目前无设置";
然后就是弹出对话框:
AlertDialog builder=null;
还有一个日历初始化对象:
Calendar c=Calendar.getInstance();
在这些完成以后就进入了正题了:
SharedPreferences settings=getPreserences(Activity.MODE_PRIVATE);
time1String=settings.getString("TIME1",defaultString);
time2String=settings.getString("TIME2",defaultString);
time3String=settings.getString("TIME3",defaultString);
InitButton1();
InitButton2();
InitButton3();
InitButton4();
InitButton5();
InitButton6();
setTime1.setText(time1String);
setTime2.setText(time2String);
setTime3.setText(time3String);
然后是六个InitButton:
分别为:
private void InitButton1(){
setTime1=(TextView)findViewById(R.id.setTime1);
mButton1=(Button)findViewById(R.id.mButton1);
mButton1.setOnClickLinstener(new View.OnClickLinstener(){
public void onClick(View arg0){
c.setTimeInMillis(System.currentTimeMillis());
int mHour=c.get(Calendar.HOUR_OF_DAY);
int mMinute=c.get(Calendar.MINUTE);
new TimePickerDialog(MainAcyivity.this,new TimePickerDialog.OnTimeSetListener(){
public void onTimeSet(TimePicker arg0,int arg1,int arg2){
c.setTimeInMillis(System.currentTimeMillis());
c.get(Calendar.HOUR_OF_DAY,arg1);
c.get(Calendar.MINUTE,arg2);
c.get(Calendar.SECOND,0);//秒
c.get(Calendar.MILLISECOND,0);//毫秒
Intent intent=new Intent(MainActivity.this,CallAlarm.class);
PendingIntent sender=PendingIntent.getBroadcase(MainActivity.this,0,intent,0);
AlarmManager am;
am=(AlarmManager)getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP,c,getTimeInMillis(),sender);
String tmpS=format(arg1)+": "+format(arg2);
setTime1.setText(tmpS);
SharedPreferences time1Share=getPreferences(0);
SharedPreferences.Editoe editor=time1Share.edit();
editor.commit();
Toast.makeText(MainActivity.this,"设置love_wq闹钟时间为"+tmpS,Toast.LENGTH_SHORT).show();
}
},mHour,mMinute,true).show();
}
});
}
这是设置闹钟一的时间,在第二个Button按钮中,我们要还原最初的设置,也就是把时间删除掉:
private void InitButton2(){
mButton2=(Button)findViewById(R.id.mButton2);
mButton2.setOnClickListener(new View.OnClickListener(){
public void onClick(View arg0){
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,"love_wq闹钟已经被删除!",Toast.LENGTH_SHOET).show();
setTime1.setText("目前无设置");
sharedPreferences time1Share=getPreferences(0);
sharedPreferences.Editor editor=time1Share.edit();
editor.commit();
}
});
}
接下来的InitButton3和InitButton4和上面的基本相同,这里就不加说明。
对于InitButton5和InitButton6两个按钮是控制闹钟的重复响铃的,其代码如下:
private 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.ic_launcher)
.setTitle("设置")
.setView(setView)
.setPositiveButton("确定",new DialogInterface.OnClickListener(){
EditText ed=(EditText)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);
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="设置 wq_love闹钟为"+tmpS+"开始,重复间隔为"+times/1000+"秒";
setTime2.setText("设置 wq_love闹钟为"+tmpS+"开始,重复间隔为"+times/1000+"秒");
SharedPreferences time3Share=getPreferences(2);
SharedPreferences.Editor editor=time3Share.edit();
editor.putString("TIME3", subStr);
editor.commit();
Toast.makeText(MainActivity.this, "设置wq_love闹钟为"+tmpS+"开始,重复间隔为"+times/1000+"秒", Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("取消",new DialogInterface.OnClickListener(){
public void onClick(DialogInterface arg0,int arg1){
}
}).create();
mButton5=(Button)findViewById(R.id.mButton3);
mButton5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
c.setTimeInMillis(System.currentTimeMillis());
tPicker.setCurrentHour(c.get(Calendar.HOUR_OF_DAY));
tPicker.setCurrentMinute(c.get(Calendar.MINUTE));
di.show();
}
});
}
private void InitButton6() {
mButton6=(Button)findViewById(R.id.mButton4);
mButton6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
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, "闹钟时间删除", Toast.LENGTH_SHORT).show();
setTime2.setText("目前无设置");
SharedPreferences time3Share=getPreferences(2);
SharedPreferences.Editor editor=time3Share.edit();
editor.putString("TIME3", "目前无设置");
editor.commit();
}
});
}
public boolean onKeyUp(int KeyCode,KeyEvent event ){
if(KeyCode==KeyEvent.KEYCODE_BACK){
builder=new AlertDialog.Builder(MainActivity.this)
.setIcon(R.drawable.ic_launcher)
.setTitle("温馨提示:")
.setMessage("您是否要退出闹钟程序?")
.setPositiveButton("确定",new DialogInterface.OnClickListener(){
public void onClick(DialogInterface arg0, int arg1) {
MainActivity.this.finish();
}
})
.setNegativeButton("取消",new DialogInterface.OnClickListener(){
public void onClick(DialogInterface arg0,int arg1){
builder.dismiss();
}
}).show();
}
return true;
}
private String format(int x){
String s=""+x;
if(s.length()==1){
s="0"+s;
}
return s;
}
接下来,我们再来看看CallAlarm.java的代码:
Intent intent=new Intent(arg0,AlarmAlert.class);
Bundle bundleRet=new Bundle();
bundleRet.putString("STR_CALLER", "");
intent.putExtras(bundleRet);
intent.addFlags(intent.FLAG_ACTIVITY_NEW_TASK);
arg0.startActivity(intent);
AlarmAlert的代码如下:
new AlertDialog.Builder(AlarmAlert.this)
.setIcon(R.drawable.ic_launcher)
.setTitle("wq_love闹钟响了")
.setMessage("快完成你制定的计划")
.setPositiveButton("关掉它",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
AlarmAlert.this.finish();
}
}).show();
但是注意要在AndroidManifest.xml中声明一下CallAlarm.java这个activity。基本就是这样了。