启动服务时是否需要添加intent-filter?

Pen*_*m10 6 service android intentfilter

我正在按照教程设置一个服务,以便在启动时启动,其中最后一段代码是:

在AndroidManifest.xml中输入此服务作为

<service android:name="MyService">
<intent-filter>
<action
android:name="com.wissen.startatboot.MyService" />
</intent-filter>
</service>
Run Code Online (Sandbox Code Playgroud)

现在在BroadcastReceiver MyStartupIntentReceiver的onReceive方法中启动此服务

public void onReceive(Context context, Intent intent) {
    Intent serviceIntent = new Intent();
    serviceIntent.setAction("com.wissen.startatboot.MyService");
    context.startService(serviceIntent);

}
Run Code Online (Sandbox Code Playgroud)

如您所见,它使用了intent-filters,并在启动时添加了操作.我可以使用吗?

startService(new Intent(this, MyService.class));
Run Code Online (Sandbox Code Playgroud)

一个人与另一个人相比有什么优势?

Com*_*are 7

假设这一切都在一个应用程序中,您可以使用后一种形式(MyService.class).

一个人与另一个人相比有什么优势?

如果您希望第三方启动此服务,我会使用自定义操作字符串.