0

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;
}

}

4

1 回答 1

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;
}

}
于 2013-01-03T16:53:28.060 回答