cry*_*rys 4 android android-intent android-service
我正在寻找一种无需课程即可启动Android服务的方法.我找到了很多例子:
startService(new Intent(this, TheService.class));
Run Code Online (Sandbox Code Playgroud)
我不想把服务类放在其中,并且仍然运行服务,直到我手动停止它.
我不需要具有服务的接口,因为它只应该打开一些没有任何参数的套接字.
该服务运行时间不应该依赖于一个应用程序的运行时间:这就需要服务应该启动它,比它应该运行,直到它被手动停止了第一应用.
所以我不能用,BroadcastReceiver因为他们活得很短.绑定到服务将导致unbind停止服务(我不想要).
有没有办法在没有服务的.class的情况下使用启动服务?替代方法:有没有办法绑定到服务,它不会将生命周期链接到已启动的应用程序?
你可以按名称开始.像这样:
public static void startYourService(Context context) {
Intent i = new Intent("com.whatever.servicename.YOUR_ACTION_NAME");
i.setPackage(context.getPackageName());
context.startService(i);
}
Run Code Online (Sandbox Code Playgroud)
您需要使用该服务向apk添加intent过滤器:
<service
android:name="com.whatever.servicename"
android:exported="true">
<intent-filter>
<action android:name="com.whatever.servicename.YOUR_ACTION_NAME" />
</intent-filter>
</service>
Run Code Online (Sandbox Code Playgroud)