- 论坛徽章:
- 0
|
Android:在Service中使用Toast
在Service中Toast,本来以为是和在Activity中一样直接用,结果发现没有反应,百度到的解决办法,原理暂时不清楚。
Java代码- 1.public class TestService extends Service {
- 2. private Handler handler;
- 3. @Override
- 4. public IBinder onBind(Intent intent){
- 5. return null;
- 6. }
- 7.
- 8. @Override
- 9. public void onCreate(){
- 10. handler = new Handler(Looper.getMainLooper());
- 11. System.out.println("service started");
- 12. handler.post(new Runnable() {
- 13. @Override
- 14. public void run() {
- 15. Toast.makeText(getApplicationContext(), "Test",Toast.LENGTH_SHORT).show();
- 16. }
- 17. });
- 18. }
- 19.}
复制代码 |
|