我知道在SO中多次询问此问题,并且我已尝试关注20个网页内容。 page 1 page 2 page 3 page 4 等等页面...
基本上我的代码source是Androidhive.info
我的问题是 - 在log cat中它显示的位置,但它没有保存在我的数据库中..
我在服务器上有一个php文件,我在按钮点击时通过 httppost 调用它。我的座右铭是
1.单击按钮,它应发送位置(此任务已完成)
2.App应该在后台运行并按时间间隔发送位置详细信息(任务待定)
正如SO I扩展服务所示,但我没有在这里使用任何警报管理器或作业调度程序
这让我的代码变得笨拙..
所以 我的问题是 应该在onLocationchanged方法中编写什么代码,以便每当位置发生变化时它都会向服务器发送数据(程序将每n分钟运行一次)
Code snippets ::
1.GPSTracker.java
public class GPSTracker extends Service implements LocationListener
{
statements;
}
2. GPSTracker.java中的一部分
public void onLocationChanged(Location location)
{
latitude = location.getLatitude();
longitude = location.getLongitude();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,0, this);
Log.e("onLocationChanged",Double.toString(latitude));
Log.e("onLocationChanged",Double.toString(longitude));
}
3.MainActivity.java
btnShowLocation.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View arg0)
{
new LongOperation().execute();
}
}
4.MainActivity.java
private class LongOperation extends AsyncTask<String, Void, Void>
{
protected void onPreExecute()
{
}
protected Void doInBackground(String... urls)
{
ArrayList<NameValuePair> paramss = new ArrayList<NameValuePair>();
paramss.add(new BasicNameValuePair("latti", latti));
paramss.add(new BasicNameValuePair("longi", longi));
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http:///insertData.php");
httppost.setEntity(new UrlEncodedFormEntity(paramss));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("pass 1", "connection success ");
}
catch(Exception e)
{
Log.e("Fail 1", e.toString());
Toast.makeText(getApplicationContext(), "Invalid IP Address",
Toast.LENGTH_LONG).show();
}
try
{
BufferedReader reader = new BufferedReader
(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
result = sb.toString();
Log.e("pass 2", "connection success ");
}
catch(Exception e)
{
Log.e("Fail 2", e.toString());
}
try
{
JSONObject json_data = new JSONObject(result);
code=(json_data.getInt("code"));
if(code==1)
{
FLAG=1;
}
else
{
FLAG=0;
}
}
catch(Exception e)
{
Log.e("Fail 3", e.toString());
}
return null;
}
}
任何建议都会有很大的帮助..
请告知是否需要更多代码段
我在Android studio中正在做这个项目
LOG ::
02-02 10:52:22.180 15792-15792/com.example.mylocation E/onLocationChanged﹕ 17.3732771
02-02 10:52:22.180 15792-15792/com.example.mylocation E/onLocationChanged﹕ 78.5046526
02-02 10:52:24.050 15792-16248/com.example.mylocation E/pass 1﹕ connection success
02-02 10:52:24.070 15792-16248/com.example.mylocation E/pass 2﹕ connection success
02-02 10:53:22.130 15792-15792/com.example.mylocation E/onLocationChanged﹕ 17.3733159
02-02 10:53:22.130 15792-15792/com.example.mylocation E/onLocationChanged﹕ 78.5045958
02-02 10:54:22.070 15792-15792/com.example.mylocation E/onLocationChanged﹕ 17.3733133
02-02 10:54:22.070 15792-15792/com.example.mylocation E/onLocationChanged﹕ 78.5046106
02-02 10:55:22.120 15792-15792/com.example.mylocation E/onLocationChanged﹕ 17.373314
02-02 10:55:22.120 15792-15792/com.example.mylocation E/onLocationChanged﹕ 78.5046077
它每分钟都在给我一个日志中的位置更新..但这个数据不会发送到数据库
我已经在doInBackground()中写了http调用,所以控件没有传递给那个方法......它永远不会被再次调用..
编辑1
当我使用待定意图时,即使我的GPS设置已启用,此屏幕也是输出
添加了代码::
public Location getLocation()
{
try
{
intent = new Intent("com.example.dotweb.mylocation");
pi = PendingIntent.getBroadcast(getApplicationContext(),0, intent, 0);
locationManager = (LocationManager) mContext
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
} else {
this.canGetLocation = true;
if (isNetworkEnabled)
{
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,60000,0, this);
Log.d("Network", "Network");
if (locationManager != null)
{
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
if (isGPSEnabled) {
if (location == null)
{
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,60000,0, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,60000,0, this);
并且locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,60000,0, pi);
正在做这个伎俩
答案 0 :(得分:0)
onLocationChanged内容。
@Override
public void onLocationChanged(Location location)
{
latitude = location.getLatitude();
longitude = location.getLongitude();
new LongOperation(latitude, longitude).execute();
}
LongOperation
是AsyncTask class
,其http call
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("www.abc.com/kkk/ppp/nnn/update.php");
httppost.setEntity(new UrlEncodedFormEntity(paramss));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("pass 1", "connection success ");
} catch (Exception e) {
Log.e("Fail 1", e.toString());
}