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

Android 我的一些用户抱怨我的报警管理器不工作。为什么?,android,alarmmanager,Android,Alarmmanager,最近,我发布了一个基于文本的游戏,其中主要英雄离线了一些时间(近15分钟)。然而,100位用户中有近10位抱怨说,主角从未上网。我有以下报警接收器类别: public class AlarmReceiver extends BroadcastReceiver { Context cont; Notification noti; @Override public void onReceive(Context context, Intent intent) { WakeLocker.acq

最近,我发布了一个基于文本的游戏,其中主要英雄离线了一些时间(近15分钟)。然而,100位用户中有近10位抱怨说,主角从未上网。我有以下报警接收器类别:

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);


}

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

您的setAlarm方法应作为BroadcastReceiver类工作。让我们从您的服务/活动课发送一些广播

Intent broadcastIntent = new Intent(this, TimeBroadcast.class);
sendBroadcast(broadcastIntent);
并将此代码放入您的广播接收器:

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
}
}


此代码将通过AlarmManager.set方法中的时间集延迟向服务/活动类发送意图。当您的类将达到预期目的时,您可以处理一些操作,如警告消息或断开afk用户/英雄的连接。

我是否需要在BroadReceiver类中编写setAlarm方法?为什么不?我认为,在您的情况下,最好的方法是从setAlarm方法中删除实际代码,并将其用于向BroadcastReceiver发送具有额外值的广播,如我上面的代码。接下来,从intent获得它,并像dynamicparam一样使用它。最后,将类中的广播(如TimeBroadcast)发送到AlarmReceiver类。这将在AlarmReceiver内执行您的代码,延迟时间间隔在sendBroadcast Intent中传递。我想了解为什么我的onReceive方法不触发?一些安卓手机会杀死我的闹钟管理器吗?怎么了?如果我像你说的那样实现,它能保证运行代码吗?我调试了你的setAlarm方法。SystemClock.elapsedRealtime()返回的日期是1970.01.03 04:12。尝试使用System.getCurrentTimeInMillis()。第二件事,你在哪里调用你的setAlarm方法?您有PendingEvent.getBroadcast(),我不确定,但它可能会等待广播消息,还有其他可用的方法,如PendingEvent.getActivity()、PendingEvent.getService()。当我更改这两件事时,您的代码对我有效。因此,您希望我将set()更改为alarmManager.set(alarmManager.RTC_WAKEUP,cal.getTimeInMillis()+time*1000,PendingContent);
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
}