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即可。
虽然不知道为什么,但问题解决啦!
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即可。
虽然不知道为什么,但问题解决啦!
相关文章推荐
- Android Service中使用Toast无法正常显示问题的解决方法
- Reporting Service Tips 101(#2) - 关于使用Sum函数会遇到的问题(1)
- Service中使用Toast出现问题及解决方法
- Android中Service的onStartCommand使用和遇到的问题
- 使用ACE日志策略时遇到的问题:DLL::open failed for ACE: Error: check log for details Unable to find service 'Logger'
- 全网最详细使用Scrapy时遇到0: UserWarning: You do not have a working installation of the service_identity module: 'cannot import name 'opentype''. Please install it from ..的问题解决(图文详解)
- Service中使用Toast显示问题
- Reporting Service Tips 101(#3) - 关于使用Sum函数会遇到的问题(2)
- Reporting Service Tips 101(#2) - 关于使用Sum函数会遇到的问题(1)
- Web Service (014---webservice使用Jquery、Ajax请求WebService所遇到的跨域问题)
- Service中使用Toast显示问题
- 使用ServiceStack.Redis过程中遇到的问题
- .net中使用TripleDESCryptoServiceProvider进行3DES加密遇到弱密钥的问题
- datagrid中使用dropdownlist编辑模版时遇到问题
- 关于您提到使用WebServices时候遇到“基础连接以关闭”的问题
- updater application block v2.0的使用过程中遇到的问题及解决办法
- 使用Oracle中fileopen命令加载文件时遇到的怪问题
- 使用css expression遇到的一个问题
- 使用Access存储Session,遇到了SessionId问题?
- 使用logic:iterate遇到的问题