我在前台服务、alarmManager 和通知方面遇到了一些问题。
基本上我正在尝试提供每天早上发出通知的服务。我正在尝试使用 foregroundService 和 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)
{
}
}}
警报接收器级
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" />