我目前每天都有重复通知,每天下午6点重复一次。我想要做的不是下午6点,而是在活动即将开始时显示通知。我有一个事件arraylist,它包含日期和时间,这是可能的。以下是我目前正在显示通知的方式。
这是我的主要活动
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, EVentsPerform.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
context).setSmallIcon(R.drawable.applogo)
.setContentTitle("Alarm Fired")
.setContentText("Events To be PErformed").setSound(alarmSound)
.setAutoCancel(true).setWhen(when)
.setContentIntent(pendingIntent)
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
notificationManager.notify(MID, mNotifyBuilder.build());
MID++;
}
}
这是我的broadcast_reciever类
公共类AlarmReceiver扩展了BroadcastReceiver {
% creat some 3D plot:
x = 1:0.1:10;
y = x;
[X,Y] = meshgrid(x,y);
f = @(x,y) sin(x).*cos(y);
Z = f(X,Y);
surf(X,Y,Z)
xlabel('Xsomething')
ylabel('Ysomething')
zlabel('Zsomething')
%%% here it starts: %%%
ax = gca;
post = findall(ax,'Type','Text'); % get all text handles
p = zeros(numel(post),2);
% collect all the position vectors:
for k = 1:numel(post)
post(k).Units = 'normalized'; % set units to the normelized figure units
% the 'position' of the text is relative to the axes, so we convert it
% to the figure units:
p(k,:) = post(k).Position(1:2)+ax.Position(1:2);
end
% find the most left and bottom items,
% and move them to the borders of the figure:
ax.Position(1:2) = min(p);
% fill the rest of the figure with th axes:
ax.Position(3:4) = 1-ax.Position(1:2);
我不知道如何设置时间,以便在有事件时重复。任何指针和帮助与赞赏。感谢
答案 0 :(得分:0)
您可以为每个事件设置闹钟。从数据库中读取所有事件时间,然后为这些事件设置单独的警报。而不是setRepeating使用这个。设置的警报变体适用于不同的Android版本,可将手机从睡眠中唤醒。
private void setAlarm(Long dateInMillis) {
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent alarmIntent = new Intent(this, AlarmReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
if (Build.VERSION.SDK_INT >= 23) {
am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, dateInMillis, pi);
} else if (Build.VERSION.SDK_INT >= 19) {
am.setExact(AlarmManager.RTC_WAKEUP, dateInMillis, pi);
} else {
am.set(AlarmManager.RTC_WAKEUP, dateInMillis, pi);
}
}
dateInMillis是您要为其设置闹钟的Calender.getInstance().getTimeInMillis()
。所以我希望你在数据库中存储了很长时间,这样才能让你的生活变得更轻松
public ArrayList<String> getAlarms() {
SQLiteDatabase db;
ArrayList<String> result = new ArrayList<String>();
String myPath = DATABASE_PATH + DATABASE_NAME;
db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
selectQuery = "SELECT * FROM TABLE";
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
result.add(cursor.getString(cursor.getColumnIndex("WhateverYourColumnIs")));
while (cursor.moveToNext()) {
result.add(cursor.getString(cursor.getColumnIndex("WhateverYourColumnIs")));
}
}
cursor.close();
db.close();
return result;
}
您可以将字符串转换为Long
DatabaseHelper db = new DatabaseHelper();
ArrayList<String> alarms = db.getAlarms()
for(String alarm : alarms){
try{
setAlarm(Long.parseLong(alarm));
}catch(NumberFormatException nfe){
//Not a number
}
}
类似的东西
private Long calendarMillis(String date, String time){
Long result;
//Do A bunch of stuff to get the information to fill in below from what you bring in.
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, year);
//...Calendar.HOUR, Calendar.MONTH, Calendar.DAY
c.set(Calendar.MINUTE, minute);
result = c.getTimeInMillis();
return result;
}
public class NotificationInformation{
Long alarmTime;
String name;
public void NotificationInformation(){
}
public void NotificationInformation(NotificationInformation ni){
this.name = ni.getName();
this.alarmTime = ni.getAlarmTime();
}
//getters and setters
public void setAlarmTime(Long alarmTime){
this.alarmTime = alarmTime;
}
public void setName(String name){
this.nime = nime;
}
public Long getAlarmTime(){
return alarmTime;
}
public String getName(){
return name;
}
}
答案 1 :(得分:0)
public class DatabaseHandler extends SQLiteOpenHelper {
private static final String DATABASE_PATH = "/data/data/YOUR_PACKAGE_HERE/databases/";
private static final String DATABASE_NAME = "YOUR_DATABASE.db";
private static final int DATABASE_VERSION = 1;
private SQLiteDatabase myDataBase = null;
private final Context myContext;
Context context = null;
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.myContext = context;
// TODO Auto-generated constructor stub
myDataBase = this.getWritableDatabase();
myDataBase.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE IF NOT EXISTS INFO (" +
"id INTEGER PRIMARY KEY AUTOINCREMENT," +
"alarmTime INTEGER," +
"name TEXT)");
}
public void addInfo(long time, String name) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("alarmTime", time);
values.put("name", name);
db.insert("INFO", null, values);
db.close();
}
public ArrayList<NotificationInfo> getInfoAll() {
String selectQuery;
SQLiteDatabase db;
Cursor cursor;
NotificationInfo ni = new NotificationInfo();
ArrayList<NotificationInfo> result = new ArrayList<NotificationInfo>();
String myPath = DATABASE_PATH + DATABASE_NAME;
db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
selectQuery = "SELECT * FROM INFO";
cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
ni.setAlarmTime(cursor.getLong(cursor.getColumnIndex("alarmTime"));
ni.setName(cursor.getLong(cursor.getColumnIndex("name"));
result.add(new NotificationInfo(ni));
while(cursor.moveToNext()){
ni.setAlarmTime(cursor.getLong(cursor.getColumnIndex("alarmTime"));
ni.setName(cursor.getLong(cursor.getColumnIndex("name"));
result.add(new NotificationInfo(ni));
}
}
cursor.close();
db.close();
return result;
}
}
public class AlarmActivity extends Activity{
//standard code for activity goes here
private void buttonClicked(){
Calendar c = Calendar.getInstance();
DatabaseHandler db = new Databasehandler(AlarmActivity.this);
ArrayList<NotificationInfo> alarms = db.getInfoAll();
db.close();
for(NotificationInfo ni : alarms){
c.setTimeInMillis(ni.getAlarmTime());
c.add(Calendar.MINUTE, -30);
setAlarm(c);
}
}
private void setAlarm(Long dateInMillis) {
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent alarmIntent = new Intent(this, AlarmReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
if (Build.VERSION.SDK_INT >= 23) {
am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, dateInMillis, pi);
} else if (Build.VERSION.SDK_INT >= 19) {
am.setExact(AlarmManager.RTC_WAKEUP, dateInMillis, pi);
} else {
am.set(AlarmManager.RTC_WAKEUP, dateInMillis, pi);
}
}
}
public class NotificationInformation{
Long alarmTime;
String name;
public void NotificationInformation(){
}
public void NotificationInformation(NotificationInformation ni){
this.name = ni.getName();
this.alarmTime = ni.getAlarmTime();
}
//getters and setters
public void setAlarmTime(Long alarmTime){
this.alarmTime = alarmTime;
}
public void setName(String name){
this.nime = nime;
}
public Long getAlarmTime(){
return alarmTime;
}
public String getName(){
return name;
}
}