反复调用onDistance()函数 [英] Repeatedly Call onDistance() function

查看:84
本文介绍了反复调用onDistance()函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨伙计们,



我有一个简单的问题,我开发了一个应用程序,可以将坐标从一个设备(客户端A)传输到另一个设备(客户端B)并且获取两者之间的距离,我使用distanceTo()来获取距离,这是在客户端B上计算的。但是这只在客户端B中的onLocationChanged中调用,并且想法是客户端A将漫游并且客户B几乎是静止的。所以检查客户B onLocationChanged的距离是不理想的。



有没有办法不断调用distanceTo()方法,如果我把它放了一段时间(true)循环,它永远不会退出,下一行代码不会被执行。如果可能的话,我会很感激这里有一些我的客户B代码:



 public void onCreate(Bundle) savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.parent);
submit =(Button)findViewById(R.id.buttonSubmit);
submit.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
// TODO自动生成的方法stub
happiness.add((Integer) item);
}
});
ArrayAdapter< CharSequence> adapter = ArrayAdapter.createFromResource(
this,R.array.happiness,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner =(Spinner)findViewById(R.id.happy);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
LocationManager mlocationManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocationListener = new MyLocationListener();
mlocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
0,0,mlocationListener);
Thread rec = new Thread(new Runnable(){
public void run(){
// TODO自动生成的方法stub
try {
s = new Socket (192.168.1.2,1980); //连接到服务器
// s = new Socket(86.46.233.230,1980);
InputStream fromServer = s.getInputStream();
if(!s.isConnected()){
Toast.makeText(getApplicationContext(),
No Connection,Toast.LENGTH_LONG)。show();
}
while(s.isConnected()){
Scanner r = new Scanner(fromServer);
if(r.hasNextLine()){
location = r.nextLine();
}
String [] currLocation = location.split(,); // STRING是每个COMMA的分支
Clatitude = Double.valueOf(currLocation [0]); //获取ARRAY的第一个元素
Clongitude = Double.valueOf(currLocation [1]); //使用ARRAY的第二个元素
//按顺序在UI线上运行
mHandler.post(new Runnable(){
public void run(){
TextView myTextview1 =(TextView)findViewById(R.id.childCo);
myTextview1.setText(location);
}
});
}
} catch(UnknownHostException e){
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
}
}
});
rec.start();
}
// ======================================= ==============================================
public class MyLocationListener实现LocationListener {
public void onProviderDisabled(String provider){
Toast.makeText(getApplicationContext(),Gps Disabled,
Toast.LENGTH_SHORT).show();
}
public void onProviderEnabled(String provider){
Toast.makeText(getApplicationContext(),Gps Enabled,
Toast.LENGTH_SHORT)。show();
}
public void onStatusChanged(String provider,int status,Bundle extras){
}
public void onLocationChanged(Location location){
// TODO Auto-generated方法存根
Platitude = location.getLatitude();
Plongitude = location.getLongitude();
//将父坐标添加到字符串以进行打印 - (测试)
ParentCoordinates =(Platitude +,+ Plongitude);
TextView myTextview2 =(TextView)findViewById(R.id.parentCo);
myTextview2.setText(ParentCoordinates);
位置child = new位置(A点); //初始化儿童位置
child.setLatitude(Clatitude); //设置儿童位置LAT& LONG
child.setLongitude(Clongitude);
位置parent = new位置(B点); //初始化儿童位置
parent.setLatitude(Platitude); //设置儿童位置LAT& LONG
parent.setLongitude(Plongitude);
float distance = child.distanceTo(parent); //从父母位置到儿童位置的距离
String dis =(+ distance);
TextView myTextview1 =(TextView)findViewById(R.id.distTo); //在文本视图中显示距离
myTextview1.setText(dis);
}
// ======================================= ==============================================
}
// =========================================== ==========================================
public void onItemSelected( AdapterView<?> parent,View view,int pos,long id){
// TODO自动生成的方法stub
item = parent.getItemAtPosition(pos); //从SPINNER中选择项目
}
public void onNothingSelected(AdapterView<?> arg0){
// TODO自动生成的方法stub
}
/ / ================================================= =====================================
}



先谢谢大家!!!

解决方案





试试使用计时器控制刻度事件在连续的时间间隔内触发该功能,



设置以下属性



 imer.Interval =( 1000 )*( 10 );  //  计时器将标记为10秒 
timer.Enabled = ; // 启用计时器
timer.Start();


Hi folks,

I''ve a quick question, I have developed an application that transmits coordinates from one device (Client A) to another (Client B) and gets the distance between both, I am using the distanceTo() to get the distance, this is calculated on Client B. However this is only called in the onLocationChanged in Client B, and the idea is that Client A will be roaming and Client B will be almost stationary. So checking the distance in Client B onLocationChanged isn''t ideal.

Is there a way to constantly call the distanceTo() method, if I put it in a while(true) loop, it will never exit and the next line of code won''t be executed. I''d appreciate a dig out on this if possible here''s some of my client B code:

public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.parent);
        submit = (Button) findViewById(R.id.buttonSubmit);
        submit.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // TODO Auto-generated method stub
                happiness.add((Integer) item);
            }           
        });
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.happiness, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner = (Spinner) findViewById(R.id.happy);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(this);
        LocationManager mlocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        LocationListener mlocationListener = new MyLocationListener();
        mlocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                0, 0, mlocationListener);
        Thread rec = new Thread(new Runnable() {
            public void run() {
                // TODO Auto-generated method stub
                try {
                    s = new Socket("192.168.1.2", 1980); // Connecting to Server
                    //s = new Socket("86.46.233.230", 1980); 
                    InputStream fromServer = s.getInputStream();
                    if (!s.isConnected()) {
                        Toast.makeText(getApplicationContext(),
                                "No Connection", Toast.LENGTH_LONG).show();
                    }
                    while (s.isConnected()) {
                        Scanner r = new Scanner(fromServer);
                        if (r.hasNextLine()) {
                            location = r.nextLine();
                        }
                        String[] currLocation = location.split(","); // STRING IS SPLIT EACH SIDE OF COMMA
                        Clatitude = Double.valueOf(currLocation[0]); // TAKING FIRST ELEMENT OF ARRAY
                        Clongitude = Double.valueOf(currLocation[1]); // TAKING SECOND ELEMENT OF ARRAY
                        // IN ORDER TO RUN ON THE UI THREAD
                        mHandler.post(new Runnable() {
                            public void run() {
                                TextView myTextview1 = (TextView) findViewById(R.id.childCo);
                                myTextview1.setText(location);
                            }
                        });
                    }
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        rec.start();
    }
    // =====================================================================================
    public class MyLocationListener implements LocationListener {
        public void onProviderDisabled(String provider) {
            Toast.makeText(getApplicationContext(), "Gps Disabled",
                    Toast.LENGTH_SHORT).show();
        }
        public void onProviderEnabled(String provider) {
            Toast.makeText(getApplicationContext(), "Gps Enabled",
                    Toast.LENGTH_SHORT).show();
        }
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
            Platitude = location.getLatitude();
            Plongitude = location.getLongitude();
            //Adding parent coordinates to string for printing - (testing)
            ParentCoordinates = (Platitude + " , " + Plongitude);  
            TextView myTextview2 = (TextView) findViewById(R.id.parentCo);
            myTextview2.setText(ParentCoordinates);
            Location child = new Location("point A");     //INITIALIZING CHILD LOCATION
            child.setLatitude(Clatitude);                 //SETTING CHILD LOCATION LAT & LONG
            child.setLongitude(Clongitude);
            Location parent = new Location("point B");    //INITIALIZING CHILD LOCATION
            parent.setLatitude(Platitude);                //SETTING CHILD LOCATION LAT & LONG
            parent.setLongitude(Plongitude);
            float distance = child.distanceTo(parent);    //GETTING DISTANCE FROM PARENT LOCATION TO CHILD LOCATION
            String dis = ("" + distance);
            TextView myTextview1 = (TextView) findViewById(R.id.distTo);  //DISPLAYING DISTANCE IN TEXT VIEW
            myTextview1.setText(dis);
        }       
        // =====================================================================================
    }
        // =====================================================================================
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
        // TODO Auto-generated method stub
        item = parent.getItemAtPosition(pos);       //GETTING ITEM SELECTED FROM SPINNER
    }
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub
    }
        // ======================================================================================
}


Thanks in advance folks!!!

解决方案

Hi,

try to use timer control tick event to fire that function in continous time interval,

set following properties

imer.Interval = (1000) * (10);             // Timer will tick evert 10 seconds
            timer.Enabled = true;                       // Enable the timer
            timer.Start();


这篇关于反复调用onDistance()函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆