开发者

How to print toast every 10 seconds in Java?

I want to print toast for every 10 seconds that is 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();

        Loc开发者_运维百科ationManager 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));
            }               
          };
}


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

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

Answered in the same vagueness as the question

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜