Service中调用Toast小记

Toast 显示需要出现在一个线程的消息队列中

 

public class TestService extends    Service { 
  private Handler handler; 
  @Override 
  public IBinder onBind(Intent intent){ 
    return null
  } 
    
  @Override 
  public void onCreate(){ 
         handler = new Handler(Looper.getMainLooper());                                                 
                System.out.println("service started"); 
         handler.post(new Runnable() {     
                         @Override     
                         public void run() {     
                                Toast.makeText(getApplicationContext(), "Test",Toast.LENGTH_SHORT).show();     
                         }     
                }); 
        } 
}     

你可能感兴趣的:(Android开发)