Here I am trying to get location details. For that i used both GPS_PROVIDER
and NETWORK_PROVIDER
and both are in enable mode only. My problem is, I do not get any response in LocationListener
.
public class MainActivity extends Activity{
private TextView mTxtTodayDate;
private Button mBtnShowTodayDate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTxtTodayDate = (TextView) findViewById(R.id.txtTodayDate);
mBtnShowTodayDate = (Button) findViewById(R.id.btnShowTodayDate);
mBtnShowTodayDate.setOnClickListener(mShowDateListener);
}
private LocationListener gpsProvider= new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
switch (status) {
case LocationProvider.AVAILABLE:
mTxtTodayDate.setText( "GPS available again\n");
break;
case LocationProvider.OUT_OF_SERVICE:
mTxtTodayDate.setText( "GPS out of service\n");
break;
case LocationProvider.TEMPORARILY_UNAVAILABLE:
mTxtTodayDate.setText("GPS temporarily unavailable\n");
break;
}
}
@Override
public void onProviderEnabled(String provider) {
mTxtTodayDate.setText("GPS Provider Enabled\n");
}
@Override
public void onProviderDisabled(String provider) {
mTxtTodayDate.setText("GPS Provider Disabled\n");
}
@Override
public void onLocationChanged(Location location) {
Log.v("MainActivity", "Location Changed in location listener");
mTxtTodayDate.setText(new SimpleDateFormat("MM/dd/yyyy", Locale
.getDefault()).format(new Date(location.getTime())));
}
};
private LocationListener networkProvider = new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
switch (status) {
case LocationProvider.AVAILABLE:
mTxtTodayDate.setText( "NETWORK PROVIDER available again\n");
break;
case LocationProvider.OUT_OF_SERVICE:
mTxtTodayDate.setText( "NETWORK PROVIDER out of service\n");
break;
case LocationProvider.TEMPORARILY_UNAVAILABLE:
mTxtTodayDate.setText("NETWORK PROVIDER temporarily unavailable\n");
break;
}
}
@Override
public void onProviderEnabled(String provider) {
mTxtTodayDate.setText("NETWORK PROVIDER Provider Enabled\n");
}
@Override
public void onProviderDisabled(String provider) {
mTxtTodayDate.setText("NETWORK PROVIDER Provider Disabled\n");
}
@Override
public void onLocationChanged(Location location) {
Log.v("MainActivity", "Location Changed in location listener");
mTxtTodayDate.setText(new SimpleDateFormat("MM/dd/yyyy", Locale
.getDefault()).format(new Date(location.getTime())));
}
};
private OnClickListener mShowDateListener = new OnClickListener() {
@Override
public void onClick(View v) {
getDate();
}
};
private void getDate(){
Log.v("MainActivity", "getDate()");
LocationManager locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
boolean flag = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
boolean flag1 = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
Log.v("MainActivity", "NETWORK_PROVIDER Enabled: "+flag+" GSP:"+flag1);
if(!flag && !flag1){
Builder dialog = new AlertDialog.Builder(this);
dialog.setMessage("Activate location listener" );
dialog.setPositiveButton("Setting", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
// TODO Auto-generated method stub
startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 100);
paramDialogInterface.dismiss();
}
});
dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int paramInt) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
dialog.show();
}else{
Log.v("MainActivity", "Provider enabled");
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, networkProvider);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, gpsProvider);
}
}
I added below permission in manifest,
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION"/>
I am doing this location listener to get current day's date only, because my project fully related with date. But application user changing date and hacking my application functions. To avoid this i am planned to get current day's date from Location
. Here I strickly do not want to use INTERNET
to get current day's date. If have any idea regarding this please share with me. Thanks in advance.