Android: Start Service on boot?

有些话、适合烂在心里 提交于 2019-12-23 03:44:09

问题


I'm reaaaaally new to Java, but an experienced C#-coder.

I've created a service which I can start/stop from an activity. My question is, how do I "install" this service so it does start upon boot of my device?

I found this:

Trying to start a service on boot on Android

I've tried to implemented this like this:

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

What's wrong? Can I have an activity and a service/receiver in one manifest?

Thanks

James


回答1:


How to start service on device boot(autorun app, etc.)

For first: since version Android 3.1+ you don't recieve BOOT_COMPLETE if user never started yor app at least once or user "force closed" application. This was done to prevent malware automaticaly register service. This security hole was closed in newer versions of Android.

Solution:

Create app with activity. When user run it once app can recieve BOOT_COMPLETE broadcast message.

For second: BOOT_COMPLETE is sent before external storage is mounted. if app is installed to external storage it won't receive BOOT_COMPLETE broadcast message.

In this case there is two solution:

  1. Install your app to internal storage
  2. Instal another small app in internal storage. This app recieves BOOT_COMPLETE and run second app on external storage.

If your app already installed in internal storage then code below can help you understand how to start service on device boot.


In Manifest.xml

Permission:

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

Register your BOOT_COMPLETED reciever:

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

Register your service:

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

In reciever 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);

    }

 }

For HTC you maybe need also add in Manifest this code if device don't catch RECEIVE_BOOT_COMPLETED:

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

Reciever now look like this:

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

How to test BOOT_COMPLETED without restart emulator or real device? It's easy. Try this:

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

How to get device id? Get list of connected devices with id's:

adb devices

adb in ADT by default you can find in:

adt-installation-dir/sdk/platform-tools

Enjoy! )




回答2:


Looks like the name in the receiver section is wrong. This is what my application entry in the AndroidManifest.xml looks like:

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

Note that the names are relative to the package in the manifest declaration. Your receiver name should be ".MyBroadcastReceiver" since the package of the manifest contains james.jamesspackage



来源:https://stackoverflow.com/questions/6080515/android-start-service-on-boot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!