android - 我可以在没有 Activity 或接收器的情况下启动服务吗?

标签 android service

我想在 APK 中启动服务。

我尝试使用如下:

<application android:icon="@drawable/icon" android:label="@string/app_name">  
        <service android:name =".TestServcie">  
            <intent-filter>  
                <action android:name="android.intent.action.MAIN" />  
                <category android:name="android.intent.category.LAUNCHER" />  
            </intent-filter>  
        </service>  
    </application>

有什么想法吗?
谢谢

最佳答案

你可以编写一个BroadcastReceiver 并在接收到Intent 后运行Service。例如在设备启动或您需要的其他 Intent 之后。

<receiver android:name=".StartupReceiver">
   <intent-filter>
     <action android:name="android.intent.action.BOOT_COMPLETED"/>
     <category android:name="android.intent.category.HOME"/>
   </intent-filter>
</receiver>

关于android - 我可以在没有 Activity 或接收器的情况下启动服务吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4468006/

相关文章:

android - 尝试在空对象上调用接口(interface)方法 'android.media.session.ISessionController android.media.session.ISession.getController()'

java - 服务被销毁时如何创建通知

Android - 使用 CommonWare 的 locpoller 时出现问题

AndroidPlot:从 1 到 11 的域标签

android - UnsatisfiedLinkError: 无法加载 cocos2dcpp: findLibrary 返回 null

android - 为什么 Android Studio 设计器显示我的自定义 View 嵌套在自身内部,而它不是

android - BaseLoaderCallback的Finish()方法不存在?

android - Camera2 api 错误无法创建捕获 session

java - 我是否需要为 java 中的一个函数创建单独的服务类

python - 来自 Python 服务的警报弹出窗口