POC - 在没有 Activity 的情况下在启动时启动服务 - Android 4+
POC - Start Service on boot without an Activity - Android 4+
这个问题看似微不足道,但我一直在苦苦思索。
我正在尝试在启动时启动服务,如果我从 mainActivity
至少启动一次(启动 activity ),例如:
AndroidManifest.xml
...
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
...
<activity
android:name="com.example.mainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.example.bootReceiver" android:enabled="true" android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
<service android:name="com.example.someService" android:enabled="true" android:exported="false"/>
...
bootReceiver.java
...
startService(new Intent(getApplicationContext(), com.example.someService.class));
...
mainActivity.java
...
startService(new Intent(getApplicationContext(), com.example.someService.class));
...
使用上面的代码,我可以在每次启动后 运行 服务,没有任何问题。
作为 POC,我正在尝试 在没有任何 activity 或至少没有mainActivity
,只需在 AndroidManifest.xml
上声明服务或创建与默认启动器 activity 同时启动的新 activity(不可见?)。
据我所知,出于安全原因,这在 android 3+(4+?)上是不可能的。
有什么办法可以实现吗?
当用户启动应用程序时,我可以从 AndroiManifest.xml
开始 2 个活动 吗?
如果我的问题不是 100% 清楚,我很抱歉,我已经尽力解释了,如果你不明白,请在下面发表评论。谢谢。
As far a I know, this isn't possible on android 3+ (4+?) due to security reasons.
Android 3.1,实际上是为了帮助防止偷渡式恶意软件。
Is there any way to achieve this ?
必须使用明确的 Intent
才能在您的组件之一上调用某种形式的 IPC,以便将应用程序移出所谓的 "stopped state" 阻止您接收播送。所以,有些东西需要:
- 通过明确的
Intent
或 开始您的一项活动
- 通过显式
Intent
或 启动您的一项服务
- 通过显式
Intent
向您的一位接收者发送广播
(我不知道尝试连接到 ContentProvider
是否有效,尽管可以说应该有效)
关键是显式 Intent
。这就是从主屏幕启动器调用 activity 的原因,因为用于启动 activity 的 Intent
将是一个显式的。
但是,如果没有这样的 activity,您将需要找到其他可以使用显式 Intent
来调用您的组件之一的东西。如果以及当用户通过系统设置应用程序激活该应用程序的功能时,某些专门服务(例如,输入法)可能会通过显式 Intent
被调用。如果您是某个其他应用程序的插件,则该其他应用程序可能会使用显式 Intent
来处理您的其中一个组件。您可以要求用户安装 Android SDK,了解如何使用命令行,并调用 adb shell am
命令来启动您的组件之一。这就是我能想到的全部。 None 完全是通用解决方案。
or by creating new activity (invisible?) that is launched at the same time as the default launcher activity
我不知道你认为这会实现什么。如果用户启动您的启动器 activity,您已经脱离停止状态,将正常接收广播。
这个问题看似微不足道,但我一直在苦苦思索。
我正在尝试在启动时启动服务,如果我从 mainActivity
至少启动一次(启动 activity ),例如:
AndroidManifest.xml
...
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
...
<activity
android:name="com.example.mainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.example.bootReceiver" android:enabled="true" android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
<service android:name="com.example.someService" android:enabled="true" android:exported="false"/>
...
bootReceiver.java
...
startService(new Intent(getApplicationContext(), com.example.someService.class));
...
mainActivity.java
...
startService(new Intent(getApplicationContext(), com.example.someService.class));
...
使用上面的代码,我可以在每次启动后 运行 服务,没有任何问题。
作为 POC,我正在尝试 在没有任何 activity 或至少没有mainActivity
,只需在 AndroidManifest.xml
上声明服务或创建与默认启动器 activity 同时启动的新 activity(不可见?)。
据我所知,出于安全原因,这在 android 3+(4+?)上是不可能的。
有什么办法可以实现吗?
当用户启动应用程序时,我可以从 AndroiManifest.xml
开始 2 个活动 吗?
如果我的问题不是 100% 清楚,我很抱歉,我已经尽力解释了,如果你不明白,请在下面发表评论。谢谢。
As far a I know, this isn't possible on android 3+ (4+?) due to security reasons.
Android 3.1,实际上是为了帮助防止偷渡式恶意软件。
Is there any way to achieve this ?
必须使用明确的 Intent
才能在您的组件之一上调用某种形式的 IPC,以便将应用程序移出所谓的 "stopped state" 阻止您接收播送。所以,有些东西需要:
- 通过明确的
Intent
或 开始您的一项活动
- 通过显式
Intent
或 启动您的一项服务
- 通过显式
Intent
向您的一位接收者发送广播
(我不知道尝试连接到 ContentProvider
是否有效,尽管可以说应该有效)
关键是显式 Intent
。这就是从主屏幕启动器调用 activity 的原因,因为用于启动 activity 的 Intent
将是一个显式的。
但是,如果没有这样的 activity,您将需要找到其他可以使用显式 Intent
来调用您的组件之一的东西。如果以及当用户通过系统设置应用程序激活该应用程序的功能时,某些专门服务(例如,输入法)可能会通过显式 Intent
被调用。如果您是某个其他应用程序的插件,则该其他应用程序可能会使用显式 Intent
来处理您的其中一个组件。您可以要求用户安装 Android SDK,了解如何使用命令行,并调用 adb shell am
命令来启动您的组件之一。这就是我能想到的全部。 None 完全是通用解决方案。
or by creating new activity (invisible?) that is launched at the same time as the default launcher activity
我不知道你认为这会实现什么。如果用户启动您的启动器 activity,您已经脱离停止状态,将正常接收广播。