OnLocationChanged给了我11分,每次我有11次调用php页面。如何制作 确保我每次只发送最后的第11个(可能不是11个)位置点。这可能是一个很好的方法,首先将数据插入sqlite数据库,然后再调用一个计时器来获取最后一行的sqlite表和将它传递给php页面,稍后它将删除该表并在2分钟内重复所有。
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText ed1=(EditText) findViewById(R.id.editText1);
final EditText ed2=(EditText) findViewById(R.id.editText2);
final EditText ed3=(EditText) findViewById(R.id.editText3);
final WebView w=(WebView) findViewById(R.id.webView1);
final Time t=new Time();
//deo koji naknadno ubacujem za kriterijum
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setPowerRequirement(Criteria.POWER_LOW);
//kraj dela koji sam naknadno ubacio
final LocationManager m=(LocationManager) MainActivity.this.getSystemService(Context.LOCATION_SERVICE);
//deo koji sam takodje ubacio naknadno
String locationprovider =m.getBestProvider(criteria,true);
//deo koji sam takodje ubacio naknadno kraj
LocationListener l=new LocationListener() {
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onLocationChanged(Location arg0) {
/*
java.text.DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
String date = dateFormat.format(new Date());
*/
//novi datum
/*
Date datum = new Date();
SimpleDateFormat sdf;
sdf = new SimpleDateFormat("MM/dd/yyyy");
String date=sdf.format(datum);
//novi datum
*/
/*
//treci datum
String pattern = "MM/dd/yyyy";
SimpleDateFormat format = new SimpleDateFormat(pattern);
String date= format.format(new Date());
//treci datum
*/
//cetvrti datum
String format="MM/dd/yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(format,Locale.US);
String date= sdf.format(new Date());
//cetvrti datum
String url=new String();
t.setToNow();
ed1.setText("");
ed2.setText("");
String longitude=new String();
String latitude=new String();
String speed=new String();
if((int)arg0.getSpeed()==0)
{
speed="0";
}
else
{
speed=""+(((int)arg0.getSpeed()*3600)/1000);
}
longitude=""+ arg0.getLongitude();
latitude=""+ arg0.getLatitude();
ed1.setText(longitude);
ed2.setText(latitude);
ed3.setText(speed);
Calendar cal=Calendar.getInstance();
url="http://www.compensatemeonline.com/truckmeonline/TruckMeOnline/UnosLokacijaSaTelefona.php?id=16"+"&longitude="+longitude+"&latitude="+latitude+"&brzina="+speed+"&vreme="+cal.getTime()+"&datum="+date;
try{
w.loadUrl(url);
}
catch (Exception e) {
// TODO: handle exception
}
// TODO Auto-generated method stub
}
};
// TODO Auto-generated method stub
m.requestLocationUpdates(locationprovider, 120000, 0, l);
// zamenio sam m.requestLocationUpdates(LocationManager.GPS_PROVIDER, 120000, 0, l); sa m.requestLocationUpdates(locationprovider, 120000, 0, l);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
答案 0 :(得分:0)
自己解决了。由于onlocationchanged提供了10个或更多的位置数据,我将数据插入到sqlite数据库中,添加了一个处理程序,从sqlite表中取出最后一个插入的行,同时删除同一行之前的所有记录。我每隔1分钟进行一次onlocationchange并每隔1.5分钟调用一次处理程序,第一次调用处理程序时我不查询sqlite数据库,它就像这样:
public class MainActivity extends Activity {
int indikator=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText ed1=(EditText) findViewById(R.id.editText1);
final EditText ed2=(EditText) findViewById(R.id.editText2);
final EditText ed3=(EditText) findViewById(R.id.editText3);
final SQLiteDatabase db=openOrCreateDatabase("TruckMe", MODE_PRIVATE, null);
db.execSQL("create table if not exists lokacije (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, lokacija VARCHAR(255));");
final WebView w=(WebView) findViewById(R.id.webView1);
final Time t=new Time();
final Button b=(Button) findViewById(R.id.button1);
b.setTextColor(Color.RED);
//deo za thread
final Handler handler = new Handler();
final Runnable runnable = new Runnable() {
public void run() {
if(indikator != 0)
{
Cursor c=db.rawQuery("select * from lokacije", null);
c.moveToLast();
try{
w.loadUrl(c.getString(c.getColumnIndex("lokacija")));
db.execSQL("delete from lokacije where id <"+c.getString(c.getColumnIndex("id")));
}
catch (Exception e) {
// TODO: handle exception
}
}
indikator=1;
handler.postDelayed(this, 90000);
}
};
//b.setBackgroundColor(Color.RED);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(b.getText().equals("SLOBODAN"))
{
b.setText("ZAUZET");
b.setTextColor(Color.GREEN);
}
else
{
b.setText("SLOBODAN");
b.setTextColor(Color.RED);
}
}
});
//deo koji naknadno ubacujem za kriterijum
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setPowerRequirement(Criteria.POWER_LOW);
//kraj dela koji sam naknadno ubacio
final LocationManager m=(LocationManager) MainActivity.this.getSystemService(Context.LOCATION_SERVICE);
//deo koji sam takodje ubacio naknadno
String locationprovider =m.getBestProvider(criteria,true);
//deo koji sam takodje ubacio naknadno kraj
LocationListener l=new LocationListener() {
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onLocationChanged(Location arg0) {
String url=new String();
String zauzetost=new String();
zauzetost=b.getText().toString();
String format="MM/dd/yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(format,Locale.US);
String date= sdf.format(new Date());
t.setToNow();
ed1.setText("");
ed2.setText("");
String longitude=new String();
String latitude=new String();
String speed=new String();
if((int)arg0.getSpeed()==0)
{
speed="0";
}
else
{
speed=""+(((int)arg0.getSpeed()*3600)/1000);
}
longitude=""+ arg0.getLongitude();
latitude=""+ arg0.getLatitude();
ed1.setText(longitude);
ed2.setText(latitude);
ed3.setText(speed);
Calendar cal=Calendar.getInstance();
url="Some web site.phpid=1"+"&longitude="+longitude+"&latitude="+latitude+"&brzina="+speed+"&vreme="+cal.getTime()+"&datum="+date+"&zauzetost="+zauzetost;
db.execSQL("insert into lokacije (lokacija) values ('"+url+"');");
// TODO Auto-generated method stub
}
};
// TODO Auto-generated method stub
//pozivanje threada
runnable.run();
//pozivanje threada
m.requestLocationUpdates(locationprovider, 60000, 0, l);
// zamenio sam m.requestLocationUpdates(LocationManager.GPS_PROVIDER, 120000, 0, l); sa m.requestLocationUpdates(locationprovider, 120000, 0, l);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}