Ale*_*lex 327 android broadcastreceiver android-service
当设备在Android上启动时,我一直在尝试启动服务,但我无法让它工作.我在网上看了很多链接,但没有一个代码可以工作.我忘记了什么吗?
<receiver
android:name=".StartServiceAtBootReceiver"
android:enabled="true"
android:exported="false"
android:label="StartServiceAtBootReceiver" >
<intent-filter>
<action android:name="android.intent.action._BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service
android:name="com.test.RunService"
android:enabled="true" />
Run Code Online (Sandbox Code Playgroud)
public void onReceive(Context context, Intent intent) {
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Intent serviceLauncher = new Intent(context, RunService.class);
context.startService(serviceLauncher);
Log.v("TEST", "Service loaded at start");
}
}
Run Code Online (Sandbox Code Playgroud)
Tim*_*uck 600
其他答案看起来不错,但我想我会把所有内容都包含在一个完整的答案中.
您的AndroidManifest.xml文件中需要以下内容:
在你的<manifest>元素中:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Run Code Online (Sandbox Code Playgroud)在您的<application>元素中(确保为您使用完全限定的[或相对]类名称BroadcastReceiver):
<receiver android:name="com.example.MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
(你不需要的android:enabled,exported等等,属性:Android的默认值是正确的)
在MyBroadcastReceiver.java:
package com.example;
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent startServiceIntent = new Intent(context, MyService.class);
context.startService(startServiceIntent);
}
}
Run Code Online (Sandbox Code Playgroud)从原来的问题:
<receiver>元素是否在<application>元素中BroadcastReceiver指定<intent-filter>use*_*968 68
如何在设备启动时启动服务(自动运行应用程序等)
首先:从版本Android 3.1+开始,如果用户从未启动过您的应用程序至少一次或用户"强制关闭"应用程序,则不会收到BOOT_COMPLETE.这样做是为了防止恶意软件自动注册服务.此安全漏洞在较新版本的Android中关闭.
解:
使用活动创建应用.当用户运行它时,应用程序可以接收BOOT_COMPLETE广播消息.
第二个:在安装外部存储之前发送BOOT_COMPLETE.如果应用程序安装到外部存储,它将不会收到BOOT_COMPLETE广播消息.
在这种情况下有两种解决方案:
如果您的应用已安装在内部存储中,则下面的代码可以帮助您了解如何在设备启动时启动服务.
在Manifest.xml中
允许:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Run Code Online (Sandbox Code Playgroud)
注册您的BOOT_COMPLETED接收器:
<receiver android:name="org.yourapp.OnBoot">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
注册您的服务:
<service android:name="org.yourapp.YourCoolService" />
Run Code Online (Sandbox Code Playgroud)
在接收器OnBoot.java中:
public class OnBoot extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
// Create Intent
Intent serviceIntent = new Intent(context, YourCoolService.class);
// Start service
context.startService(serviceIntent);
}
}
Run Code Online (Sandbox Code Playgroud)
对于HTC,如果设备没有捕获RECEIVE_BOOT_COMPLETED,您可能还需要添加Manifest此代码:
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
Run Code Online (Sandbox Code Playgroud)
接收器现在看起来像这样:
<receiver android:name="org.yourapp.OnBoot">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
如何在没有重启模拟器或真实设备的情况下测试BOOT_COMPLETED?这很简单.试试这个:
adb -s device-or-emulator-id shell am broadcast -a android.intent.action.BOOT_COMPLETED
Run Code Online (Sandbox Code Playgroud)
如何获取设备ID?获取带有ID的已连接设备列表:
adb devices
Run Code Online (Sandbox Code Playgroud)
ADT中的adb默认情况下可以在以下位置找到:
adt-installation-dir/sdk/platform-tools
Run Code Online (Sandbox Code Playgroud)
请享用!)
Ton*_*ony 34
随着
<action android:name="android.intent.action.BOOT_COMPLETED" />
Run Code Online (Sandbox Code Playgroud)
也用,
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
Run Code Online (Sandbox Code Playgroud)
HTC设备似乎没有抓住BOOT_COMPLETED
Evg*_*man 20
请注意,在问题的开头,有一个拼写错误:
<action android:name="android.intent.action._BOOT_COMPLETED"/>
代替 :
<action android:name="android.intent.action.BOOT_COMPLETED"/>
一个小"_"和所有这些麻烦:)
Ome*_*ter 13
我刚刚发现它可能是因为>中的Fast Boot选项SettingsPower
当我关闭此选项时,我的应用程序会收到此广播,但不是.
顺便说一句,我Android 2.3.3的HTC Incredible S.
希望能帮助到你.
Ric*_*red 12
我认为您的清单需要添加:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Run Code Online (Sandbox Code Playgroud)
在尝试了所有提到的答案和技巧之后,我终于找到了为什么代码在我的手机中不起作用.某些Android手机如"Huawei Honor 3C Android 4.2.2 " 在其设置中有一个Statup Manager菜单,您的应用必须在列表中进行检查.:)
我有一个额外的<category>标签,不知道是否有任何区别.
<receiver android:name="BootIntentReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
您是否尝试过省略if-clause "android.intent.action.BOOT_COMPLETED".equals(intent.getAction(),因为接收器可能只接收到该意图?
小智 5
这就是我所做的
1. 我创建了 Receiver 类
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//whatever you want to do on boot
Intent serviceIntent = new Intent(context, YourService.class);
context.startService(serviceIntent);
}
}
Run Code Online (Sandbox Code Playgroud)
2.在清单中
<manifest...>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application...>
<receiver android:name=".BootReceiver" android:enabled="true" android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
...
Run Code Online (Sandbox Code Playgroud)
3.并且毕竟你需要在你的MainActivity中“设置”接收器,它可能在onCreate里面
...
final ComponentName onBootReceiver = new ComponentName(getApplication().getPackageName(), BootReceiver.class.getName());
if(getPackageManager().getComponentEnabledSetting(onBootReceiver) != PackageManager.COMPONENT_ENABLED_STATE_ENABLED)
getPackageManager().setComponentEnabledSetting(onBootReceiver,PackageManager.COMPONENT_ENABLED_STATE_ENABLED,PackageManager.DONT_KILL_APP);
...
Run Code Online (Sandbox Code Playgroud)
我从 ApiDemos 学到的最后一步
| 归档时间: |
|
| 查看次数: |
185515 次 |
| 最近记录: |