@Override
 public void handleMessage(Message msg) {
   switch (msg.what) {
     case MSG_SPAM:
       {
         boolean fg = msg.arg1 != 0;
         Intent intent = new Intent(ActivityTestMain.this, SpamActivity.class);
         Bundle options = null;
         if (fg) {
           ActivityOptions opts = ActivityOptions.makeTaskLaunchBehind();
           options = opts.toBundle();
         }
         startActivity(intent, options);
         scheduleSpam(!fg);
       }
       break;
     case MSG_SPAM_ALARM:
       {
         long when = SystemClock.elapsedRealtime();
         Intent intent = new Intent(ActivityTestMain.this, AlarmSpamReceiver.class);
         intent.setAction("com.example.SPAM_ALARM=" + when);
         PendingIntent pi = PendingIntent.getBroadcast(ActivityTestMain.this, 0, intent, 0);
         mAlarm.setAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME, when + (30 * 1000), pi);
         scheduleSpamAlarm(30 * 1000);
       }
       break;
   }
   super.handleMessage(msg);
 }
Esempio n. 2
0
  static void scheduleExactAlarm(
      Context ctxt, AlarmManager alarms, long period, boolean isDownload) {
    Intent i =
        buildBaseIntent(ctxt)
            .putExtra(EXTRA_PERIOD, period)
            .putExtra(EXTRA_IS_DOWNLOAD, isDownload);
    PendingIntent pi = PendingIntent.getBroadcast(ctxt, 0, i, 0);

    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {
      Log.e("PollReceiver", "allow while idle");
      alarms.setAndAllowWhileIdle(
          AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + period, pi);
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
      alarms.setExact(
          AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + period, pi);
    } else {
      alarms.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + period, pi);
    }
  }
Esempio n. 3
0
        @Override
        public void onReceive(Context context, Intent intent) {
          Log.i(TAG, "Received " + intent);
          Util.logExtras(intent);

          SharedPreferences prefs =
              PreferenceManager.getDefaultSharedPreferences(SinkholeService.this);
          int delay = Integer.parseInt(prefs.getString("screen_delay", "0"));
          boolean interactive = Intent.ACTION_SCREEN_ON.equals(intent.getAction());

          AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
          PendingIntent pi =
              PendingIntent.getBroadcast(
                  context,
                  0,
                  new Intent(ACTION_SCREEN_OFF_DELAYED),
                  PendingIntent.FLAG_UPDATE_CURRENT);
          am.cancel(pi);

          if (interactive || delay == 0) {
            last_interactive = interactive;
            reload(null, "interactive state changed", SinkholeService.this);
          } else {
            if (ACTION_SCREEN_OFF_DELAYED.equals(intent.getAction())) {
              last_interactive = interactive;
              reload(null, "interactive state changed", SinkholeService.this);
            } else {
              if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
                am.set(AlarmManager.RTC_WAKEUP, new Date().getTime() + delay * 60 * 1000L, pi);
              else
                am.setAndAllowWhileIdle(
                    AlarmManager.RTC_WAKEUP, new Date().getTime() + delay * 60 * 1000L, pi);
            }
          }

          // Start/stop stats
          PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
          mServiceHandler.sendEmptyMessage(pm.isInteractive() ? MSG_STATS_START : MSG_STATS_STOP);
        }