Android-重新选择闹钟

时间:2014-03-04 20:55:33

标签: android android-intent alarmmanager intentservice

我有一个Intent服务,每天一次与服务器连接,它工作正常,就像在android教程中的例子。 由于它只是每天轮询服务器一次,我有一个疑问。 如果服务尝试连接服务器而用户不在线,会发生什么? 我想念那一天,或者有一种方法可以在运行时重新安排闹钟,并在用户上线时尝试连接?

这是我的代码: obs:我在我的启动器活动中调用setAlarm(context)

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

1 个答案:

答案 0 :(得分:0)

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