Android – 开机启动服务

从Stack Exchange和其他地方所见过的所有东西,我已经正确设置了一切,以便在Android OS引导时启动IntentService。 不幸的是,它不是开机启动,我没有得到任何错误。 也许专家可以帮助…

performance:

<?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> 

BroadcastReceiver启动:

 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); } } 

更新 :我尝试了几乎所有的build议,我添加了日志,如Log.v("BatteryLogger", "Got to onReceive, about to start service"); 到StartupIntentReceiver的onReceive处理程序,并没有任何logging。 所以它甚至没有到达BroadcastReceiver。

我想我正在部署APK并正确testing,只需在Eclipse中运行debugging,并且控制台表示已成功将其安装到位于\ BatteryLogger \ bin \ BatteryLogger.apk的我的Xoom平板电脑上。 然后进行testing,我重新启动平板电脑,然后查看DDMS中的日志,并在操作系统设置中检查运行服务。 这一切听起来是正确的,还是我错过了什么? 再次,任何帮助非常感谢。

那么这里是一个完整的AutoStart应用程序示例

AndroidManifest文件

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="pack.saltriver" android:versionCode="1" android:versionName="1.0" android:permission="android.permission.RECEIVE_BOOT_COMPLETED"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <receiver android:name=".autostart"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver> <activity android:name=".hello"></activity> <service android:enabled="true" android:name=".service" /> </application> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission> </manifest> 

autostart.java

 public class autostart extends BroadcastReceiver { public void onReceive(Context context, Intent arg1) { Intent intent = new Intent(context,service.class); context.startService(intent); Log.i("Autostart", "started"); } } 

service.java

 public class service extends Service { private static final String TAG = "MyService"; @Override public IBinder onBind(Intent intent) { return null; } public void onDestroy() { Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show(); Log.d(TAG, "onDestroy"); } @Override public void onStart(Intent intent, int startid) { Intent intents = new Intent(getBaseContext(),hello.class); intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intents); Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show(); Log.d(TAG, "onStart"); } } 

hello.java – 执行应用程序后,每次启动设备时都会popup。

 public class hello extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Toast.makeText(getBaseContext(), "Hello........", Toast.LENGTH_LONG).show(); } } 

以下应该工作。 我已经validation。 可能是你的问题在别的地方。

 public class MyReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { Log.d("TAG", "MyReceiver"); Intent serviceIntent = new Intent(context, Test1Service.class); context.startService(serviceIntent); } } public class Test1Service extends Service { /** Called when the activity is first created. */ @Override public void onCreate() { super.onCreate(); Log.d("TAG", "Service created."); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d("TAG", "Service started."); return super.onStartCommand(intent, flags, startId); } @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); Log.d("TAG", "Service started."); } @Override public IBinder onBind(Intent arg0) { return null; } } <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.test" android:versionCode="1" android:versionName="1.0" android:installLocation="internalOnly"> <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <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" /> <!-- <activity android:name=".MyActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER"></category> </intent-filter> </activity> --> <service android:name=".Test1Service" android:label="@string/app_name" > </service> <receiver android:name=".MyReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver> </application> </manifest> 

由于设备在启动后进入睡眠状态,您的服务可能会在完成之前closures。 你需要先获得一个唤醒锁。 幸运的是, 支持库给了我们一个类来做到这一点:

 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); } } 

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

  @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" /> 

看起来与我的非常相似,但我使用接收器的完整包名称:

 <receiver android:name=".StartupIntentReceiver"> 

我有:

 <receiver android:name="com.your.package.AutoStart"> 

如果没有完整的软件包,我已经取得了成功,你知道呼叫链被中断吗? 如果您使用Log()debugging,那么它在什么时候不再起作用?

我认为这可能是在您的IntentService,这一切都看起来不错。