@Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    Toast.makeText(this, "Texting service started", Toast.LENGTH_SHORT).show();

    mServiceHandler.sendEmptyMessage(1);
    return START_STICKY;
  }
Esempio n. 2
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);
        }
 @Override
 public void onDestroy() {
   mServiceHandler.sendEmptyMessage(0);
   Toast.makeText(this, "Texting service stopped", Toast.LENGTH_SHORT).show();
 }