Android Question StartActivity from Service?

W. Graf

Member
Licensed User
Longtime User
Hi!

I'm having a problem/question:
I want to start an activity from a service-module. The Activity only contains a Label and a button. The user read the text in the Label and confirms it by pressing the button. It works only for 3-4 times. At the 5th time, the activity tries to appear, but the screen appears "empty" (black screen) and the app seems to hang. I have to use the task manager to remove the black screen.
Then I saw in B4A the last sentence from the description to StartActivity: "Note that you should usually not call StartActivity from a Service". Maybe this is the reason... So what is the correct/recommend way to display an activity from a service?

In the thread https://www.b4x.com/android/forum/threads/startactivity-from-service-and-lock-screen.42193/ I found this:

B4X:
Dim In As Intent
In.Initialize(In.ACTION_MAIN,"")
In.SetComponent("my.package.name/.main")
In.flags = 6815872 'FLAG_TURN_SCREEN_ON, FLAG_DISMISS_KEYGUARD, FLAG_SHOW_WHEN_LOCKED and FLAG_KEEP_SCREEN_ON
'In.Flags = 33554432 'FLAG_ACTIVITY_FORWARD_RESULT
StartActivity(In)

But it didn't help - the same problem.

Details about my project:
I wrote a service module. In Service_Start(), I'm calling a sub, which establishs a TCP-connection (Socket) to my server and this server sends a byte-sequence to my app. I'm using ASyncstreams to read the bytes. In the service-module I have placed the NewData-event (from ASyncStreams). The NewData-event is calling StartActivity(Main). In the Main-Activity, there is a button. If I click on the button, then the following things were done:

B4X:
StartServiceAt("Starter", DateTime.Now + Global.Service_Intervall*1000, False)
Activity.RemoveAllViews
Activity.Finish

After that, there were no instructions to execute, until the Service is called again (after xxx seconds).

I don't understand, why it works for 3-4 times. Maybe a internal memory problem?
During my tests: the phone is not sleeping, the screen is still on and unlocked.

Thank you for any advice.

Nice greetings,
Wolfgang
 

lemonisdead

Well-Known Member
Licensed User
Longtime User
Hello Wolfgang,
The first thing is to remember that you don't have to start the Starter service by yourself. It is automatically started when not already, while a module of your app is started.

This code is wrong for example
B4X:
StartServiceAt("Starter", DateTime.Now + Global.Service_Intervall*1000, False)

So, try starting another Service of your app to make the Starter service starting
 
Upvote 0

W. Graf

Member
Licensed User
Longtime User
Hi lemonisdead,

thank you for your quick Reply.

I'm sorry, but I don't understand: my app only consists of the starter Service and the Main activity (the activity, which I want to Show the user). What can I schedule instead of Starter?
 
Upvote 0

lemonisdead

Well-Known Member
Licensed User
Longtime User
What can I schedule instead of Starter?
You could add another service and schedule that one from the button. When that service will be started, it will start the Starter service if the Starter service is not already started.
 
Upvote 0

W. Graf

Member
Licensed User
Longtime User
I moved the content from the Starter Service to a second service.
It's the same phenomen. It works for 3-4 times and then the app hang.

But I don't think, that scheduling the Starter Service is the problem. In the past, I wrote an app, which scheduled the starter service too. There was never a problem. The Starter Service is also started, when the app hangs (I write a log-file, where I can see it). The log-file tells me, that the StartActivity()-instruction was executed. After that, the sub (which includes the StartActivity-instruction) is ending (no further instructions). So, this is the momentan, the Activity should be shown. But the events "Activity_Create" and the "Activity_Resume" are not fired any more.

I think, I'm doing something wrong with StartActivity. And like the description of StartActivity, I should not call it from service. But how is the correct way?? It's not easy....
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Using StartServiceAt with the starter service is indeed wrong as the starter service should never be started explicitly.

However this is probably not related to the error you describe.

Don't do anything special except of calling StartActivity(Main).

You don't need to remove all views. The activity will be destroyed anyway.

Do you eventually see the "application not responding" dialog?
 
Upvote 0

W. Graf

Member
Licensed User
Longtime User
At first, I didn't call RemoveAllViews(). It was just a try, after I discovered the Problem (I found it in another thread in this Forum ... I hoped, to remove the app from Memory quicker...). I removed it, but it still has no effect:

B4X:
StartServiceAt("Starter", DateTime.Now + Global.Service_Intervall*1000, False)
Activity.Finish

Up to now, couldn't see the "application not responding" Dialog. It's only a black Screen.

But today in the morning, I just see a new Detail:
Yesterday in the evening, I didn't Close my app on the mobile Phone - I left it with the black Screen. Today in the morning, after unlocking the mobile phone, the black Screen was replaced by the correct Screen. The timestamp (I print the current time in a Label on the Activity) was just from now. So it seems, that Android killed the process over the night and today morning, it started the app from itself?!

So I clicked the button and the Screen disappears (as planned). 10 seconds later, the app was recalled (because of StartServiceAt()) but black again. 5 minutes later, the black Screen was replace by the correct Screen, but I couldn't press the button - the app was freezed. A few seconds later, the "Application not responding" Dialog appears and my app was black again.

Very strange! Maybe a bug in Android (I'm using a Samsung Mini S4, Android 4.4.2)?

@Erel: "you" wrote in B4A for the StartActivity-Instruction(), that we shouldn't call it from a Service. What do you recommend? What's the correct way? Maybe this is the Problem? Or: why shouldn't I call StartActivity from Service?
 
Upvote 0

W. Graf

Member
Licensed User
Longtime User
Ok, I can repeat this Situation:

After starting my app, the Activity is shown correctly for 3-4 times.
The next time, the StartActivity()-instruction is executed, but the Activity_Resume-event is called 3-5 minutes later. There are no instructions between StartActivity() and Activity_Resume. Activity_Create has no instructions (empty). I'm getting no ANR-Dialog.

If I Close my app (Settings - Applications - My App - Stopp), and restart it, then there is no Change ... just having the black Screen. But I'm getting the ANR-Dialog after every few seconds . I'm always clicking on "Wait". A few minutes later, the correct Screen appears and I can click on my button to Close it (-> my app doesn't hang).
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
"you" wrote in B4A for the StartActivity-Instruction(), that we shouldn't call it from a Service. What do you recommend?
Actually I was quoting Google recommendation not to start an activity from a service as it might surprise the user. It is not related to this issue.

You need to find out why your app hangs. It might be related to DoEvents calls.
 
Upvote 0
Top