android start activity from background thread

孤街醉人 提交于 2019-12-25 04:51:50

问题


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.

来源:https://stackoverflow.com/questions/34328894/android-start-activity-from-background-thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!