服务 - 服务不会停止

Service - the service isn't stopping

我遇到了一些服务问题。

服务:

public class MyService extends Service {
public MyService() {
}

private static final String TAG = "AutoService";
private Timer timer;
private TimerTask task;

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    Toast.makeText(this, "Auto Service Created", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onCreate");

    int delay = 5000; // delay for 5 sec.
    int period = 5000; // repeat every sec.

    timer = new Timer();

    timer.scheduleAtFixedRate(task = new TimerTask(){
        public void run()
        {
            System.out.println("done");
        }
    }, delay, period);


}

@Override
public boolean stopService(Intent name) {
    timer.cancel();
    task.cancel();
    return super.stopService(name);

}

@Override
public void onDestroy(){
    Toast.makeText(this, "Service Destroyed", Toast.LENGTH_SHORT).show();
    super.onDestroy();
}

主要活动:

public class ClientSocket extends ActionBarActivity {

CheckBox enablecheck;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_client_socket);

    enablecheck = (CheckBox)findViewById(R.id.enablecheck);

    enablecheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
    {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // TODO Auto-generated method stub
            if(enablecheck.isChecked()){
                startService(new Intent(ClientSocket.this, MyService.class));
            }else
            {
                stopService(new Intent(ClientSocket.this, MyService.class));

            }
        }

    });
}

清单文件:

<?xml version="1.0" encoding="utf-8"?>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/Theme.AppCompat.Light" >
    <activity
        android:name=".ClientSocket"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service
        android:name=".MyService"
        android:enabled="true"
        android:exported="true" >
    </service>
</application>

我只是在测试,当我尝试停止服务时,函数 onDestroy() 被调用但没有停止服务。 service/android:exported 和 android:enable 都是正确的。

有什么帮助吗?

Timer 实例创建后台线程。如果你想完全停止服务,你还需要停止线程。 Timer 有一个 cancel() 方法,您可以在 onDestroy() 方法中调用它来终止 Timer。

此外,导出服务意味着其他应用程序(进程)可以访问它。你很可能不需要那个。

代码中的问题是对什么的误解

@Override
public boolean stopService(Intent name) {
    timer.cancel();
    task.cancel();
    return super.stopService(name);

}

确实如此。

服务停止时调用的不是回调。相反,它覆盖 Context 上的 stopService 方法,您 调用 来停止服务(就像您在示例中的 Activity 中所做的那样) .

事实上,您的代码中 stopService 的覆盖根本没有效果。

timer.cancel();
task.cancel();

应该放在服务的onDestroy方法中。