How to give UI to foreground service in Android watch? - android

I have developed an android watch app. It has a play button which when you click starts the sensor manager to analyse the sensor data. Using the data, I recognize the gestures performed by the user. So when I press the play button, sensor data analysis starts and the user can see a stop button to stop the sensor whenever he wants. So once the sensor is started, if I swipe and go back to the home screen and the again open the app, I can see the play button instead of stop button. I want to see the stop button when the user returns. Ideally I want to run a foreground service to detect the sensor readings.
So I want to start a foreground service when the user hits the play button and when the user open the notification, he must see the stop button to stop the sensor data as well as the service. How can I do this? Please advise.

UI:
Save your required UI data to the Bundle of the current states (stop button appearing) using onSaveInstanceState for that instance to your onPause() so that it can be retrieved by the onResume() using onRestoreInstanceState and viewed.
Service:
The Service should be bound to the activity so the two communicate directly. The service can be stopped from the activity using
stopService(new Intent(this, MyService.class));
You can also call stopSelf() from within the Service.

Related

Android application onExit event

I'm newbee on android, and already read Android app start and end event and Android onClose event but haven't found the answer.
I'm developing a simple application - flashlight.
And I want to release camera LED when application (not activity) is ended. Android has Application onCreate event, but doesn't have an appropriate onEnd/onExit event.
Activity OnDestroy event is not the case, because it is raised each time the device changes its orientation.
Does Application have onEnd event?
Does Application have onEnd event?
No. Mostly, that is because applications do not "end". They are either in the foreground, in the background, or their processes are terminated.
I found a way to do it:
You have a listener onPause() that is triggered when your app goes in background.
And usually when you kill your app in the task manager...your app goes in background for a few sec.
And that is the only way I found to do this.
For my example I have an app that plays a music automatically at launch. (Even if mobile is in silent mode).
I use onPause() to stop music and set original volume back to silent.
protected void onPause(){
super.onPause();
mp.pause(); //that is mediaplayer pause
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,originalVol, 0);
} //set volume to its original state

Android Location Service doesn't stop on home button

I have a service that runs to get user current location through LocationListener. I stop the service on the activity's onDestroy() but this method is only called when the user hits the back button to exit the app.
I want the service to stop both when the user hits the back button to exit and when they hit the home button. However, as far as i know, there's no way to intercept the home button. How do people deal with this issue?
Is it normal to leave the location listener running after the user hits the home button?
Use onPause() instead on onDestroy()

How to stop/pause an activity in watch from mobile?

I am currently starting an activity in watch from mobile by clicking a button. I am using wearable listener service to do it. Once the activity is started, the user performs gestures which are recognized and sent to mobile. So at some point in time, I want the user to click a button in mobile to close/pause this activity in watch. How can I do it? Should I destroy it or pause it? What should be my approach? Please advise
When you receive the message telling to close app in watch from phone,you can cancel all running work and close all activities in watch app or call System.exit(0).

android: put call phone in background when is answered

I'm developing an Android application that have a button to init a call phone when is clicked.
I want to detect when the call phone is answered and automatically put the native phone application in background and show my application again. Meanwhile, the call phone runs in background.
The result has to look like when you click the back button of the smartphone or if you open manually my application while the call phone is in progress. But this should be done automatically without pressing any buttons.
I know that I have call listeners to do something when the state of call is ringing, offhook or idle, but the problem is putting the call application in background from my application.
Sequence:
user clicks button in my app to launch native phone application (Android).
the phone call starts (call state: ringing).
the phone call is off hook (call state: Off-hook).
automatically, my application detects it and puts the native phone app in background and puts my application in foreground.
Is it possible and how I can do it?
Create a background service and implement BroadcastReceivers to listen the call state triggers.
you can put your app in the foreground after the call was answered using Activity start.
Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage(
getBaseContext().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
now the problem is that the started activity will be a NEW activity (It will clear all previous activites - without these flags it won't return back from the call screen).
i don't know how can I actually return to already working activity in the background.

Android: Confused about application states

I have seen this post here (http://stackoverflow.com/questions/151777/how-do-i-save-an-android-applications-state). I am having a similar problem I believe.
I have an app which listens to an audio stream (using the mediaPlayer object). If I press the Home button it will continue streaming and hide my app. Then, at a later point I can go back to my app and press stop when I'm done. This is what I want. If however I press the Back button, when I later open my app again the app has been redrawn from fresh. Text boxes, buttons, everything has reset like I've just opened the app for the first time so I can't stop my audio stream. Clicking stop does nothing because the app has 'forgotten' it is streaming (the stream runs under a separate handler from the main UI thread, so I'm guessing since its been 'reset' it has lost track of its handlers?).
Why does this happen with the Back button, and how can I stop it?
Move the streaming functionality into an Android Service. Use an Activity to bind to the Service and to interact with it.

Categories

Resources