4th*_*his 6 android android-fragments
我有一个活动类,在它的onResume部分我已经使用了下一个代码 -
@Override
protected void onResume() {
super.onResume();
bindService(new Intent(MainActivity.this, IMService.class), mConnection , Context.BIND_AUTO_CREATE);
}
Run Code Online (Sandbox Code Playgroud)
在onPause我使用下一个代码 -
@Override
protected void onPause()
{
unbindService(mConnection);
super.onPause();
}
Run Code Online (Sandbox Code Playgroud)
您可以理解我对服务具有约束力和解除绑定性.
现在问题是我想使用片段而不是Activity.
为了做到这一点,我已经将onPuse代码更改为 -
@Override
protected void onPause()
{
getActivity().unbindService(mConnection);
super.onPause();
}
Run Code Online (Sandbox Code Playgroud)
看起来很好.
但onResume我遇到的问题是绑定部分,我已经尝试了下一个代码 -
@Override
protected void onResume() {
super.onResume();
bindService(new Intent(getActivity(), IMService.class), mConnection , Context.BIND_AUTO_CREATE);
}
Run Code Online (Sandbox Code Playgroud)
但Eclipse给我一个错误说 -
The method bindService(Intent, ServiceConnection, int) is undefined for the type MainFragment
Run Code Online (Sandbox Code Playgroud)
该服务在清单中显示如下 -
<service android:name="com.example.test.services.IMService" >
</service>
Run Code Online (Sandbox Code Playgroud)
那么为什么我不能将服务绑定到片段中呢?也许我需要在getActivity代码中添加一些东西?
提供任何帮助
感谢@Raghunandan的帮助 - 这是解决方案 -
getActivity().bindService(new Intent(getActivity(), IMService.class), mConnection , Context.BIND_AUTO_CREATE);
Run Code Online (Sandbox Code Playgroud)
Rag*_*dan 16
使用
getActivity().bindService(params)
Run Code Online (Sandbox Code Playgroud)
它需要一个Context
| 归档时间: |
|
| 查看次数: |
6738 次 |
| 最近记录: |