Posts

Showing posts from December, 2017

Long running background services in Android

We all know for service should be in running even when app is killed is through sticky service. Just override the onStartCommand() method and return START_STICKY. The code snippet is,

@Override public int onStartCommand(Intent intent, int flags, int startId) { super.onStartCommand(intent, flags, startId); return START_STICKY; }

Here one tricky situation could arise. If app is killed background service will restart, basically its onCreate() will get called and reinitialises every variables.  If your business logic doesn't want this situation, basically if the service is holding some state machine, then we may have to store the state details in a persistent way.

Another way we can ensure service doesn't restart even though app get killed is through notification. We should effectively call startForeground(id, Notification). We can do this in onCreate() of the service. The code snippet is,

@Override public void onCreate() { super.onCreate(); startForegro…

Messenger and Service in Android

Generally to get some result back from service or intent service we use Broadcast receiver. That is, service send a broadcast and in activity we register a receiver and and do some operations according to the result we got from service.

Here I am going to tell a alternate way to get the result back from service or intent service. It is a simple and effective way using Handler and Messenger.
Consider, we have to download a image from network

button.setOnclickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, DownloadService.class); intent.setData(Uri.parse("https://actiontreeservice.ca/files/trees.png")); intent.putExtra(DownloadService.EXTRA_MESSENGER, new Messenger(handler)); startService(intent); } });
and our handler looks like this,

private Handler handler=new Handler() { @Override public void handleMessage(Message msg) { Toast.makeText(Mai…

Location Awareness service in Android

Steps.

1. First we need to get the API key, go to terminal and type,


keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android
if JDK not installed, you will get a error like,
No Java runtime present, requesting install
A dialog appears click on more info and it will take directly to oracle JDK download site. Download JDK 9 and install and try again the above command. You will get the SAH1 and SAH256 fingerprints. 
2. Click on "Get key" button, then either select an existing project from google developer console or create a new project from this site

3. Now add the API_KEY to manifest file under <application> as <meta-data android:name="com.google.android.awareness.API_KEY" android:value="@string/google_awareness_api_key"> </meta-data>
4.  Now we are ready to use Google awareness API in our app. Same like how we use fused location provider, create a googleApiClient and add the callbacks. …