1

I have this MainService, which, once started, turns the screen off and opens it up only after a timer finishes (additionally, said service also stops, after the timer ends).

My question is: is there a way for the application to also go into the background, just before the screen turns off? And by going into the background I mean for the Home Screen to appear.

MainActivity:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final Button startApp = (Button) findViewById(R.id.startApp);
    final EditText timer = (EditText) findViewById(R.id.insertTimer);

    assert startApp != null;
    startApp.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, "Countdown, Started", Toast.LENGTH_SHORT).show();

            Intent intent = new Intent(MainActivity.this, MainService.class);

            assert timer != null;
            intent.putExtra("timer", timer.getText().toString());

            Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 1000);

            registerReceiver(broadcastReceiver, new IntentFilter(MainService.BROADCAST_ACTION));

            startService(intent);
        }
    });
}

private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        updateUI(intent);
    }
};

private void updateUI(Intent intent) {
    String counter = intent.getStringExtra("counter");
    TextView txtCounter = (TextView) findViewById(R.id.txtCounter);
    assert txtCounter != null;
    txtCounter.setText(counter);
}

MainService:

public class MainService extends Service {

public static final String BROADCAST_ACTION = "com.example.vladpintea.friendsbeforecents.displayevent";
private final Handler handler = new Handler();
int counterr = 0;
Intent intentt;

String usedTimer;
long interval;

//TimerTask that will cause the run() runnable to happen.
TimerTask myTask = new TimerTask() {
    public void run() {
        stopSelf();
    }
};
//Timer that will make the runnable run.
Timer myTimer = new Timer();

@Override
public void onCreate() {
    intentt = new Intent(BROADCAST_ACTION);

    registerReceiver(counter, new IntentFilter(Intent.ACTION_SCREEN_ON));

    Toast.makeText(MainService.this, "Service, Created", Toast.LENGTH_SHORT).show();
}

private BroadcastReceiver counter = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(MainService.this, "Whoops! You've Lost.", Toast.LENGTH_SHORT).show();
        Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 30000);
    }
};

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Toast.makeText(MainService.this, "Service, Started", Toast.LENGTH_SHORT).show();

    usedTimer = intent.getStringExtra("timer");
    try {
        interval = Long.parseLong(usedTimer);
    } catch (NumberFormatException ignored) {}

    myTimer.schedule(myTask, interval);

    handler.removeCallbacks(sendUpdatesToUI);
    handler.postDelayed(sendUpdatesToUI, 1000);

    return super.onStartCommand(intent, flags, startId);
}

private Runnable sendUpdatesToUI = new Runnable() {
    public void run() {
        handler.postDelayed(this, 1000);
    }
};

public void DisplayLoggingInfoPlus() {
    intentt.putExtra("counter", String.valueOf(++counterr));
    sendBroadcast(intentt);
}

@Override
public void onDestroy() {
    DisplayLoggingInfoPlus();

    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
            | PowerManager.ACQUIRE_CAUSES_WAKEUP
            | PowerManager.ON_AFTER_RELEASE, "MyWakeLock");
    wakeLock.acquire();

    Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 30000);
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}
  • Does [this](http://stackoverflow.com/questions/3724509/going-to-home-screen-programmatically) cover it? – Vucko Aug 15 '16 at 18:41
  • Unfortunately, no, because I'm trying to go to the Home Screen from a service, not through a button from an activity. – Vlad Pintea Aug 15 '16 at 18:43
  • That does not matter. Why can't you just trigger this with a timer or whatever triggers the screen to lock? – Vucko Aug 15 '16 at 18:44
  • It worked. Thank you. I'll answer it myself to close the topic. – Vlad Pintea Aug 15 '16 at 18:49
  • I'd prefer that I answer it and you accept it :D – Vucko Aug 15 '16 at 18:52
  • 1
    Oh, ok :) Go ahead. – Vlad Pintea Aug 15 '16 at 18:52
  • As a reference, all I did was copy/paste this --- Intent intent = new Intent(MainActivity.this, MainService.class); Intent startMain = new Intent(Intent.ACTION_MAIN); startMain.addCategory(Intent.CATEGORY_HOME); startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(startMain); --- in the "onClick" function from MainActivity. – Vlad Pintea Aug 15 '16 at 18:53

2 Answers2

1

Use this post to see how to trigger the home pressed manually. Now simply trigger this code wherever you wanted this behavior to happen, e.g. in the timer that locks the screen.

Community
  • 1
  • 1
Vucko
  • 7,371
  • 2
  • 27
  • 45
0

Create public static void gotohome() { moveTaskToBack(true); } In your Activity class and then call it from service.

user6657161
  • 361
  • 1
  • 8