Service中使用Toast

如下,直接显示是不行的,只有通过handler显示才可以。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
@Override
public void onCreate()
{
super.onCreate();
handler=new Handler(Looper.getMainLooper());
handler.post(new Runnable(){
public void run(){
Toast.makeText(getApplicationContext(), "Service is created!", Toast.LENGTH_LONG).show();
}
});
}
@Override
public void onStart(Intent intent,int startId)
{
super.onStart(intent, startId);
handler=new Handler(Looper.getMainLooper());
handler.post(new Runnable(){
public void run(){
Toast.makeText(getApplicationContext(), "Service is on!", Toast.LENGTH_LONG).show();
}
});
}
@Override
public void onDestroy(){
super.onDestroy();
handler=new Handler(Looper.getMainLooper());
handler.post(new Runnable(){
public void run(){
Toast.makeText(getApplicationContext(), "Service is off!", Toast.LENGTH_LONG).show();
}
});