简单闹钟程序

这个应用实现了简单的闹钟功能,关闭程序的状态中也可以进行闹钟的提醒.
遗留了一个问题:
如果我通过应用本身关闭程序,是可以实现我上述所说的功能,但是如果我在手机进程管理中实现应用程序的关闭操作,这个提醒就不起作用了,我很疑惑的是,我通过应用程序本身关闭了后,在进程中也是查看不到这个应用了的.所以哪位知道的帮留个言指点下,谢谢!!!!

ClockDemo.java:

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:

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:

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:

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:


public class ObjectPool {


/** The alarm object. */
public static AlarmHelper mAlarmHelper;

}


main.xml:

<?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>


最后贴个配置文件:

<?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>

看了那么多,估计某些人又嫌烦了,我只贴上代码的目的是希望大家能看代码,而不是电脑上下了一个又一个的源文件,只知道用而不关心其中的技术点,这样就不能举一番三了,O(∩_∩)O哈哈~
为了不同的同学,我还是贴上源文件吧在.....
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值