start service at boot

I haven’t been messing around with the Android SDK for too long but I’ve already found some things that I think are worth sharing. Android has a concept of Services, or a process that can sit in the background and run a task without needing to interact with the user. There’s plenty of reasons why a Service might not need to be running all the time (say an alarm clock app with no alarms scheduled), but for the most part, Services need to be started at boot. Here’s how, tested from Android 1.5 to 2.2, since no other example I could find on the Internet was complete for this ever-changing SDK.

This example ServiceStartAtBootService, is a member of the package com.example.ssab (StartServiceAtBoot): Be careful with your Service implementation, as the onStart() method is depreciated in newer versions of the SDK!

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class StartAtBootService extends Service
{
public IBinder onBind(Intent intent)
{
return null;
}

@Override
public void onCreate()
{
Log.v("StartServiceAtBoot", "StartAtBootService Created");
}

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Log.v("StartServiceAtBoot", "StartAtBootService -- onStartCommand()");

// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}

/*
* In Android 2.0 and later, onStart() is depreciated. Use
* onStartCommand() instead, or compile against API Level 5 and
* use both.
* http://android-developers.blogspot.com/2010/02/service-api-changes-starting-with.html
@Override
public void onStart(Intent intent, int startId)
{
Log.v("StartServiceAtBoot", "StartAtBootService -- onStart()");
}
*/

@Override
public void onDestroy()
{
Log.v("StartServiceAtBoot", "StartAtBootService Destroyed");
}
}



Use any Service you already have instead of the above. Within application in your AndroidManifest.xml define your Servicewith an intent-filter:

<service android:name="StartAtBootService">
<intent-filter>
<action android:name="com.example.ssab.StartAtBootService">
</action>
</intent-filter>
</service>


Now the new part. The OS broadcasts ACTION_BOOT_COMPLETED when it has finished booting. Your app can ask to receive this notification by requesting permission in your manifest:

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


Your app now gets the broadcast, but still needs to do something with it. This is done by subclassing the BroadcastReceiver class. As far as I know, ACTION_BOOT_COMPLETED is the only Intent broadcast to apps, but because this could change at any point (and I can all but guarantee it will), do yourself a favor and check the Intent in your onReceive() method:


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class StartAtBootServiceReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Intent i = new Intent();
i.setAction("com.example.ssab.StartAtBootService");
context.startService(i);
}
}
}

The last thing you need to do is register your BroadcastReceiver in your manifest, within application:

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


That should do it. If you’re using Eclipse, run your app once and then exit the emulator. Then issue emulator -avd your_avd_name to launch your emulator without uninstalling your app. An adb logcat | grep StartAtBootService should show your app starting at boot.

As an additional info: BOOT_COMPLETE is sent to applications before external storage is mounted. So if application is installed to external storage it won't receive BOOT_COMPLETE broadcast message.

http://developer.android.com/guide/appendix/install-location.html


More details here in section Broadcast Receivers listening for "boot completed"

You can emulate all broadcast actions by connecting via adb to the device and open a device shell.

Here we go:

  • open console/terminal and navigating to /platform-tools
  • type "adb shell" or on linux/mac "./adb shell"
  • in the shell type "am broadcast -a android.intent.action.BOOT_COMPLETED" or whatever action you want to fire

There are a bunch of nice commands coming with adb or the adb shell. Just try it



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值