如何在Java中每隔10秒打印一次吐司?

时间:2011-09-28 14:19:33

标签: android

我想每隔10秒打印吐司Toast.makeText(current location,latitude,longitude)

public class SimpleService extends Service {


    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        Toast.makeText(this,"Service created ...", Toast.LENGTH_LONG).show();

        LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
        Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        double longitude = location.getLongitude();
        double latitude = location.getLatitude();
        Toast.makeText(this, "current location, latitude,longitude", 0);

    }



    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this, "Service destroyed ...", Toast.LENGTH_LONG).show();
    }
}



public class SimpleServiceController extends Activity {

       @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            Button start = (Button)findViewById(R.id.serviceButton);
            Button stop = (Button)findViewById(R.id.cancelButton);

            start.setOnClickListener(startListener);
            stop.setOnClickListener(stopListener);

       }

       private OnClickListener startListener = new OnClickListener() {
        public void onClick(View v){
            startService(new Intent(SimpleServiceController.this,SimpleService.class));


        }

       };

       private OnClickListener stopListener = new OnClickListener() {
            public void onClick(View v){
                stopService(new Intent(SimpleServiceController.this,SimpleService.class));
            }               
          };
}

1 个答案:

答案 0 :(得分:-2)

while(true){
    try{
        Thread.sleep(10000);
    catch(Exception ex){}

    Toast.makeText(this, "current location, latitude,longitude", 0).show();
}

以与问题相同的模糊回答