android start activity from background thread

2019-09-19 11:08发布

问题:

My app creates a timer task (background thread) and periodically need to display something. Main screen maybe hidden later by other apps. I found out (AFAIRemember on stackoverflow) that for making toast notification I need to use runOnUiThread runnable(just Toast.makeText crashed the app).

Now I want my background thread to be able to collect user input. I created activity class with layout and call it via intent

Intent intent = new Intent(this, screen2.class);
startActivityForResult(intent, 1);

When main screen is active, my new screen shows fine. But when main screen hidden, my other screen is hidden and I see it only when I make my app active again via Recents button.

I tried to run intent for second screen via runOnUiThread runnable - same result - new screen hidden until my app is active.

How can background thread display some activity on screen (UI thread)?

P.S.

Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivity(takePictureIntent); - displayes camera screen hidden, whereas action call below bring call screen on top of other app currently on screen

callIntent.setData(Uri.parse("tel:" + Uri.encode("+79851559700")));

        callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        callIntent.setPackage("com.android.phone");
        try {
            startActivity(callIntent);

Adding Intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); to camera intent made camera screen display from background timer tast, however it did not made my activity to do it.

Changing to intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK); made second screen appear on foreground however somehow it replaced main screen.