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); }
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-permission android:name="android.permission.WAKE_LOCK" />
- 3 回答
- 0 关注
- 399 浏览
添加回答
举报
0/150
提交
取消