为了账号安全,请及时绑定邮箱和手机立即绑定

android-启动服务

android-启动服务

慕标5832272 2019-12-18 18:13:52
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设置中正在运行的服务。这听起来是对的,还是我遗漏了什么?再一次,任何帮助都是非常感谢的。
查看完整描述

3 回答

?
湖上湖

TA贡献2003条经验 获得超2个赞

您的服务可能会在它完成之前被关闭,因为设备在启动后将进入休眠状态。你需要先获得一个唤醒锁。幸运的是,支持库为我们提供了一个类。要做到这一点:

public class SimpleWakefulReceiver extends WakefulBroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // This is the Intent to deliver to our service.
        Intent service = new Intent(context, SimpleWakefulService.class);

        // Start the service, keeping the device awake while it is launching.
        Log.i("SimpleWakefulReceiver", "Starting service @ " + SystemClock.elapsedRealtime());
        startWakefulService(context, service);
    }}

然后,在您的服务中,确保释放唤醒锁:

    @Override
    protected void onHandleIntent(Intent intent) {
        // At this point SimpleWakefulReceiver is still holding a wake lock
        // for us.  We can do whatever we need to here and then tell it that
        // it can release the wakelock....
        Log.i("SimpleWakefulReceiver", "Completed service @ " + SystemClock.elapsedRealtime());
        SimpleWakefulReceiver.completeWakefulIntent(intent);
    }

不要忘记添加Wake_LOCK权限:

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



查看完整回答
反对 回复 2019-12-19
  • 3 回答
  • 0 关注
  • 354 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信