【问题标题】:Android: Start Service on boot?Android:启动时启动服务?
【发布时间】:2011-05-21 08:20:25
【问题描述】:

我确实是 Java 新手,但我是一名经验丰富的 C# 编码器。

我创建了一个服务,我可以从一个活动开始/停止。 我的问题是,我如何“安装”这个服务,让它在我的设备启动时启动?

我发现了这个:

Trying to start a service on boot on Android

我试过这样实现:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="james.jamesspackage" android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application android:icon="@drawable/icon" android:label="@string/app_name"
    android:debuggable="true">
    <activity android:name=".jamessActivity" android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service android:enabled="true" android:name=".MyService">
        <intent-filter>
            <action android:name="james.jamesspackage.MyService" />
        </intent-filter>
        </service>
    <receiver android:name="james.jamesspackage.MyBroadcastReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <category android:name="android.intent.category.HOME" />
        </intent-filter>
    </receiver>
</application>
</manifest>

怎么了?我可以在一个清单中包含一个活动和一个服务/接收器吗?

谢谢

詹姆斯

【问题讨论】:

  • 安装是什么意思?您喜欢的帖子解释了在编译应用程序时需要添加到清单中的内容。

标签: android service boot


【解决方案1】:

如何在设备启动时启动服务(自动运行应用等)

首先:从 Android 3.1+ 版本开始,如果用户从未启动过您的应用程序至少一次或用户“强制关闭”应用程序,您不会收到 BOOT_COMPLETE。 这样做是为了防止恶意软件自动注册服务。此安全漏洞已在较新版本的 Android 中关闭。

解决方案:

创建带有活动的应用程序。当用户运行它一次应用程序可以收到 BOOT_COMPLETE 广播消息。

第二个:在挂载外部存储之前发送 BOOT_COMPLETE。如果应用安装到外部存储,它将不会收到 BOOT_COMPLETE 广播消息。

在这种情况下有两种解决方案:

  1. 将您的应用安装到内部存储中
  2. 在内部存储中安装另一个小应用程序。此应用收到 BOOT_COMPLETE 并在外部存储上运行第二个应用。

如果您的应用已经安装在内部存储中,那么下面的代码可以帮助您了解如何在设备启动时启动服务。


在 Manifest.xml 中

权限:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

注册您的 BOOT_COMPLETED 接收器:

<receiver android:name="org.yourapp.OnBoot">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
    </intent-filter>
</receiver>

注册您的服务:

<service android:name="org.yourapp.YourCoolService" />

在接收方 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);

    }

 }

对于 HTC,如果设备未捕获 RECEIVE_BOOT_COMPLETED,您可能还需要在清单中添加此代码:

<action android:name="android.intent.action.QUICKBOOT_POWERON" />

接收器现在看起来像这样:

<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>

如何在不重启模拟器或真机的情况下测试 BOOT_COMPLETED? 这简单。试试这个:

adb -s device-or-emulator-id shell am broadcast -a android.intent.action.BOOT_COMPLETED

如何获取设备ID?获取具有 id 的已连接设备列表:

adb devices

ADT 中的 adb 默认可以在以下位置找到:

adt-installation-dir/sdk/platform-tools

享受吧! )

【讨论】:

    【解决方案2】:

    看起来接收者部分中的名称有误。这是我在 AndroidManifest.xml 中的应用程序条目的样子:

       <application android:icon="@drawable/icon" android:label="@string/app_name">
    
        <receiver android:name=".BootListener" 
                 android:enabled="true" 
                 android:exported="false"
                 android:label="BootListener">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
    
           <service android:name=".UpdateService">
           </service>
    
           <activity android:name=".Info"
                     android:label="@string/app_name">
               <intent-filter>
                   <action android:name="android.intent.action.MAIN" />
                   <category android:name="android.intent.category.LAUNCHER" />
               </intent-filter>
           </activity>
    
        <activity android:name=".TravelMapperPreferences"
                  android:label="Settings">
        </activity>        
    
       </application>
    

    请注意,名称与清单声明中的包相关。您的接收者名称应为“.MyBroadcastReceiver”,因为清单的包包含 james.jamesspackage

    【讨论】:

      猜你喜欢
      • 2011-12-03
      • 1970-01-01
      • 2018-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多