我的问题是。
在Android中,是否有一种方法可以创建一个服务,即使您重新启动手机,它也可以保持活动状态,直到完成其任务为止?
例如,闹钟应用程序。如果您重新启动手机,它将被触发而无任何问题。
在Android中,所有服务都是这样的吗?还是我们有其他解决方案?
请详细说明。
需要2个步骤:
(1) 创建BroadcastReceiver并在该接收器的onReceive()方法中启动服务:
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, YourService.class);
context.startService(service);
}
}
(2) 现在像这样在清单中声明此接收器:
<receiver android:name=".MyReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
这样,您的服务将始终运行,并且即使手机重新启动也会自动启动。有关更多详细信息,请参阅此链接:
http://www.vogella.com/articles/AndroidServices/article.html
有没有一种方法可以创建一个服务,即使您重新启动手机,它也能保持活动状态
没有。关闭手机时,所有正在运行的服务都会被终止。
例如闹钟应用程序。如果您重新启动手机,它将被触发而不会出现任何问题。
是的,但这不是因为服务保持活动状态。而是因为闹钟应用程序响应了android.intent.action.BOOT_COMPLETED
意图。
您可以创建一个BoradcastReceiver来响应此意图并启动您的服务。
问题在于用户可以手动终止此服务。如果要构建闹钟,则不应始终在后台运行服务。
您应该利用AlarmManager和PendingIntent。
类似于您的清单中的内容:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver
android:name=".broadcasts.InitReceiver"
android:exported="false" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.TIME_SET" />
<action android:name="android.intent.action.TIMEZONE_CHANGED" />
</intent-filter>
</receiver>
而对于广播,可以使用类似这样的内容。
public class InitReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Schedule your alarms again.
}
}
Intent intent = new Intent(context, Ring.class);
intent.setData(alarmUri);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, ringTime, pendingIntent );
Ring
是负责处理闹铃的广播。
请查看服务文档。您需要START_STICKY标志,如果由于低内存等原因终止了您的服务,则会使系统重新启动您的服务。
处理设备重启...
清单
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<service
android:name=".BootService"
android:label="Boot Service" >
<intent-filter>
<action android:name="com.my.package.BootService" />
</intent-filter>
</service>
<receiver
android:name=".receiver.BootReceiver"
android:enabled="true"
android:exported="true"
android:label="Boot Receiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
源代码
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(this.getClass().getName(), "Boot Receiver onReceive()");
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction()))
{
// Fire up your service.
Intent serviceIntent = new Intent("com.my.package.BootService");
serviceIntent.putExtra(Constants.BOOT_LAUNCH_EXTRA, true);
context.startService(serviceIntent);
}
}
}
public class BootService extends IntentService {
public BootService()
{
super("BootService");
}
@Override
protected void onHandleIntent(Intent intent) {
Log.d(this.getClass().getName(), "Boot Service onHandleIntent()");
if(intent.hasExtra(Constants.BOOT_LAUNCH_EXTRA))
{
//...
}
}
}