特定日期的Android通知 [英] Android notification at specific date

查看:222
本文介绍了特定日期的Android通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须做一个应用程序,我必须设置一个日期,在特定的日期在9点我必须做一个通知。什么是最简单的方法?(我想让应用程序工作,即使我重新启动我的手机,或拿出我的电池,是AlarmManager一个解决方案?)

解决方案

要安排一个Action,您可以使用AlarmManager



尝试此代码对我有用:



1 /声明BroadcastReceiver CLASS启动Action,此类可以在您的活动内部或其他Java文件中。

 public class Receiver extends BroadcastReceiver {

@Override
public void onReceive(Context context,Intent intent){
// TODO自动生成的方法stub
Toast.makeText(context,intent.getStringExtra(param),Toast.LENGTH_SHORT).show();
}

}

2 /在你的Oncreate方法放此代码

  AlarmManager alerts =(AlarmManager)this.getSystemService(Context.ALARM_SERVICE); 

接收器接收器=新的接收器();
IntentFilter filter = new IntentFilter(ALARM_ACTION);
registerReceiver(receiver,filter);

Intent intent = new Intent(ALARM_ACTION);
intent.putExtra(param,我的预定动作);
PendingIntent operation = PendingIntent.getBroadcast(this,0,intent,0);
//我选择3s后我的应用程序启动
alerts.set(AlarmManager.RTC_WAKEUP,System.currentTimeMillis()+ 3000,操作);

启动你的应用程序一个Toast将在3秒后出现,所以你可以更改System.currentTimeMillis )+3000你的醒来时间。


I have to do an app in wich i must set a date and at that specific date at 9 o'clock i must do a notification. What is the simplest method of doing that?(i want the app to work even if i reboot my phone, or take out my battery, is AlarmManager a solution?)

解决方案

To schedule an Action you can use the AlarmManager

Try this code it's work for me:

1 / Declare the BroadcastReceiver CLASS to launch the Action, this class can be inside your activity or outside in an other java file

public class Receiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Toast.makeText(context, intent.getStringExtra("param"),Toast.LENGTH_SHORT).show();
    }

}

2/ Inside your Oncreate Method put this code

AlarmManager alarms = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);

    Receiver receiver = new Receiver();
    IntentFilter filter = new IntentFilter("ALARM_ACTION");
    registerReceiver(receiver, filter);

    Intent intent = new Intent("ALARM_ACTION");
    intent.putExtra("param", "My scheduled action");
    PendingIntent operation = PendingIntent.getBroadcast(this, 0, intent, 0);
    // I choose 3s after the launch of my application
    alarms.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+3000, operation) ;   

Launch your App a Toast will be appeared after 3 seconds, So you can change "System.currentTimeMillis()+3000" with your wake up time.

这篇关于特定日期的Android通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆