如何在Android中停止我的应用程序服务?

时间:2013-03-05 08:03:13

标签: android service

我写了一个服务和广播接收器类。此服务绑定任务。我可以成功启动服务和广播接收器。我也可以停止广播接收器。但我无法阻止我的服务。

这是我启动和停止服务的代码。

public class Pedometer extends Activity {


    private boolean mIsRunning;

    public static Context mContext;

    private StartAtBootServiceReceiver receiver;
    private SharedPreferences prefs;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mContext = getBaseContext();

        setContentView(R.layout.main);

        prefs = getApplicationContext().getSharedPreferences(
                  "com.sa.sademo", Context.MODE_PRIVATE);

        receiver = new StartAtBootServiceReceiver();
        IntentFilter filter = new IntentFilter(Intent.ACTION_BOOT_COMPLETED);
        mContext.registerReceiver(receiver, filter);

        Button stopServiceAndBR = (Button) findViewById(R.id.stopServiceAndBR);
        bankButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Pedometer.this.stopStepService();
            }
        });

        startStepService();
    }


    public static Context getContext() {
        return mContext;
    }

    @Override
    protected void onResume() {
        super.onResume();

        if (mIsRunning) {
            bindStepService();
        }
    }

    @Override
    protected void onPause() {
        if (mIsRunning) {
            unbindStepService();
        }
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
    }

    private StepService mService;

    private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
            mService = ((StepService.StepBinder)service).getService();
        }

        public void onServiceDisconnected(ComponentName className) {
            mService = null;
        }
    };


    private void startStepService() {
        mIsRunning = true;
        startService(new Intent(Pedometer.this,
                StepService.class));
    }

    private void bindStepService() {
        bindService(new Intent(Pedometer.this, 
                StepService.class), mConnection, Context.BIND_AUTO_CREATE);
    }

    private void unbindStepService() {
        unbindService(mConnection);
    }

    private void stopStepService() {
        mIsRunning = false;
        if (mService != null) {
                PackageManager pm = mContext.getPackageManager();
                ComponentName component = new ComponentName(mContext, StartAtBootServiceReceiver.class);
                pm.setComponentEnabledSetting(component , PackageManager.COMPONENT_ENABLED_STATE_DISABLED , PackageManager.DONT_KILL_APP);

                unbindStepService();
                mIsRunning = false;
                if (mService != null) {
                    stopService(new Intent(Pedometer.this,
                          StepService.class));
                }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您可以在服务中执行以下操作:

//A variable to check
boolean cancel = false;

//a method to set this variable
public void cancel(){
   cancel = true;
}

//inside of some point of your service you do:

while (yourCondition){

  if (!cancel){

    //do the service work

  } else {

     throw new StopServiceException(); // you have to create it.
  }

}

在使用Service实例的应用程序中,您可以调用cancel方法:

myService.cancel();