我要发送JSON字符串
{
"latitude": 53.86898504,
"longitude": 10.66561187,
"time": "25.04.2015 11:37:11",
"route": 4
}
每 60 秒向服务器发送一次,所以当我尝试从我的互联网连接 BroadcastReceiver 发送 JSON 字符串时,那里的 JSON 字符串为 null 但是当我从 onLocationChanged 方法发送数据时,我在 PostData 类中获取字符串,但我想要如果互联网不可用,则在互联网连接可用时发送数据,我将短时间存储该字符串。我如何实现它以获取 CONNECTIVITY_ACTION BroadcastReceiver 中的 JSONString?
我很感激任何帮助。
MainActivity 类:
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);
}
}
}
}
最佳答案
看起来你可以通过添加一个额外的标志作为实例变量来确定你是否在没有互联网的情况下获得位置更新,并使 isConnected
标志成为一个实例变量,从而获得你想要的行为.
想法是,如果有互联网连接,则每次收到位置更改事件时都发送位置数据,否则设置标志并等待互联网连接,以发送上次位置更新的数据。
我把你在问题中发布的原始代码进行了修改:
public class MainActivity extends ActionBarActivity {
int route_number;
double pLong;
double pLat;
String time;
String jSONString;
boolean isConnected; //added
boolean locationUpdatedNoInternet; //added
long lastLocationTime = 0; //added
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();
//set instance variable isConnected here instead of creating local variable
isConnected = netInfo != null
&& netInfo.isConnectedOrConnecting();
//boolean isConnected = netInfo != null
//&& netInfo.isConnectedOrConnecting();
if (isConnected) {
Toast.makeText(context,
"The device is connected to the internet ",
Toast.LENGTH_SHORT).show();
//check if we got a location update with no internet
long currTime = System.currentTimeMillis();
if (locationUpdatedNoInternet == true) {
//check that last location send was longer than 5 seconds ago
if (lastLocationTime == 0 || ((currTime - lastLocationTime) > 5000 )){
lastLocationTime = System.currentTimeMillis(); //set last sent time
locationUpdatedNoInternet = false; //re-set to false
PostData sender = new PostData();
//since we are checking locationUpdatedNoInternet flag, we definitely have location data.
System.out.println("The output of internet broadcast: " + jSONString);
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) {
pLong = location.getLongitude();
pLat = location.getLatitude();
//...
time = sdf.format(location.getTime());
jSONString = convertToJSON(pLong, pLat, time);
System.out.println("The output of onLocationChanged: "+ jSONString);
long currTime = System.currentTimeMillis();
if (isConnected == true) {
//check that last location send was longer than 5 seconds ago
if (lastLocationTime == 0 || ((currTime - lastLocationTime) > 5000 )) {
//if connected, just send the data
lastLocationTime = System.currentTimeMillis(); //set last sent time
locationUpdatedNoInternet = false;
PostData sender = new PostData();
sender.timer(jSONString);
textJSON.setText(jSONString);
}
}
else{
//set flag so that the last location update will be sent once connection is established
locationUpdatedNoInternet = true;
}
}
}
}
}
关于android - 当互联网连接可用时从 BoadcastReceiver 发送纬度和经度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29863769/