I want to send JSON String
{
"latitude": 53.86898504,
"longitude": 10.66561187,
"time": "25.04.2015 11:37:11",
"route": 4
}
to the server every 60 seconds so when I try to send the JSON string from my internet connection BroadcastReceiver the JSON string is null there but when I send the data from onLocationChanged method I am getting the string in the PostData class but I want to send the data just when the internet connection is avialable if the internet is not avialable i will store the string for short time. How can I implement it to get the JSONString in the CONNECTIVITY_ACTION BroadcastReceiver?
I appreciate any help.
MainActivity class:
public class MainActivity extends ActionBarActivity {
Location location;
LocationManager locationManager;
String jSONString;
TextView textJSON;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textJSON = (TextView) findViewById(R.id.textJSON);
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener ll = new myLocationListener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 0, ll);
}
private class BroadcastReceiverListener extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(
android.net.wifi.WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
//code to get the strongest wifi access point in the JSON string the routes's value.
}
else if (intent.getAction().equals(
android.net.ConnectivityManager.CONNECTIVITY_ACTION)) {
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = connectivityManager
.getActiveNetworkInfo();
boolean isConnected = netInfo != null
&& netInfo.isConnectedOrConnecting();
if (isConnected) {
Toast.makeText(context,
"The device is connected to the internet ",
Toast.LENGTH_SHORT).show();
if (location == null) {
Location locat = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (locat == null) {
LocationListener locLis = new myLocationListener();
LocationManager locMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
Looper myLooper = Looper.myLooper();
locMan.requestSingleUpdate(criteria, locLis,
myLooper);
} else {
System.out.println("locat is not null");
}
} else {
PostData sender = new PostData();
sender.timer(jSONString);
textJSON.setText(jSONString);
}
} else {
Toast.makeText(context,
"Please connect the device to the internet.",
Toast.LENGTH_SHORT).show();
}
}
}
}
class myLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
if (location != null) {
double pLong = location.getLongitude();
double pLat = location.getLatitude();
...
String time = sdf.format(location.getTime());
jSONString = convertToJSON(pLong, pLat, time);
System.out.println("The output of onLocationChanged: "+ jSONString);
//The code works fine here. JSON string has its values here but in broadcastReceiver JSON string has null.
// PostData sender = new PostData();
// sender.timer(jSONString);
// textJSON.setText(jSONString);
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire