【问题标题】:stop an Android service停止 Android 服务
【发布时间】:2017-03-27 14:51:14
【问题描述】:

我已经搜索并找到了与标题相似的问题,但到目前为止还没有解决方案。 我有以下服务

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

    private static final String TAG =
            "ServiceExample";

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(TAG, "Service onCreate");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        Log.i(TAG, "Service onStartCommand " + startId);

        final int currentId = startId;

        Runnable r = new Runnable() {
            public void run() {

                for (int i = 0; i < 3; i++)
                {
                    long endTime = System.currentTimeMillis() +
                            10*1000;

                    while (System.currentTimeMillis() < endTime) {
                        synchronized (this) {
                            try {
                                wait(endTime -
                                        System.currentTimeMillis());
                            } catch (Exception e) {
                            }
                        }
                    }
                    Log.i(TAG, "Service running " + currentId);
                }
                //stopSelf();
            }
        };

        Thread t = new Thread(r);
        t.start();
        return Service.START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.i(TAG, "Service onBind");
        return null;

    }

    @Override
    public void onDestroy() {
        Log.i(TAG, "Service onDestroy");
        super.onDestroy();
        //Log.i(TAG, "Service Destroyed");
    }

}

这是从我的活动中调用的,该活动有两个按钮:一个用于启动服务,一个用于停止服务

public class ServiceExampleActivity extends AppCompatActivity {

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

    public void buttonClick(View view)
    {
        Intent intent = new Intent(this, MyService.class);
        startService(intent);
    }

    public void stopClick(View view)
    {
        Intent intent = new Intent(this, MyService.class);
        stopService(intent);
    }

}

但是这不起作用。服务没有停止。 奇怪的是调用了 onDestroy 函数,但之后 runnable 仍在运行。 如何停止服务?

==============================================

编辑:我终于通过以下更改实现了它: 在 onDestroy 函数中

@Override
    public void onDestroy() {
        Log.i(TAG, "Service onDestroy");
      //  mHandler.removeCallbacks(r);
        t.interrupt();
        super.onDestroy();
        //Log.i(TAG, "Service Destroyed");
    }

线程 t 必须是类成员(在它在 onStartCommand 中声明之前),我将可运行对象修改为 this

r = new Runnable() {
            public void run() {

                for (int i = 0; i < 3; i++)
                {
                    long endTime = System.currentTimeMillis() +
                            10*1000;

                    while (System.currentTimeMillis() < endTime) {
                        synchronized (this) {
                            try {
                                wait(endTime -
                                        System.currentTimeMillis());
                            } catch (Exception e) {
                                return; //HERE to detect if the thread has been interrupted
                            }
                        }
                    }
                    Log.i(TAG, "Service running " + currentId);
                }
                stopSelf();//I have to use this otherwise it doesn't stop
            }
        };

我想知道为什么通常我必须使用 stopSelf 但被打断时就没有必要了。我想 stopSelf 只是调用 onDestroy

【问题讨论】:

    标签: android service


    【解决方案1】:

    停止服务只会杀死上下文。该框架不知道您可能已经启动的任何其他线程 - 您的工作是在 onDestroy() 中杀死它们。创建线程时保存它。在onDestroy(),打断它。并且在你线程的runnable中,定期检查线程是否被中断,如果是则返回。不要拨打Thread.stop(),因为这可能会使您的应用处于不安全状态。

    【讨论】:

    • 谢谢。我修改了问题,并根据您的建议进行了更改。它现在似乎可以工作了!
    猜你喜欢
    • 2014-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-14
    • 2018-11-11
    • 2011-11-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多