package com.Reminder;
import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
/**
* trigger the Broadcast event and set the alarm
*/
public class ReminderSetting extends Activity {
Button btnEnable;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* create a button. When you click the button, the alarm clock is enabled */
btnEnable=(Button)findViewById(R.id.btnEnable);
btnEnable.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setReminder(true);
}
});
}
/**
* Set the alarm
*
* @param b whether enable the Alarm clock or not
*/
private void setReminder(boolean b) {
// get the AlarmManager instance
AlarmManager am= (AlarmManager) getSystemService(ALARM_SERVICE);
// create a PendingIntent that will perform a broadcast
PendingIntent pi= PendingIntent.getBroadcast(ReminderSetting.this, 0, new Intent(this,MyReceiver.class), 0);
if(b){
// just use current time as the Alarm time.
Calendar c=Calendar.getInstance();
// schedule an alarm
am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi);
}
else{
// cancel current alarm
am.cancel(pi);
}
}
}
package com.Reminder;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
/**
* Receive the broadcast and start the activity that will show the alarm
*/
public class MyReceiver extends BroadcastReceiver {
/**
* called when the BroadcastReceiver is receiving an Intent broadcast.
*/
@Override
public void onReceive(Context context, Intent intent) {
/* start another activity - MyAlarm to display the alarm */
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setClass(context, MyAlarm.class);
context.startActivity(intent);
}
}
<receiver android:name=".MyReceiver">
<intent-filter>
<action android:name= "com.Reminder.MyReceiver" />
</intent-filter>
</receiver>
package com.Reminder;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore.Audio;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
/**
* Display the alarm information
*/
public class MyAlarm extends Activity {
/**
* An identifier for this notification unique within your application
*/
public static final int NOTIFICATION_ID=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_alarm);
// create the instance of NotificationManager
final NotificationManager nm=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// create the instance of Notification
Notification n=new Notification();
/* set the sound of the alarm. There are two way of setting the sound */
// n.sound=Uri.parse("file:///sdcard/alarm.mp3");
n.sound=Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "20");
// Post a notification to be shown in the status bar
nm.notify(NOTIFICATION_ID, n);
/* display some information */
TextView tv=(TextView)findViewById(R.id.tvNotification);
tv.setText("Hello, it's time to bla bla...");
/* the button by which you can cancel the alarm */
Button btnCancel=(Button)findViewById(R.id.btnCancel);
btnCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
nm.cancel(NOTIFICATION_ID);
finish();
}
});
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有