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 關注
- 431 瀏覽
添加回答
舉報
0/150
提交
取消