在后台永久运行服务-Android [英] Running a Service forever in background - Android
问题描述
我已经在Android应用程序中创建了 Service
,该服务会通过 BroadcastReceiver
在 BOOT_COMPLETE
上自动启动.那工作得很好.但是此服务仅执行一次在 onCreate()
方法中定义的任务.另一方面,我想永远在后台运行 Service
.实际上,在 onCreate()
方法内部,我正在从数据库中读取数据,并在需要时生成通知.而且通知可以随时生成,因此我想永远运行我的服务.我是Android的新手,我已经看到了一些示例&教程,但他们没有帮助.请回答我如何永远运行我的 Service
.
I have created a Service
in my android application which starts automatically on BOOT_COMPLETE
through BroadcastReceiver
. And That is working perfectly fine. But this service performs the task that I have define inside onCreate()
method, only once. On the Other hand I want to run the Service
forever in background. Actually inside onCreate()
method I am reading data from my database and I am generating notifications if required. And the notifications can be generated any time, so therefore I want to run my service forever. I am new to Android, and I have seen may examples & tutorial but they did not helped. So kindly Answer that how can I run my Service
forever.
这是服务代码:
package com.example.abc.project1;
import android.app.*;
import android.content.Intent;
import android.os.IBinder;
import android.util.*;
import org.json.*;
import java.io.*;
import java.net.*;
import java.util.*;
public class HelloService extends Service {
private static final String TAG = "HelloService";
public static boolean isRunning = false;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
isRunning = true;
new Thread(new Runnable() {
@Override
public void run() {
/*Here I want to do my task forever (reading from database & generate notifications)*/
}
}).start();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return Service.START_STICKY;
}
@Override
public void onDestroy() {
isRunning = false;
}
}
这是BroadcastReceiver的代码
package com.example.abc.project1;
import android.content.*;
import android.util.Log;
public class MyBroadcastreceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent startServiceIntent = new Intent(context, HelloService.class);
context.startService(startServiceIntent);
}
}
这是AndroidManifest.xml的一部分
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.abc.project1">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gef.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_CATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
<service android:name=".HelloService"></service>
<receiver android:name="com.example.abc.project1.MyBroadcastreceiver" android:enabled="true" android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<data android:scheme="package" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
</application>
</manifest>
推荐答案
这非常简单.
步骤:
1.创建一个服务类.
2.创建一个 BroadcastReceiver 类
3.使用 onDestroy 服务方法
呼叫 BroadReceiver 4.再次在 BroadReceiver 类的 onReceive 方法中启动服务.
引用此链接
It is very simple.
steps:
1.create a Service class.
2.create a BroadcastReceiver class
3.call BroadReceiver in onDestroy method of service
4.In onReceive method of BroadReceiver class start service once again.
refer this link
这篇关于在后台永久运行服务-Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!