特定日期的Android通知 [英] Android notification at specific date
问题描述
要安排一个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屋!