A blog or place for Mobile Application Developers to improve his skills and learn new .

Thursday, 19 December 2013

How to start and stop service in android.

Here we see how to start and stop a service dynamically.

What is Service ?

A service is a
component which runs in the background without direct interaction with the
user. As the service has no user interface, it is not bound to the lifecycle of
an activity.

As there are two methods
by which we start and stop service :

startService (Intent service)

stopService (Intent service)

In order to start a service
we need to call a method name onStartCommand(Intent
intent, int flags, int startId)and to stop a service call a method name
onDestroy() . All this work we do in
class which extends Service class.

Create a project with
the following details :

·ProjectName: StartStopServicee

·PackageName: sat.tuts4mobile.startstop

·ActivityName: MainActivity

In the MainActivity.java file, copy the following code :

package
sat.tuts4mobile.startstop;

import
android.os.Bundle;

import
android.app.Activity;

import
android.content.Intent;

import
android.view.Menu;

import
android.view.View;

import
android.widget.Button;

public class
MainActivity extends Activity {

@Override

protected void onCreate(Bundle
savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Button start = (Button)
findViewById(R.id.button1);

start.setOnClickListener(new
View.OnClickListener() {

@Override

public void
onClick(View v) {

//
TODO Auto-generated method stub

startService(new
Intent(MainActivity.this, MyService.class));

}

});

Button stop = (Button)
findViewById(R.id.button2);

stop.setOnClickListener(new
View.OnClickListener() {

@Override

public void
onClick(View v) {

//
TODO Auto-generated method stub

stopService(new
Intent(MainActivity.this, MyService.class));

}

});

}

@Override

public boolean
onCreateOptionsMenu(Menu menu) {

// Inflate the menu;
this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main,
menu);

return true;

}

}

Create a new class
name MyService.java file, copy the
following code :

Thursday, 12 December 2013

Here we learn how to get user location , how to get latitude and longitude and also the address.

As there are two ways by which we can get user location :

Using GPS

Using Network

Pros and Cons:

GPS is most accurate, it quickly consumes battery power, and doesn't return the location as quickly as users want.

Network Location Provider determines user location using cell tower and Wi-Fi signals, providing location information in a way that works indoors and outdoors, responds faster, and uses less battery power.

Here we get location using network provider. Later on we will discuss about how to get location using GPS.

In order to get location we use the "LocationManager" class by calling "requestLocationUpdates()".

We also need to implement the "LocationListener" interface. This provide us the latitude and longitude.

If you want to get the location by name , use Geocoder class . In this class we pass the latitude and longitude and the address of that position.

To get the location in application , we need to add following permissions to the AndroidMainfest.xml file.

ACCESS_COARSE_LOCATION:

Allows an app to access approximate location derived from network location sources such as cell towers and Wi-Fi.

ACCESS_FINE_LOCATION:

Allows an app to access precise location from location sources such as GPS, cell towers, and Wi-Fi.