我的一些用户抱怨我的闹钟管理器不起作用。为什么呢?

时间:2017-07-20 19:52:47

标签: android alarmmanager

最近,我发布了一款基于文本的游戏,其中主要英雄离线一段时间(近15分钟)。然而,100个用户中有近10个抱怨主要英雄从未上网。我有以下Alarm Receiver类:

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


}

}

我在另一个名为&#34; Playground.class&#34;的类中设置了setAlarm:

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

}

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

1 个答案:

答案 0 :(得分:0)

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

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
}

}

此代码将在AlarmManager.set方法中按时间集延迟向您的Service / Activity类发送Intent。当Intent来到你的班级时,你可以处理一些动作,如警告信息或断开afk用户/英雄。

相关问题