Android启动时的服务

我需要在启动时启动服务。 我搜查了很多。 他们在谈论Broadcastreceiver。 由于我是Android开发新手,我没有清楚的了解Android上的服务。 请提供一些源代码。

创build一个BroadcastReceiver并注册它以接收ACTION_BOOT_COMPLETED 。 您还需要RECEIVE_BOOT_COMPLETED权限。

阅读: 收听和广播全局消息,并设置警报

您的接收器:

 public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Intent myIntent = new Intent(context, YourService.class); context.startService(myIntent); } } 

你的AndroidManifest.xml:

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.broadcast.receiver.example" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true"> <activity android:name=".BR_Example" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!-- Declaring broadcast receiver for BOOT_COMPLETED event. --> <receiver android:name=".MyReceiver" android:enabled="true" android:exported="false"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"/> </intent-filter> </receiver> </application> <!-- Adding the permission --> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> </manifest> 

当设备启动时,可以注册自己的应用程序服务以自动启动。 例如,当你想从一个http服务器接收推送事件,并想要在发生新事件时立即通知用户,你需要这个。 用户不必在服务开始之前手动启动活动…

这很简单。 首先给你的应用权限RECEIVE_BOOT_COMPLETED。 接下来你需要注册一个BroadcastReveiver。 我们称之为BootCompletedIntentReceiver。

你的Manifest.xml现在应该是这样的:

 <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.jjoe64"> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <application> <receiver android:name=".BootCompletedIntentReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver> <service android:name=".BackgroundService"/> </application> </manifest> 

作为最后一步你必须实现接收器。 这个接收器只是开始你的后台服务。

 package com.jjoe64; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.preference.PreferenceManager; import com.jjoe64.BackgroundService; public class BootCompletedIntentReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) { Intent pushIntent = new Intent(context, BackgroundService.class); context.startService(pushIntent); } } } 

http://www.jjoe64.com/2011/06/autostart-service-on-device-boot.html

大多数这里发布的解决scheme都缺less一个重要部分:如果没有唤醒锁,就会导致服务在完成处理之前被杀死。 在另一个线程中看到这个解决scheme,在这里回答。

你需要先获得一个唤醒锁。 幸运的是, 支持库给了我们一个类来做到这一点:

 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" /> ... <service android:name=".SimpleWakefulReceiver"> <intent-filter> <action android:name="com.example.SimpleWakefulReceiver"/> </intent-filter> </service> 

您应该注册BOOT_COMPLETE以及REBOOT

 <receiver android:name=".Services.BootComplete"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"/> <action android:name="android.intent.action.REBOOT"/> </intent-filter> </receiver> 

还要在Manifest和使用许可中注册您创build的服务

 <application ...> <service android:name=".MyBroadcastReceiver"> <intent-filter> <action android:name="com.example.MyBroadcastReciver"/> </intent-filter> </service> </application> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> 

然后在接收方收到你的服务

 public class MyBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Intent myIntent = new Intent(context, MyService.class); context.startService(myIntent); } } 

首先在manifest.xml文件中注册一个接收者:

  <receiver android:name="com.mileagelog.service.Broadcast_PowerUp" > <intent-filter> <action android:name="android.intent.action.ACTION_POWER_CONNECTED" /> <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" /> </intent-filter> </receiver> 

然后为这个接收器写一个广播,如:

 public class Broadcast_PowerUp extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equals(Intent.ACTION_POWER_CONNECTED)) { Toast.makeText(context, "Service_PowerUp Started", Toast.LENGTH_LONG).show(); } else if (action.equals(Intent.ACTION_POWER_DISCONNECTED)) { Toast.makeText(context, "Service_PowerUp Stoped", Toast.LENGTH_LONG) .show(); } } }