Android 如何自行停止服务?

Android 如何自行停止服务?,android,Android,我在活动中启动一个服务,然后我希望该服务在一段时间后自行停止 我在服务中调用了stopSelf(),但它不起作用 如何让服务自行停止?如果“不工作”意味着进程没有被终止,那么android就是这样工作的。System.exit(0)或Process.killProcess(Process.myPid())将终止您的进程。但这不是安卓的做事方式 HTH通过说“不工作”,我猜您的意思是服务的ondestory()-方法没有被调用 我也遇到了同样的问题,因为我使用了这个标志来访问服务本身。 这会使服务

我在活动中启动一个服务,然后我希望该服务在一段时间后自行停止

我在服务中调用了stopSelf(),但它不起作用

如何让服务自行停止?

如果“不工作”意味着进程没有被终止,那么android就是这样工作的。
System.exit(0)
Process.killProcess(Process.myPid())
将终止您的进程。但这不是安卓的做事方式

HTH

通过说“不工作”,我猜您的意思是服务的
ondestory()
-方法没有被调用

我也遇到了同样的问题,因为我使用了这个标志来访问服务本身。 这会使服务保持活动状态,直到每个连接都被激活

一旦我更改为不使用标志(零),我就可以自行终止服务(
stopSelf()

示例代码:

final Context appContext = context.getApplicationContext();
final Intent intent = new Intent(appContext, MusicService.class);
appContext.startService(intent);
ServiceConnection connection = new ServiceConnection() {
  // ...
};
appContext.bindService(intent, connection, 0);
终止服务(而不是进程):


希望对您有所帮助。

由于您没有发布代码,我无法确切知道您正在执行的操作,但您必须声明您正在停止的操作:

this.stopSelf();
例如:

public class BatchUploadGpsData extends Service {
    @Override
    public void onCreate() {
        Log.d("testingStopSelf", "here i am, rockin like a hurricane.   onCreate service");
        this.stopSelf();
    }
通过调用
stopSelf()
,服务将停止

请确保没有线程在后台运行,这会让您感觉服务没有停止

在线程中添加打印语句


希望这有帮助。

我刚刚遇到了同样的问题。在我的例子中,我有一个用于与服务通信的单例服务管理器。在manager中,服务的启动方式如下:

context.bindService(new Intent(context, MyService.class), serviceConnection, Context.BIND_AUTO_CREATE); 
通过按照Alik Elzin的建议删除Context.BIND_AUTO_CREATE,我可以停止使用此.stopSelf()的服务,并在执行此操作时调用onDestroy()。这个问题是,在此之后,我无法使用上面的命令从管理器重新启动服务

最后,我通过使用来自服务的回调来修复这个问题,该回调告诉管理器停止服务。这样,在启动/停止服务时,经理总是负责,一切似乎都很好。我不知道这样做是否有反作用

代码非常简单。在服务中创建回调并在管理器中进行设置,如在连接类中所示:

private ServiceConnection mServiceConnection = new ServiceConnection() {
    public void onServiceConnected(ComponentName className, IBinder service) {
        myService = ((MyService.LocalBinder)service).getService();

        myService.setCallback(new MyService.MyServiceCallback() {
            @Override
            public void onStop() {
                stopService();
            }
        });
    }

    public void onServiceDisconnected(ComponentName className) {
        myService = null;
    }
};
及停止服务:

public void stopService()
{
    if(mServiceConnection != null){
        try {
            mContext.unbindService(mServiceConnection);
        } catch (Exception e) {}
    }

     mContext.stopService(new Intent(mContext, BleDiscoveryService.class));
}

在该服务中,需要停止时只需调用myCallback.onStop()。

这里没有提到的另一个恶意攻击是抛出一个类似NPE的异常。有一天,我需要停止InputMethodService,而这项技术非常有用。

让您的服务自行停止。。创建一个
BroadcastReceiver
类。。在你的服务中,像这样给你的接受者打电话

   stopForeground(true);
            stopSelf();
在用

sendBroadcast(new Intent("MyReceiver"));
    <receiver
        android:name="MyReceiver"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="MyReceiver"/>
        </intent-filter>
    </receiver>
在广播接收机中

 public class MyReceiver extends BroadcastReceiver {

     @Override
     public void onReceive(Context context, Intent intent) {

         context.stopService(new Intent(context,NotificationService.class));
     }
 }
清单文件

sendBroadcast(new Intent("MyReceiver"));
    <receiver
        android:name="MyReceiver"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="MyReceiver"/>
        </intent-filter>
    </receiver>


如果您在服务中使用单独的
线程
,则在通过调用
stopSelf()
stopService()
停止服务后,
线程
将保持运行。如果你想停止
线程
你应该调用
线程
中的
线程.interrupted()
(如果
线程
已经处于睡眠状态,可能会导致
异常)

使用stopSelf()停止自身的服务

我知道这是一个老问题,但在我的情况下(浮动窗口即服务),我必须先删除视图,然后调用
stopSelf()


因为答案是“call stopSelf()”,所以您需要提供更多关于“它不起作用”的详细信息。需要更多像答案一样“不起作用”的内容吗?请随意标记接受:)@AlikElzin kilaka肇事逃逸不要选择最佳答案。例如,在ReP=500之前,请考虑限制提问。如果我错了,请纠正我,但如果没有“这个”工作,也不会停止SUBSELF()。如果可能,请测试。没必要,但为了澄清起见添加它也没什么坏处我总是忘了在清单中添加广播接收器