2011-09-28 44 views
0

我想打印烤麪包,每隔10秒即爲Toast.makeText(current location,latitude,longitude)如何在Java中每10秒打印一次吐司?

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

回答

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

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

在同一個模糊回答爲

+0

可以實現計時器的問題,它會比讓你的線程隱含睡得更好一點 – 2011-09-28 15:21:30