前台服务,alarmManager和通知都有问题。
基本上我正在尝试做每天早上发出通知的服务。我正在尝试使用前台服务和cpuWakeLock执行此操作,但有些原因我仍然无法每天早上启动此通知。在模拟器中一切正常。
使用前台服务的主要原因是,如果没有它,我还没有工作,当应用程序关闭时警报会继续触发。
我有点绝望。
这是我的代码(已清理版)
public class MentorService_cleaned extends Service {
NotificationManager notificationManager;
private PowerManager.WakeLock cpuWakeLock = null;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onCreate();
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.pficon)
.setContentTitle("xxx")
.setContentText("xxx")
.setContentIntent(pendingIntent).build();
notification.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
startForeground(1337, notification);
Calendar alarmStartTime = Calendar.getInstance();
Calendar now = Calendar.getInstance();
int alarmHour = 7;
int alarmMinute = 0;
alarmStartTime = Calendar.getInstance();
alarmStartTime.set(Calendar.HOUR_OF_DAY, alarmHour);
alarmStartTime.set(Calendar.MINUTE, alarmMinute);
alarmStartTime.set(Calendar.SECOND, 0);
if (alarmStartTime.before(now)) { //if it's after 7:00 move to next day
alarmStartTime.add(Calendar.DATE, 1);
}
Intent _Intent = new Intent(getApplicationContext(), AlertReceiver.class);
final PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, _Intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
alarm.set(AlarmManager.RTC_WAKEUP, alarmStartTime.getTimeInMillis(), pIntent);
// test pushNotification
pushNotification();
return START_STICKY;
}
@Override
public void onCreate() {
try {
if ((cpuWakeLock == null) || cpuWakeLock.isHeld() == false) {
PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock cpuWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ShakeEventService onCreate Tag");
cpuWakeLock.acquire();
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onDestroy() {
super.onDestroy();
if (this.cpuWakeLock.isHeld())
this.cpuWakeLock.release();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
public void pushNotification(){
try {
NotificationCompat.Builder notificBuilder = new NotificationCompat.Builder(this);
notificBuilder.setContentTitle("xxx");
//notificBuilder.setContentText(nextCard.caption);
//notificBuilder.setTicker(nextCard.caption);
notificBuilder.setContentText("xxx");
notificBuilder.setTicker("xxx");
notificBuilder.setSmallIcon(R.drawable.book);
notificBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);
Intent intent = new Intent(this, MainActivity.class);
TaskStackBuilder tStackBuilder = TaskStackBuilder.create(this);
tStackBuilder.addParentStack(Card.class);
tStackBuilder.addNextIntent(intent);
PendingIntent pendingIntent = tStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
notificBuilder.setContentIntent(pendingIntent);
notificationManager = (NotificationManager) getSystemService((Context.NOTIFICATION_SERVICE));
if (PFContents.profile.Notification) {
notificationManager.notify(1, notificBuilder.build());
}
}catch(Exception e)
{
}
}}
AlertReceicer级
public class AlertReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context ctxt, Intent i) {
Intent in = new Intent(ctxt, MentorService.class);
in.putExtra("fromMain", "false");
ctxt.startService(in);
}
}
这是我的清单文件:
<uses-permission android:name="android.permission.WAKE_LOCK" />
<service
android:name=".MentorService"
android:enabled="true"
android:exported="true"
android:process=".myprocess"
android:stopWithTask="false" />
答案 0 :(得分:0)
除此之外您可以尝试GCMNetworkManager,它是Alarmmanager的扩展。
AlarManager代码:
private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
...
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
// Set the alarm to start at 8:30 a.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 30);
// setRepeating() lets you specify a precise custom interval--in this case,
// 20 minutes.
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
1000 * 60 * 20, alarmIntent);
此处 AlarmReceiver.class(即扩展BroadcastReciever)将触发您可以执行操作的位置: - 创建通知。 - 如果需要,修改下一个警报。
答案 1 :(得分:0)
重复闹钟 - 因为您每天早上都想要通知。
闹钟正常工作,确保您正确地传递Time in Milliseconds。它也适用于背景。