我想在android应用程序中提供后台服务,即使应用程序关闭,后台服务也应该保持运行
我想在android应用程序中提供后台服务,即使应用程序处于关闭、终止或后台状态,后台服务也应始终保持运行。我目前有后台服务,当应用程序最小化时,后台服务运行良好,但当我关闭应用程序并终止应用程序时,后台服务不起作用。如果应用程序终止,则套接字将关闭 甚至我也使用了android隔离进程真的….我想在android应用程序中提供后台服务,即使应用程序关闭,后台服务也应该保持运行,android,background-service,Android,Background Service,我想在android应用程序中提供后台服务,即使应用程序处于关闭、终止或后台状态,后台服务也应始终保持运行。我目前有后台服务,当应用程序最小化时,后台服务运行良好,但当我关闭应用程序并终止应用程序时,后台服务不起作用。如果应用程序终止,则套接字将关闭 甚至我也使用了android隔离进程真的…. 我想让插座在任何情况下都可用 public class MyTestService extends IntentService { private ServerSocket serverSocket;
我想让插座在任何情况下都可用
public class MyTestService extends IntentService {
private ServerSocket serverSocket;
private Socket tempClientSocket;
Thread serverThread = null;
public static final int SERVER_PORT = 3001;
public MyTestService() {
// Used to name the worker thread, important only for debugging.
super("test-service");
}
@Override
public void onCreate() {
super.onCreate(); // if you override onCreate(), make sure to call super().
// If a Context object is needed, call getApplicationContext() here.
}
@Override
protected void onHandleIntent(Intent intent) {
Socket socket;
try {
serverSocket = new ServerSocket(SERVER_PORT);
} catch (IOException e) {
e.printStackTrace();
}
if (null != serverSocket) {
while (true) {
try {
socket = serverSocket.accept();
try {
BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String mess = input.readLine();
BufferedWriter bf = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
PrintWriter out = new PrintWriter(bf,true);
out.println("message recieve");
input.close();
bf.close();
serverSocket.close();
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, "asdsadasd")
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle("Title")
.setContentText(mess)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setAutoCancel(true);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(1, mBuilder.build());
} catch (IOException e) {
e.printStackTrace();
}
}
catch (Exception e){
}
}
}
}
}
我会看你到底想做什么
fun workRequest(){
val constraints=constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.SETREQUIRESERCHARGING(真)
.setRequiresDeviceIdle(真)
.build()
val工作者:工作请求=
PeriodicWorkRequestBuilder(6,时间单位:小时)
.setInputData(workRequestData)
.setConstraints(约束)
.build()
WorkManager.getInstance().enqueue(工作者)
}
您可以使用Android framework API中的作业调度器来安排任务或在后台工作。您还可以指定网络类型、要运行作业的时间段或其他几个参数。
创建一个类,该类扩展JobService
类并重写onStartJob和onStopJob方法
public class myJob extends JobService {
public boolean onStartJob(JobParameters jobParameters) {
}
public boolean onStopJob(JobParameters jobParameters) {
}
}
在OnStartJob
中编写要在后台运行的代码。
将此添加到用于配置jobservice应如何运行的JobInfo
JobInfo.Builder builder = new JobInfo.Builder(jobID, new ComponentName(context, Myjob.service));
在JobInfo.Builder`上,您可以包括调度条件。例如:
builder.setPeriodic(10000);
builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
创建Jobscheduler
的实例,并从上面传递JobInfo
数据。这将根据指定的调度参数调用JobService
类
JobScheduler jobScheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
jobScheduler.schedule(builder.build());
以下是关于jobscheduler的更多信息:如何以及在何处使用它,先生,因为我已经创建了一个服务,并在单独的线程中使用了基于套接字的侦听代码。我实际上想在后台运行一个服务,该服务接收来自套接字的数据,甚至应用程序终止。我已经更新了我的问题,请检查它,并在这种情况下告诉我如何实现我的目标我已经提供了我使用过的代码。我已经更新了我的问题。请检查它,并告诉我在这种情况下我是如何实现我的目标的。你的api版本是什么。。?它会低于API 26吗?
JobScheduler jobScheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
jobScheduler.schedule(builder.build());