android-启动服务

android-启动服务

从我在StackExchange和其他地方看到的所有信息来看,我已经正确地设置了在Android操作系统启动时启动IntentService的所有内容。不幸的是,它没有在启动时启动,而且我没有收到任何错误。也许专家们能帮上忙.。

清单:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.phx.batterylogger"
  android:versionCode="1"
  android:versionName="1.0"
  android:installLocation="internalOnly"><uses-sdk android:minSdkVersion="8" />
  <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  <uses-permission android:name="android.permission.BATTERY_STATS" />
  <application android:icon="@drawable/icon" android:label="@string/app_name">
    <service android:name=".BatteryLogger"/>
    <receiver android:name=".StartupIntentReceiver">  
        <intent-filter>  
            <action android:name="android.intent.action.BOOT_COMPLETED" />  
        </intent-filter>  
    </receiver></application></manifest>

启动广播收发器:

package com.phx.batterylogger;import android.content.BroadcastReceiver;import android.content.Context;
import android.content.Intent;public class StartupIntentReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent serviceIntent = new Intent(context, BatteryLogger.class);
        context.startService(serviceIntent);
    }}

更新:我尝试了以下所有建议,并添加了日志,例如Log.v("BatteryLogger", "Got to onReceive, about to start service");到StartupIntentRec信机的onRecept处理程序,任何记录都不会被记录。所以它甚至没有到达广播收发器。

我认为我正在正确部署APK并进行测试,只在Eclipse中运行Debug,控制台说它成功地将它安装到我的Xoom平板电脑\BatteryLogger\bin\BatteryLogger.apk上。然后进行测试,重新启动平板电脑,然后查看DDMS中的日志并检查OS设置中正在运行的服务。这听起来是对的,还是我遗漏了什么?再一次,任何帮助都是非常感谢的。


慕标5832272
浏览 358回答 3
3回答

湖上湖

您的服务可能会在它完成之前被关闭,因为设备在启动后将进入休眠状态。你需要先获得一个唤醒锁。幸运的是,支持库为我们提供了一个类。要做到这一点:public&nbsp;class&nbsp;SimpleWakefulReceiver&nbsp;extends&nbsp;WakefulBroadcastReceiver&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;@Override &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;onReceive(Context&nbsp;context,&nbsp;Intent&nbsp;intent)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;This&nbsp;is&nbsp;the&nbsp;Intent&nbsp;to&nbsp;deliver&nbsp;to&nbsp;our&nbsp;service. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Intent&nbsp;service&nbsp;=&nbsp;new&nbsp;Intent(context,&nbsp;SimpleWakefulService.class); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Start&nbsp;the&nbsp;service,&nbsp;keeping&nbsp;the&nbsp;device&nbsp;awake&nbsp;while&nbsp;it&nbsp;is&nbsp;launching. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Log.i("SimpleWakefulReceiver",&nbsp;"Starting&nbsp;service&nbsp;@&nbsp;"&nbsp;+&nbsp;SystemClock.elapsedRealtime()); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;startWakefulService(context,&nbsp;service); &nbsp;&nbsp;&nbsp;&nbsp;}}然后,在您的服务中,确保释放唤醒锁:&nbsp;&nbsp;&nbsp;&nbsp;@Override &nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;void&nbsp;onHandleIntent(Intent&nbsp;intent)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;At&nbsp;this&nbsp;point&nbsp;SimpleWakefulReceiver&nbsp;is&nbsp;still&nbsp;holding&nbsp;a&nbsp;wake&nbsp;lock &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;for&nbsp;us.&nbsp;&nbsp;We&nbsp;can&nbsp;do&nbsp;whatever&nbsp;we&nbsp;need&nbsp;to&nbsp;here&nbsp;and&nbsp;then&nbsp;tell&nbsp;it&nbsp;that &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;it&nbsp;can&nbsp;release&nbsp;the&nbsp;wakelock.... &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Log.i("SimpleWakefulReceiver",&nbsp;"Completed&nbsp;service&nbsp;@&nbsp;"&nbsp;+&nbsp;SystemClock.elapsedRealtime()); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SimpleWakefulReceiver.completeWakefulIntent(intent); &nbsp;&nbsp;&nbsp;&nbsp;}不要忘记添加Wake_LOCK权限:<uses-permission&nbsp;android:name="android.permission.RECEIVE_BOOT_COMPLETED"&nbsp;/> <uses-permission&nbsp;android:name="android.permission.WAKE_LOCK"&nbsp;/>
打开App,查看更多内容
随时随地看视频慕课网APP