我的一些用户抱怨我的警报管理器不起作用。为什么?

Some my users complain that my alarm manager does not work. Why?

最近,我发布了一款主要英雄离线一段时间(将近15分钟)的文字游戏。然而,100 名用户中有近 10 名用户抱怨主英雄永远不会上线。我有以下警报接收器 class:

public class AlarmReceiver extends BroadcastReceiver {
Context cont;
Notification noti;

@Override
public void onReceive(Context context, Intent intent) {
    WakeLocker.acquire(context);
    cont = context;
    context.sendBroadcast(new Intent("MESSAGE"));
    writeWaitTimeAlert("0");
    if(!isAppForground(context))
        notifyMe();
    WakeLocker.release();
}

public void writeWaitTimeAlert(String str) {

    try {
        FileOutputStream fileOutputStream = cont.openFileOutput("timeBoolean.txt", Context.MODE_PRIVATE);
        fileOutputStream.write(str.getBytes());
        OutputStreamWriter osw = new OutputStreamWriter(fileOutputStream);
        osw.flush();
        osw.close();
        fileOutputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public boolean isAppForground(Context mContext) {

    ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
    if (!tasks.isEmpty()) {
        ComponentName topActivity = tasks.get(0).topActivity;
        if (!topActivity.getPackageName().equals(mContext.getPackageName())) {
            return false;
        }
    }

    return true;
}
public void notifyMe() {

    Intent intent = new Intent(cont, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(cont, 0, intent, 0);
    noti = new Notification.Builder(cont)
            .setTicker("My app")
            .setContentTitle("Message")
            .setContentText("Hello!")
            .setSmallIcon(R.drawable.ic_stat_message)
            .setContentIntent(pendingIntent).getNotification();
    noti.flags = Notification.FLAG_AUTO_CANCEL;
    NotificationManager nm = (NotificationManager) cont.getSystemService(Context.NOTIFICATION_SERVICE);
    nm.notify(0, noti);


}

}

我在另一个名为 "Playground.class":

的 class 中设置了警报
public void setAlarm(long time) {


    Toast.makeText(context.getApplicationContext(), "Offline!", Toast.LENGTH_SHORT).show();

    Intent intent = new Intent(context, AlarmReceiver.class);
    intent.putExtra("alarmId", REQUEST_CODE);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, REQUEST_CODE, intent, 0);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
            SystemClock.elapsedRealtime() +
                    time * 1000, pendingIntent);

}

请帮助我或建议我其他解决方法。谢谢!

您的 setAlarm 方法应该作为 BroadcastReceiver class。让我们从您的 Service/Activity class.

发送一些广播
Intent broadcastIntent = new Intent(this, TimeBroadcast.class);
sendBroadcast(broadcastIntent);

并将此代码放入您的 BroadcastReceiver 中:

public class TimeBroadcast extends BroadcastReceiver {

public TimeBroadcast() {
}

public TimeBroadcast(Context context) {
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(System.currentTimeMillis());
    int repeatTime = 10 * 1000 * 60;
    Intent intent = new Intent(context, TimeBroadcast.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis() + repeatTime, pendingIntent);
}

@Override
public void onReceive(Context context, Intent intent) {

    Intent serviceIntent = new Intent(context, YOUR_CLASS);
    //pass values, actions, flags, whatever you want and send your Intent where you want
}

}

此代码将向您的 Service/Activity class 发送 Intent,延迟时间在 AlarmManager.set 方法中设置。当 Intent 到达您的 class 时,您可以处理一些操作,例如警告消息或断开连接 users/heroes.