简体   繁体   English

经过固定时间后如何停止服务?

[英]How to stop my service after passing fixed amount of time?

At point A in my application I start my service and expect the service get closed from point B. However, there might be few scenarios that point B doesn't ask service to get closed. 在应用程序的A点,我启动了服务,并期望该服务从B点关闭。但是,在某些情况下,B点不会要求服务关闭。 In this case I want the service close itself after fixed amount of time. 在这种情况下,我希望服务在固定的时间后关闭。

I have written following code into my Service class and expect the service gets closed after 10 seconds from launch time (It will be 45min in the future but I don't want to stay that long for test). 我已将以下代码编写到我的Service类中,并期望该服务在启动后10秒钟后关闭(将来是45分钟,但我不想花那么长时间进行测试)。

public class ChatService extends Service implements ITCPConnection
{
    private static final int SERVICE_LIFE_TIME = 10 * 1000; // In millis

    private AlarmReceiver mAlarmReceiver;
    private AlarmManager alarmMgr;
    private PendingIntent alarmIntent;

    @Override
    public void onCreate()
    {
        super.onCreate();

        //
        mAlarmReceiver = new AlarmReceiver();
        registerReceiver(mAlarmReceiver, new IntentFilter());

        //
        Intent intent = new Intent(this, AlarmReceiver.class);
        alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
        alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmMgr.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + SERVICE_LIFE_TIME, alarmIntent);
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        Log.e(TAG, "onDestroy()");

        // Unregister receiver
        if (mAlarmReceiver != null)
        {
            unregisterReceiver(mAlarmReceiver);
        }

        disconnect();
    }

    public void disconnect()
    {
        // If the alarm has been set, cancel it.
        if (alarmMgr!= null)
        {
            alarmMgr.cancel(alarmIntent);
        }

        ...

        Log.e(TAG, "disconnect()");
    }


    /*****************
     * Alarm Receiver
     *****************/
    private static class AlarmReceiver extends BroadcastReceiver
    {
        @Override
        public void onReceive(Context context, Intent intent)
        {
            Log.e(TAG, "Stop service from AlarmReceiver");
            context.stopService(intent);
        }
    }
}

My problem is AlarmReceiver.onReceive() never gets called and therefore my service will be alive indefinitely. 我的问题是AlarmReceiver.onReceive()从未被调用,因此我的服务将无限期地存活。

What you are trying to do is to targeting a broadcast receiver explicitly. 您试图做的是明确地定位广播接收器。

According to this , it cannot be done over a dinamically created (ie not declared into the manifest) broadcast receiver, because the os would not know how to resolve it. 根据这个 ,就不能在dinamically创建(即不申报入清单)广播接收器完成的,因为OS不知道如何解决它。 To check if this is the root of the problem, you can go with the implicit way and set an action inside the intent and by filtering it in the IntentFilter. 要检查这是否是问题的根源,可以采用隐式方式,并在intent内设置一个操作,然后在IntentFilter中对其进行过滤。

Anyway, using the post delayed can be seen as a valid alternative, since you expect the service to be shut down naturally or still be around to intercept the delayed event. 无论如何,使用延迟发布可以被视为一种有效的选择,因为您希望服务自然关闭或仍然可以拦截延迟事件。

Another (unrelated) thing is that you are calling 另一件事(无关)是您在打电话

            context.stopService(intent);

by using the broadcast intent and not the intent that started the service. 通过使用广播意图而不是启动服务的意图。 You could simply call stopSelf() . 您可以简单地调用stopSelf()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM