Java Android服务需要始终运行(从不暂停或停止)
我创建了一项服务,并希望在手机重新启动或强制关闭之前始终运行此服务。该服务应在后台运行 已创建服务和启动服务的示例代码: 启动服务:Java Android服务需要始终运行(从不暂停或停止),java,android,service,Java,Android,Service,我创建了一项服务,并希望在手机重新启动或强制关闭之前始终运行此服务。该服务应在后台运行 已创建服务和启动服务的示例代码: 启动服务: Intent service = new Intent(getApplicationContext(), MyService.class); getApplicationContext().startService(service); public class MyService extends Service { @Override publ
Intent service = new Intent(getApplicationContext(), MyService.class);
getApplicationContext().startService(service);
public class MyService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO do something useful
HFLAG = true;
//smsHandler.sendEmptyMessageDelayed(DISPLAY_DATA, 1000);
return Service.START_NOT_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
// TODO for communication return IBinder implementation
return null;
}
}
<service
android:name=".MyService"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
</service>
服务:
Intent service = new Intent(getApplicationContext(), MyService.class);
getApplicationContext().startService(service);
public class MyService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO do something useful
HFLAG = true;
//smsHandler.sendEmptyMessageDelayed(DISPLAY_DATA, 1000);
return Service.START_NOT_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
// TODO for communication return IBinder implementation
return null;
}
}
<service
android:name=".MyService"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
</service>
清单声明:
Intent service = new Intent(getApplicationContext(), MyService.class);
getApplicationContext().startService(service);
public class MyService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO do something useful
HFLAG = true;
//smsHandler.sendEmptyMessageDelayed(DISPLAY_DATA, 1000);
return Service.START_NOT_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
// TODO for communication return IBinder implementation
return null;
}
}
<service
android:name=".MyService"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
</service>
是否可以像应用程序暂停或其他任何操作一样始终运行此服务。 一段时间后,我的应用程序会暂停,服务也会暂停或停止。
因此,如何在后台始终运行此服务。为了在自己的进程中启动服务,必须在xml声明中指定以下内容
<service
android:name="WordService"
android:process=":my_process"
android:icon="@drawable/icon"
android:label="@string/service_name"
>
</service>
在这里你可以找到一个对我非常有用的好教程
希望这有帮助您可以为该服务实施
startForeground
,即使它死机,您也可以使用startCommand()上的START\u STICKY
重新启动它。不确定这是否是正确的实现。是否可以像应用程序暂停和其他任何操作一样运行此服务
对
在服务onStartCommand方法中,返回START\u STICKY
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
使用startService(MyService)在后台启动服务,使其始终保持活动状态,而不管绑定的客户端数量如何
Intent intent = new Intent(this, PowerMeterService.class);
startService(intent);
创建活页夹
public class MyBinder extends Binder {
public MyService getService() {
return MyService.this;
}
}
定义服务连接
private ServiceConnection m_serviceConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
m_service = ((MyService.MyBinder)service).getService();
}
public void onServiceDisconnected(ComponentName className) {
m_service = null;
}
};
使用bindService绑定到服务
Intent intent = new Intent(this, MyService.class);
bindService(intent, m_serviceConnection, BIND_AUTO_CREATE);
对于您的服务,您可能希望在关闭相应的活动后发出通知以启动该活动
private void addNotification() {
// create the notification
Notification.Builder m_notificationBuilder = new Notification.Builder(this)
.setContentTitle(getText(R.string.service_name))
.setContentText(getResources().getText(R.string.service_status_monitor))
.setSmallIcon(R.drawable.notification_small_icon);
// create the pending intent and add to the notification
Intent intent = new Intent(this, MyService.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
m_notificationBuilder.setContentIntent(pendingIntent);
// send the notification
m_notificationManager.notify(NOTIFICATION_ID, m_notificationBuilder.build());
}
您需要修改清单,以单顶模式启动活动
android:launchMode="singleTop"
请注意,如果系统需要资源,而您的服务不是非常活跃,则可能会被终止。如果这是不可接受的,请使用startForeground将服务带到前台
startForeground(NOTIFICATION_ID, m_notificationBuilder.build());
一个简单的解决方案是在系统停止服务时重新启动服务
我发现这种方法的实现非常简单:
如果您已经有了一项服务,并且希望它一直工作,那么您需要添加两件事:
在服务本身中:
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
在舱单中:
android:launchMode="singleTop"
不需要添加绑定,除非您在服务中需要它。我找到了一种简单明了的方法,可以让服务始终运行
这家伙解释得很清楚,并且使用了一个好的算法。他的方法是在服务即将终止时发送广播,然后使用广播重新启动服务
您应该检查它:将此添加到清单中
<service
android:name=".YourServiceName"
android:enabled="true"
android:exported="false" />
我已经克服了这个问题,我的示例代码如下
在您的主要活动中添加以下行,这里BackGroundClass是服务类。您可以在New->JavaClass中创建此类(在此类中,添加您需要在后台执行的流程(任务))。为了方便起见,首先用通知铃声作为后台进程来表示它们
startService(new Intent(this, BackGroundClass .class));
在BackGroundClass中,只需包含我的编码,您就可以看到结果
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.widget.Toast;
public class BackgroundService extends Service {
private MediaPlayer player;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
player = MediaPlayer.create(this,Settings.System.DEFAULT_RINGTONE_URI);
player.setLooping(true);
player.start();
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
player.stop();
}
}
在AndroidManifest.xml中,尝试添加以下内容
<service android:name=".BackgroundService"/>
运行程序,只需打开应用程序,您可能会在后台发现通知警报。甚至,您可能会退出应用程序,但在关闭应用程序或卸载应用程序之前,您仍可能听到铃声警报。这表示通知警报处于后台进程。像这样,您可以添加一些进程作为背景
善意注意:请不要使用TOAST进行验证,因为即使在后台进程中,它也只会运行一次
startService(new Intent(this, BackGroundClass .class));
希望它能帮助 您不需要广播接收器。如果你愿意从上面的Stephen Donecker示例中复制一个api(serviceconnection)并粘贴到google中,你就会得到这个,如果不将其作为系统映像的一部分,这是不可能的。在某些情况下,Android可能会完全杀死你的应用程序。当您的应用程序被终止时,其服务将不活动。感谢您的回复,我如何使我的应用程序成为系统映像的一部分。您需要修改固件。如果这是可以接受的,我可以写一个答案。。如输入呼叫,请检查您的服务是否正在运行,如果没有,请重新启动。。。同样的方法使用媒体扫描软件,启动软件,输入短信。。。等等。你必须检查每次&每次你的服务正在运行或不只是像Watsup按摩服务。但我不会再要求你这么做。要了解更多详细信息,请检查:andandroid:name=“WordService”这对我的服务意味着什么?是的,你有自己的服务吗name@juan:你必须检查一下:@DhavalSodhaParmar这正是我的观点我不明白你在说什么mean@Juan:它表示您的服务在seprate线程中运行。这并不意味着你不能停止它。过了一段时间,这项服务会自动进入睡眠状态。这是一个非常好的答案——它工作得非常完美。其他许多类似问题的答案都是部分的,而这一个答案是完整的。你能详细说明一下#8吗?这是在哪里执行的?大约#5,这是在启动服务的地方执行的吗?我是否正确地理解您使用通知使服务始终保持活动状态?@Stephen Donecker为什么我必须绑定它?@SilviaHisham您没有。绑定通常在服务需要与活动通信时完成。在不允许在后台使用广播接收器的Android O中如何工作启动服务?没有上述代码不工作。.onstartcommand仅在我发布问题后调用此命令在不允许在后台使用广播接收器的Android O中如何工作开始服务的背景兄弟?