gpt4 book ai didi

android - 带有警报管理器的android中的每日通知

转载 作者:行者123 更新时间:2023-11-30 01:40:35 25 4
gpt4 key购买 nike

我想为我的 android 应用程序创建一个提醒系统,我想每天向用户的手机发送一次通知。我一直在寻找使用 AlarmManager 来实现它的任何解决方案,我有一段这样的代码:

public class TaskActivity extends ActionBarActivity ... {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_task);
periodicNotifier();
}

public void periodicNotifier(){
Intent myIntent = new Intent(this, NotifyService.class);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);

Calendar cal = Calendar.getInstance();
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.add(Calendar.DAY_OF_MONTH, 1);

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 24*60*60*1000, pendingIntent);
}

}

这是我的 NotifyService 类

public class NotifyService extends Service {

@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onCreate() {
NotificationManager notifManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Intent dest = new Intent(this.getApplicationContext(), TaskActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, dest, 0);

Notification notification = new Notification.Builder(this)
.setContentTitle("My Application")
.setContentText("Go to application now!")
.setSmallIcon(R.drawable.ic_notif)
.setContentIntent(pendingIntent)
.build();

notifManager.notify(1, notification);
}
}

我的问题是:

我想每天早上 00:00 发出通知,我试过这个代码然后将我的手机时间更改为 23:59 然后等到 00:00 但没有通知。我的代码有问题吗?或者有什么方法可以做到这一点?

任何答案将不胜感激,谢谢!

最佳答案

你必须试试这段代码你必须删除你的 AlarmManager , PendingIntent

在你的 Activity 集 BroadcastReceiver 上

private BroadcastReceiver mMessageReceiver;

和你的 onCreate() 方法里面注册你的 mMessageReceiver 并启动你的服务

registerReceiver(mMessageReceiver, new IntentFilter("time")); 
startService(new Intent(getApplicationContext(), ServiceClockInOut.class));

在你的//接收“我的事件”事件的 Intent 的处理程序中

private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String timer = intent.getStringExtra("timer");
Utils.showNotification(mContext, 1);
}
};

为您服务

public class ServiceClockInOut extends Service {
// constant
public static final long NOTIFY_INTERVAL = 5 * 1000; // 10 seconds

// run on another Thread to avoid crash
private Handler mHandler = new Handler();
// timer handling
private Timer mTimer = null;

@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onCreate() {
// cancel if already existed
if (mTimer != null) {
mTimer.cancel();
} else {
// recreate new
mTimer = new Timer();
}
registerReceiver(mMessageReceiver, new IntentFilter("end_time"));
// schedule task
mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(), 0, NOTIFY_INTERVAL);
}

class TimeDisplayTimerTask extends TimerTask {

@Override
public void run() {
// run on another thread
mHandler.post(new Runnable() {

@Override
public void run() {
// display toast

SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm aa");
String currentDateTime = dateTimeFormat.format(new Date()).toString();
String[] startSplit = currentDateTime.split(" ");
String CurrentTime = startSplit[1] + " " + startSplit[2].toUpperCase();


System.out.println("service start-====== " + CurrentTime + " == " + Constant.mStringStartTime
+ " == " + Constant.mStringBreakTime);

Intent intent = new Intent();
intent.putExtra("timer", "1");
sendBroadcast(intent);



}

});
}

}
// handler for received Intents for the "my-event" event
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Extract data included in the Intent
if (mTimer != null)
mTimer.cancel();
unregisterReceiver(mMessageReceiver);
}
};
}

关于android - 带有警报管理器的android中的每日通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34565799/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com