i want to is there any to know that if data is being sent and received. as i m trying to build a application that take your current location. though i m able to find if internet is connected or not, but if internet is connected and so my app say it is connected and moves ahead to fetch data . as even though net is connected but i m not receiving any packets of data . so how can i check if i m receiving and data from net then only i want to ahead . any help would be appreciated
public class Getlocation extends ActionBarActivity implements LocationListener {
boolean isNetworkEnabled = false;
boolean isGpsworking = false, dialogshown = false;
String phone, lattitude, longitude;
LocationManager locationmanager;
Location location;
double userlat, userlng;
AlertDialog.Builder alertDialog;
SharedPreferences pref;
boolean activitypaused = false;
ProgressDialog pdailog;
Geocoder geocoder;
StringBuilder temp_location;
TextView t1, t2;
Button b1;
CheckInternetConnection connected_to_internet;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_getlocation);
pref = getSharedPreferences("Example", Context.MODE_PRIVATE);
geocoder = new Geocoder(this, Locale.getDefault());
pdailog = new ProgressDialog(Getlocation.this);
t1 = (TextView) findViewById(R.id.get_location_text_view);
t2 = (TextView) findViewById(R.id.get_location_text_view_static);
connected_to_internet = new CheckInternetConnection(getApplicationContext());
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
//bar = getSupportActionBar()
this.setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle("My Location");
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
sample.statusbarcolor(this);
}
b1 = (Button) findViewById(R.id.get_location_button);
b1.setVisibility(View.VISIBLE);
if (pref.getString("lat", null) == null & pref.getString("lng", null) == null) {
pdailog.setTitle("Please Wait");
pdailog.setMessage("Please wait!Fecthing your Current location");
pdailog.show();
getloc();
pdailog.dismiss();
} else if (getIntent().getExtras().get("Coming_From").equals(0)) {
Intent intent = new Intent("com.sample.MAINACTIVITY");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
} else if (getIntent().getExtras().get("Coming_From").equals(1)) {
t1.setVisibility(View.VISIBLE);
t2.setVisibility(View.VISIBLE);
b1.setVisibility(View.VISIBLE);
if (!(pref.getString("Location_Name", null)).equalsIgnoreCase("")) {
t1.setText("" + pref.getString("Location_Name", "No Location Found"));
}
}
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
pdailog.setTitle("please wait");
pdailog.setMessage("Please wait!Fecthing your Current location");
pdailog.show();
getloc();
pdailog.dismiss();
}
});
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.getlocation, menu);
return true;
}
public boolean onPrepareOptionsMenu(Menu menu)
{
MenuItem homeItem = menu.findItem(R.id.actionHome);
homeItem.setVisible(true);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
case R.id.actionHome:
Intent intent = new Intent("com.sample.MAINACTIVITY");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
public void getloc() {
if (connected_to_internet.Connected_To_Internet()) {
locationmanager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
isNetworkEnabled = locationmanager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (isNetworkEnabled) {
locationmanager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 1000 *5, 1, this);
} else if (!(dialogshown)) {
showSettingsAlert();
}
Runnable run_object = new Runnable() {
public void run() {
if (isNetworkEnabled) {
if (locationmanager != null) {
try {
location = locationmanager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
} catch (Exception e) {
e.printStackTrace();
if (location == null) {
getloc();
}
}
if (location != null) {
userlat = location.getLatitude();
userlng = location.getLongitude();
List<Address> address;
String locality = null;
try {
address = geocoder.getFromLocation(userlat, userlng, 1);
temp_location = new StringBuilder();
Address add = address.get(0);
temp_location.append(add.getSubLocality());
locality = add.getLocality();
} catch (Exception e) {
}
if (!locality.equalsIgnoreCase(temp_location.toString())) {
pref.edit().putString("Location_Name", temp_location.toString() + ", " + locality).commit();
pref.edit().putString("City_name",locality).commit();
} else {
pref.edit().putString("Location_Name", temp_location.toString()).commit();
pref.edit().putString("City_name",locality).commit();
}
pref.edit().putString("lat", Double.toString(userlat)).commit();
pref.edit().putString("lng", Double.toString(userlng)).commit();
if (getIntent().getBooleanExtra("SplashScreen", false)) {
Intent intent = new Intent("com.sample.LOGIN");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
} else {
Intent in = new Intent("com.sample.MAINACTIVITY");
Toast.makeText(getApplicationContext(),"Your location is set to "+ temp_location.toString() + ", " + locality, Toast.LENGTH_LONG).show();
startActivity(in);
}
} else {
// Toast.makeText(getApplicationContext(), "Location Can Not Obtained! Closing Application. Try Again!", Toast.LENGTH_LONG).show();
Intent intent = new Intent(Getlocation.this, SplashScreen.class);
startActivity(intent);
finish();
}
}
} else {
/* if(!(dialogshown)) {
showSettingsAlert();
Toast.makeText(getApplicationContext(), "Please Turn-On Your GPS!", Toast.LENGTH_LONG).show();
}*/
}
}
};
new Handler().post(run_object);
}else{
Toast.makeText(getApplicationContext(),"Please Check Internet Connection",Toast.LENGTH_LONG).show();
}
}
public void showSettingsAlert() {
alertDialog = new AlertDialog.Builder(this);
dialogshown = true;
alertDialog.setCancelable(false);
// Setting Dialog Title
alertDialog.setTitle("GPS Required");
// Setting Dialog Message
alertDialog.setMessage("GPS is not enabled. Please go to settings and enable GPS.");
// Setting Icon to Dialog
// alertDialog.setIcon(R.drawable.delete);
// On pressing Settings button
alertDialog.setPositiveButton("Settings",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(
Settings.ACTION_LOCATION_SOURCE_SETTINGS);
dialog.dismiss();
startActivity(intent);
}
});
// on pressing cancel button
alertDialog.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
dialogshown = false;
if (getIntent().getExtras().get("Coming_From").equals(1)) {
//do something
} else {
Toast.makeText(getApplicationContext(), "Application Requires One-Time Location To Work Properly.", Toast.LENGTH_LONG).show();
}
}
});
// Showing Alert Message
alertDialog.show();
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}`