2012-10-26 45 views
3

我的问题是。安卓服务生活时间

在Android中有没有方法来创建一个服务,可以生存,甚至在重新启动手机,直到它不执行其任务,

例如报警应用。如果你重新启动你的手机,它将被触发,没有任何问题。

在android中所有的服务都是这样的,或者我们有一些解决方案吗?

请详细解释。

+0

有开机发送广播。 – njzk2

回答

0

查看Services documentation。您希望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)) 
    { 
       //... 
    } 
} 

} 
+1

这不包括“重新启动手机”。 – CommonsWare

+0

...渐渐到该位:) –

+0

http://stackoverflow.com/questions/13094926/android-services-life-time-continuation – abidkhan303

2

这将需要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)现在decalre这个接收器清单是这样的:

<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

+0

您还需要添加<使用许可权的android:NAME =“机器人。 permission.RECEIVE_BOOT_COMPLETED“/>到您的清单。 –

+0

哦,从技术上讲,它启动了BroadcastReceiver,而不是服务。 BroadcastReceiver需要构建一个Intent来启动实际的服务。 –

+0

问题仍然存在,我已经添加了新的岗位与我problem.http://stackoverflow.com/questions/13094926/android-services-life-time-continuation – abidkhan303

0

在Android是有办法创造出可以留你重启手机还活着,甚至,直到它不执行其任务的服务,

不,如果我们按字面解释您的请求。当您重新启动设备时,所有进程都会终止,就像您关闭设备时所有进程都被终止一样。

例如报警应用。如果你重新启动你的手机,它将被触发,没有任何问题。

这是因为“报警程序”越来越在启动时控制,通过ACTION_BOOT_COMPLETEDBroadcastReceiver,并重新安排其与报警AlarmManager

类似地,执行下载队列的服务将在启动时通过ACTION_BOOT_COMPLETEDBroadcastReceiver获得控制权,届时它将确定还需要下载并返回到该工作的内容。

2

是有办法创造出能够赖以生存的业务,即使你 重启手机

号关闭电话时,所有正在运行的服务被杀害。

例如报警应用。如果你重新启动你的手机,将会触发 没有任何问题。

是的,但这不是因为服务还活着。这是因为闹钟应用程序响应了android.intent.action.BOOT_COMPLETED意图。

你可以做的是创建响应这个意图,并且开始为您服务BoradcastReceiver。

的问题是,用户可以手动杀该服务。如果你想建立一个闹钟,你应该不是有一个服务总是在后台运行。

你应该利用AlarmManagerPendingIntent的。

喜欢的东西在你的清单:

<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是处理报警环广播。

+0

问题仍然存在,我已经添加了新的岗位与我problem.http ://stackoverflow.com/questions/13094926/android-services-life-time-continuation – abidkhan303