如何破坏服务?

时间:2014-03-19 09:23:21

标签: android service broadcastreceiver

我有一个wifi广播接收器,像这样:

Intent in = new Intent(context, WifiChangeService.class);
    in.putExtra("bssid", wi.getBSSID());
    context.startService(in);

和服务(wifichangeservice),像这样:

public class WifiChageService extends Service {



@Override
public void onCreate() {
   //here i want to test if bssid=="Home Network" , if it's equal i want to do "something"
    // then how can  I destroy or stop service , afer that "something" work is done
}

4 个答案:

答案 0 :(得分:0)

public void onDestroy()
{    
  Intent in = new Intent(context, WifiChangeService.class);
  context.stopService(in);
}

答案 1 :(得分:0)

@Override
public void onCreate() {
   //here i want to test if bssid=="Home Network" , if it's equal i want to do "something"
    // then how can  I destroy or stop service , afer that "something" work is done
  if(yourbssid == netwokbssid){
     doYourStuff();
     stopSelf(); // also you can use onDestroy() directly
   }
}

答案 2 :(得分:0)

您可以尝试使用此方法启动和停止服务。

// Method to start the service
public void startService() {
     context.startService(new Intent(context, WifiChageService.class));
}

// Method to stop the service
public void stopService() {
     context.stopService(new Intent(context, WifiChageService.class));
}

在这里,我为您Start and Stop Service添加了参考网址。希望它对你有所帮助。感谢

答案 3 :(得分:0)

致电stopSelf();停止服务,请查看下面的示例。

    public class WifiChageService extends Service {

    @Override
    public void onCreate() {

// call stopSelf() whenever you want to stop service

     stopSelf(); 

    }

    }