您的位置:首页 > 其它

Service中使用Toast,及遇到的问题

2017-07-18 20:27 232 查看
在Service中开启蓝牙连接,连接成功后Toast,提示连接状态,方法如下:

public class BluetoothService extends Service {

@Override
public IBinder onBind(Intent intent){
return null;
}

@Override
public void onCreate(){
handler=new Handler(Looper.getMainLooper());
handler.post(new Runnable() {   

@Override 

public void run() {    

           Toast.makeText(getApplicationContext(), text,Toast.LENGTH_SHORT).show();

}

});
super.onCreate();
}

}

一开始的时候Toast显示的是一个result字符串,但是出现如下问题:

Cannot refer to the non-final local variable result defined in an enclosing scope

后来上网找到了方法,final String text=result; 

在前面加final,显示text即可。

虽然不知道为什么,但问题解决啦!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Service Toast