Android Background Service Manage UI Operation

I am new to Android app development and therefore am having serious problems when the app starts successfully. I think most of my problems can be explained by the lack of Android platform concepts and the use of Android PC programming concepts!;)

What I intend to do is develop a UI based Android app, but I have strong requirements that the app should behave like a daemon! When I say demon; when starting the application, the user interface should not be visible! Based on a specific wake-up event, the application demonstrates wake-up so that the user interface is visible to the user. Likewise, based on a specific sleep event, the app's UI should be hidden. Something similar to an alarm app on Android.

The UI part was very easy for me because I was using the Android port of Qt and the app works like a charm. But this works like any normal application and does not include my requirement above. I tried looking at the Qt documentation and there is no precondition for that and in some forum they recommended using Android SDK methods.

Since I am not a Java expert, I read a lot about Android development and was able to create an application that "somewhat" satisfies the above requirement, but I have few problems and I wanted to know if I went the right way in the right approach. So, to summarize, what i did below.

  • I subclassed android.app.Service and in my overriden onStartCommand, I create a worker thread that checks for wakeup and sleep event. For the sake of discussion, let's assume that the wake event looks like every 60 minutes and the sleep event looks like 5 minutes after waking up.
  • Inside the onStartCommand method, I broadcast the Intent, awaiting the actions that created my service to receive it (step 6 below explains why I am doing this)
  • A worker thread satisfying the wake condition starts my UI work like this:

    Intent activityIntent = new Intent(getBaseContext(), MyUIActivity.class); activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); getApplication().startActivity(activityIntent);

  • A workflow satisfying the wait condition requires my UI activity to end by broadcasting an intent waiting for the UI activity to receive it.

  • OnStartCommand finally returns START_NOT_STICKY.
  • I have a main activity that registers a BroadcastReceiver instance with an IntentFilter to receive an intent after the service has been started from step 2 above. On receiving the intent, I call finish () to close the UI for my service demo!

The above setting is working fine and I can see that the service is running in the background with proper Toast messages and the UI also appears when the wake condition is satisfied and the Qt widgets are displayed fine on the screen. However, I have little problem in making this setup work safely. Below are the problems (not sure if they are real problems or not):

Sometimes when I start the application, I don't see a toast message that the service is being started from the onStartCommand method, but the following message is displayed in the logcat:

W/ActivityManager( 628): Scheduling restart of crashed service org.example.myapp/.MyService in 1000ms I/ActivityManager( 628): START u0 {flg=0x10000000 cmp=org.example.myapp/.MyApplicationActivity (has extras)} from pid 21764 W/ActivityManager( 628): Permission Denial: starting Intent { flg=0x10000000 cmp=org.example.myapp/.MyApplicationActivity (has extras) } from null (pid=21764, uid=2000) not exported from uid 10129

When I try to start the application manually, it works great!

Whenever I try to hide the UI activity by calling finish (); I think this is when I see the following message:

I/WindowState( 628): WIN DEATH: Window{428ef138 u0 org.example.myapp/org.example.myapp.MyApplicationActivity} W/ActivityManager( 628): Scheduling restart of crashed service org.example.myapp/.MyService in 1000ms W/WindowManager( 628): Force-removing child win Window{41962f40 u0 SurfaceView} from container Window{428ef138 u0 org.example.myapp/org.example.myapp.MyApplicationActivity}

After that, the service stops being active!

Basically, my question is: Is this the right way to do it?

+3


source to share


1 answer


Please confirm when a sleep event occurs, the device is in sleep mode i.e. off or on?

If he is already asleep, your recipient will not receive an intent because he is already asleep. You have to set the nay flag in your activity so you can check when the activity is resumed and execute the activity accordingly and complete the activity instance.



You don't see your toasts because the service lifecycle is not related to the activity lifecycle. Services continue in the background until you explicitly stop them or start them.

0


source







All Articles