Android 安卓-救援警报

Android 安卓-救援警报,android,android-intent,alarmmanager,intentservice,Android,Android Intent,Alarmmanager,Intentservice,我有一个Intent服务,每天与服务器连接一次,效果很好,就像android教程中的示例一样。 因为它每天只轮询服务器一次,所以我有一个疑问。 如果服务尝试连接服务器而用户未联机,会发生什么情况? 我错过了那一天,还是有办法在运行时重新安排闹钟,并在用户在线时尝试连接 这是我的密码: obs:我在启动程序活动中调用setAlarm(上下文) public class SampleBootReceiver extends BroadcastReceiver { SampleAlarm

我有一个Intent服务,每天与服务器连接一次,效果很好,就像android教程中的示例一样。 因为它每天只轮询服务器一次,所以我有一个疑问。 如果服务尝试连接服务器而用户未联机,会发生什么情况? 我错过了那一天,还是有办法在运行时重新安排闹钟,并在用户在线时尝试连接

这是我的密码: obs:我在启动程序活动中调用setAlarm(上下文)

  public class SampleBootReceiver extends BroadcastReceiver {

    SampleAlarmReceiver alarm = new SampleAlarmReceiver();

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
            alarm.setAlarm(context);
        }
    }
}



public class SampleAlarmReceiver extends WakefulBroadcastReceiver {
    private AlarmManager alarmMgr;
    private PendingIntent alarmIntent;

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent service = new Intent(context, SampleSchedulingService.class);
        startWakefulService(context, service);
    }
    public void setAlarm(Context context) {
        alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context, SampleAlarmReceiver.class);
        alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());


       alarmMgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME,
               SystemClock.elapsedRealtime()+60*1000, AlarmManager.INTERVAL_DAY , alarmIntent);

        ComponentName receiver = new ComponentName(context, SampleBootReceiver.class);
        PackageManager pm = context.getPackageManager();

        pm.setComponentEnabledSetting(receiver,
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP);

    }



public class SampleSchedulingService extends IntentService {
    public boolean serviceGetNews;

    public SampleSchedulingService() {
        super("SchedulingService");
    }


    @Override
    public void onCreate() {
        super.onCreate();

    }

    public static final int NOTIFICATION_ID = 1;
    public static NotificationManager mNotificationManager;

    @Override
    protected void onHandleIntent(Intent intent) {
        Utils.serviceGetNews(getBaseContext());
        SampleAlarmReceiver.completeWakefulIntent(intent);

    }
    public static void sendNotification(String msg, Context context) {
        mNotificationManager = (NotificationManager)
                context.getSystemService(Context.NOTIFICATION_SERVICE);

        PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
                new Intent(context, NewsActivity.class), 0);

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(context)
                        .setSmallIcon(R.drawable.ic_launcher)
                        .setContentTitle("title")
                        .setStyle(new NotificationCompat.BigTextStyle()
                                .bigText(msg))
                        .setContentText(msg)
                .setAutoCancel(true);

        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    }
}

您有两个问题,其中一个问题的答案取决于您的
服务
实施情况。第一个是关于
报警
。一旦您设置了闹钟和
挂起内容
,无论设备的睡眠状态或用户交互如何,它都将在正确的时间播放。您还将其设置为重复发送,因此它也将被重新安排在每天大约相同的时间再次发送。关于用户交互和联系服务器的服务的第二个问题完全取决于您的
服务
实现。如果您需要服务在下一个警报之前重新尝试,则需要构建该逻辑。

您需要更好地阐明您的使用情况。您是否已经在使用
AlarmManager
启动
BroadcastReceiver
,然后启动您的服务?您的
服务
是否设置为通过其他机制自动启动?我发布了我正在使用的代码。这是开发者教程中的一个副本,用于使用IntentService连接服务器。如果我创建两个警报,一个每天重复一次,另一个每小时重复一次,并选择使用哪个警报来完成任务?有一个很好的解决方案吗?