2011-09-11 39 views
10

Hãy xem xét mã sau đây. Trong phương pháp Service.onStart() tôi đã tạo và bắt đầu một chuỗi sẽ hiển thị thông báo Bánh mì nướng nhưng nó không hoạt động!Cách hiển thị thông báo bánh mì nướng từ chủ đề nền

public class MyService extends Service{ 

    private static final String TAG = "MyService"; 
    @Override 
    public IBinder onBind(Intent intent) 
    { 
     return null;  
    }  

    @Override 
    public void onCreate() 
    { 
    Toast.makeText(this, "My Service Created", Toast.LENGTH_SHORT).show(); 
     } 
    @Override 
    public void onDestroy() 
    { 
    Toast.makeText(this, "My Service Stopped", Toast.LENGTH_SHORT).show(); 
    } 
    @Override 
    public void onStart(Intent intent, int startid) 
    { 
     Toast.makeText(this, "My Service Started", Toast.LENGTH_SHORT).show(); 
     DBIteratorThread dbThread=new DBIteratorThread(); 
     dbThread.myService=this; 
     Thread t1 = new Thread(dbThread); 
      t1.start(); 
    } 

} 
class DBIteratorThread implements Runnable 
{ 

    MyService myService; 

    public void run() 
    { 
    // Toast.makeText(myService, "Thread is Running", Toast.LENGTH_SHORT).show(); 
      } 
} 
+0

này nên làm việc cho bạn cũng http: // stackoverflow. com/questions/6134013/android-how-can-i-show-a-bánh mì nướng-từ-một-thread-chạy-trong-một-dịch vụ từ xa – eyespyus

Trả lời

0

Thay thế this bằng getBaseContext().

+0

vẫn không hoạt động –

8

Tôi đã viết một lớp để hiển thị Toasts từ các quy trình nền. Có thể được sử dụng ở mọi nơi, ví dụ: trong một số AsyncTask. Bạn chỉ cần tạo một thể hiện của lớp này như

ToastHandler mToastHandler = new ToastHandler(yourContext); 

và sau đó gọi showToast() với văn bản hoặc tài nguyên của bạn id và thời gian Toast's như bạn thường làm với makeToast().

Đây là mã hoặc tải trực tiếp link:

import android.content.Context; 
import android.os.Handler; 
import android.widget.Toast; 

/** 
* A class for showing a <code>Toast</code> from background processes using a 
* <code>Handler</code>. 
* 
* @author kaolick 
*/ 
public class ToastHandler 
{ 
    // General attributes 
    private Context mContext; 
    private Handler mHandler; 

    /** 
    * Class constructor. 
    * 
    * @param _context 
    *   The <code>Context</code> for showing the <code>Toast</code> 
    */ 
    public ToastHandler(Context _context) 
    { 
    this.mContext = _context; 
    this.mHandler = new Handler(); 
    } 

    /** 
    * Runs the <code>Runnable</code> in a separate <code>Thread</code>. 
    * 
    * @param _runnable 
    *   The <code>Runnable</code> containing the <code>Toast</code> 
    */ 
    private void runRunnable(final Runnable _runnable) 
    { 
    Thread thread = new Thread() 
    { 
     public void run() 
     { 
     mHandler.post(_runnable); 
     } 
    }; 

    thread.start(); 
    thread.interrupt(); 
    thread = null; 
    } 

    /** 
    * Shows a <code>Toast</code> using a <code>Handler</code>. Can be used in 
    * background processes. 
    * 
    * @param _resID 
    *   The resource id of the string resource to use. Can be 
    *   formatted text. 
    * @param _duration 
    *   How long to display the message. Only use LENGTH_LONG or 
    *   LENGTH_SHORT from <code>Toast</code>. 
    */ 
    public void showToast(final int _resID, final int _duration) 
    { 
    final Runnable runnable = new Runnable() 
    { 
     @Override 
     public void run() 
     { 
     // Get the text for the given resource ID 
     String text = mContext.getResources().getString(_resID); 

     Toast.makeText(mContext, text, _duration).show(); 
     } 
    }; 

    runRunnable(runnable); 
    } 

    /** 
    * Shows a <code>Toast</code> using a <code>Handler</code>. Can be used in 
    * background processes. 
    * 
    * @param _text 
    *   The text to show. Can be formatted text. 
    * @param _duration 
    *   How long to display the message. Only use LENGTH_LONG or 
    *   LENGTH_SHORT from <code>Toast</code>. 
    */ 
    public void showToast(final CharSequence _text, final int _duration) 
    { 
    final Runnable runnable = new Runnable() 
    { 
     @Override 
     public void run() 
     { 
     Toast.makeText(mContext, _text, _duration).show(); 
     } 
    }; 

    runRunnable(runnable); 
    } 
} 
+0

mục đích của "thread.interrupt () "và" thread = null "trong runRunnable()? Đặt chuỗi thành null là thừa vì biến đó biến mất khi hàm trả về. – Clyde

+0

Tôi không có chuyên gia 'thread'. Tôi chỉ muốn tránh các vấn đề hoặc tác dụng phụ có thể xảy ra. ;-) – kaolick

+0

Cảm ơn Kaolick, Hoạt động như một sự quyến rũ;) .. – Shah

19

Đỗ UI nhét trong chính chủ đề/UI. Hãy thử điều này:

Handler handler = new Handler(Looper.getMainLooper()); 

handler.post(new Runnable() { 

     @Override 
     public void run() { 
      //Your UI code here 
     } 
    }); 
0

Bạn không thể hiển thị Bánh mì nướng trên sợi không phải là sợi chỉ của hoạt động.
bạn chỉ có thể chạy nó ở một nơi khác nếu bạn sử dụng phương pháp runOnUiThread để nó chạy trên thread ui

Nhìn vào câu hỏi này
Android: Toast in a thread

Các vấn đề liên quan