android提醒事件
来源:互联网 发布:遥感数据下载 编辑:程序博客网 时间:2024/05/18 06:25
首先参考的其他网上分享的资源。参考链接忘记了,谢谢他们的分享
1、先启动service
Intent intent = new Intent("创建包名");
setAlarmTime();
startService(intent);
Toast.makeText(AtyEditNote.this, "设置提醒了", Toast.LENGTH_SHORT).show();
2、LongRunningService.java
public class LongRunningServer extends Service {
private Calendar mCalendar ;
@Override
//通过alarmManager来实现时间控制,
public int onStartCommand(Intent intent, int flags, int startId) {
Log.v("my", "start "+getPackageName()+".service");
//接收sharedPreferences
//put the info
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
//reader可以修改Minutes,而改变提醒的时间l
//设置启动广播的时间,启动提醒
Calendar c = setCanlendarAlarmTime();
Log.v("my", "the setting alarm time is:"+c.getTimeInMillis());
//此处是设置没90分钟启动一次
//90分钟的毫秒数
// int minutes = 1000*10;
long minutes = c.getTimeInMillis()-Calendar.getInstance().getTimeInMillis();
//SystemClock.elapseReadtime()表示1970,1,1到今天的毫秒数
long triggerAtTime = SystemClock.elapsedRealtime()+minutes;
//此处设置AlarmReceive这个server
Intent i = new Intent(LongRunningServer.this,MyReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
//elpsed_realtime_wakeup表示定时服务的出发时间从系统开机记时开始,并且唤醒cpu
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
triggerAtTime, pi);
//主要是这个方法来设置启动提醒功能。第一个参数数用来设置提醒的类型,具体可以百度
//第二个参数是设置 提醒开始的时间
//第三个不是很清楚,intent启动服务的
Log.v("my", "triggerTime is:"+triggerAtTime);
Log.v("my", "the alarm date is:"+c.getTime());
Log.v("my", "current time is:"+Calendar.getInstance().getTimeInMillis()+";alarm time is:"+c.getTimeInMillis()
+";sub time is:"+(c.getTimeInMillis()-Calendar.getInstance().getTimeInMillis()));
Log.v("my", "start "+getPackageName()+".service");
return super.onStartCommand(intent, flags, startId);
}
private Calendar setCanlendarAlarmTime(){
Calendar calendar = Calendar.getInstance();
SharedPreferences preferences = getSharedPreferences("AtyEditNote", Activity.MODE_PRIVATE);
String time = preferences.getString("alarmTime","0");
Log.v("my", "on service time is:"+time);
int month = Integer.valueOf(time.substring(0,2));
int day = Integer.valueOf(time.substring(3,5));
int hour = Integer.valueOf(time.substring(6,8));
int minute= Integer.valueOf(time.substring(9));
calendar.set(Calendar.MONTH, month-1);
calendar.set(Calendar.DAY_OF_MONTH, day);
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 11);
calendar.set(Calendar.MILLISECOND, 11);
//put the get time
Log.v("my", "month:"+month+";day:"+day+";hour:"+hour+";minute:"+minute);
return calendar;
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
Log.v("my", "-------->onStart service");
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Log.v("my", "-------->onCreate Service");
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
//在service结束后关闭AlarmManager
AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent i = new Intent(this,MyReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
manager.cancel(pi);
Log.v("my", "end "+getPackageName()+".service");
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
3、广播接收,里面包含提醒事件(主要是notification)
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//设置通知内容并在onReceive这个函数是开启
NotificationManager notifiacationManager =
(NotificationManager)context.getSystemService(
context.NOTIFICATION_SERVICE);
//原本的代码,不过好像不能运行
/*Notification notification = new Notification(R.drawable.ic_dialog_alert,
"Hello",System.currentTimeMillis());
String title="有新提醒";
String content="详细内容是......";
notification.setLatestEventInfo(context, title, content, null);
notification.defaults = Notification.DEFAULT_ALL;
notifiacationManager.notify(1,notification);*/
//在次开启longRunnigServer这个服务
Bitmap btm = BitmapFactory.decodeResource(context.getResources(),R.drawable.ic_launcher);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("新提醒")
.setContentText("请查看记事本");
mBuilder.setTicker("message");
mBuilder.setNumber(1);
mBuilder.setLargeIcon(btm);
long [] pattern = {0,500,1000};
mBuilder.setVibrate(pattern);
Intent i = new Intent(context,LongRunningServer.class);
PendingIntent pi = PendingIntent.getActivity(
context,
0,
i,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pi);
notifiacationManager.notify(0,mBuilder.build());
// context.startService(i);//原本的代码是服务于广播相互通信,不断重复,这里我注释,只实现一次提醒
Log.v("my", "success running MyReceiver extends BroadcastReceiver");
}
}
总的过程是:先在一个activity里面启动服务,在服务中通过alarmManager控制时间启动广播,在广播中设置提醒
- android 设置提醒事件
- android提醒事件
- Android日历事件提醒
- Android AlarmManager设置定时事件提醒
- Android 弹出系统日历,添加提醒事件
- android自定义日历实现事件提醒
- iOS 日历提醒事件
- ios本地添加提醒事件
- ios 添加日历提醒事件
- Android广播事件机制,并实现简单的定时提醒功能
- Android广播事件机制及应用(实现简单的定时提醒功能)
- Android广播事件机制,并实现简单的定时提醒功能
- Android广播事件机制及应用(实现简单的定时提醒功能)
- android 读写系统日历、插入事件、添加多个提醒的问题
- Android 编译 提醒
- Android之提醒用户
- android Notification 状态栏提醒
- android-数字提醒
- MACHINE LEARNING IN ACTION KNN
- 软工:CMMI
- Java 并发之 CountDownLatch、CyclicBarrier 和 Semaphore
- android事件分发教程(一):View
- JSON
- android提醒事件
- 三角形内部的整点的个数
- [leetcode]53. Maximum Subarray 最大连续子串python实现【medium】
- C语言小项目--贪吃蛇游戏
- bootstrap的大型居中div
- spring笔记:第二章(1)
- akira之修改linux 3.6.8内核并添加系统调用
- LNK2005 int * c? already defined in *.obj
- 递归算法