我用okhttp请求服务器,在得到结果以后,根据结果内容,开启一个服务,服务是用来进行一个倒计时功能的,
我是在okhttp里的
public void onResponse(Call call, Response response) throws IOException {
Looper.prepare();
String str=response.body().string();
/**
* 根据服务器返回结果弹出Toast
*/
if (str.equals("预约成功")){
Toast.makeText(mContext,str,Toast.LENGTH_SHORT).show();
/**
* 定时任务,闹钟提醒
*/
ShareUtils.putString(mContext.getApplicationContext(),"ROOMNUMBER",POsition);
ShareUtils.putString(mContext.getApplicationContext(),"SEATNMBER",number);
//***************
Intent intent1=new Intent(mContext, MyService.class);
mContext.startService(intent1);
//***************
}else {
Toast.makeText(mContext,str,Toast.LENGTH_SHORT).show();
}
/**
*
*/
Looper.loop();
}
但我感觉这个方法不是在子线程里面运行吗?我在这个里面开启的服务,能关闭吗?
我是想在一个fragment里面,关闭这个服务,但是我关闭服务的代码写在主线程了,
两个不一样的线程开启服务,关闭服务能行吗?
handler =new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what){
case 9:
//关掉预约成功之后计时的服务
Intent stopIntent = new Intent(getActivity(),MyService.class);
getActivity().stopService(stopIntent);
Toast.makeText(getContext(),"签到成功",Toast.LENGTH_SHORT).show();
//*******
SHUJUKU();
//********
//ShareUtils.putBoolean(getContext().getApplicationContext(),"married",true);
ShareUtils.putBoolean(getContext().getApplicationContext(),"married",true);
SharedPreferences.Editor editor4 = getContext().getSharedPreferences("data", Context.MODE_PRIVATE)
.edit();
editor4.putString("STATE", "1");
editor4.apply();
onResume();
break;
我想在这里关闭这个服务,请问我目前这样写能达到我想要的效果吗?
我如果在第一个请求结果里面也用hander处理,在hander里面来开启这个服务会怎样呢?