Android的服务在后台没有运行? [英] Android Service not running in background?
问题描述
当我preSS home键或后退按钮,但是当应用程序被关闭的服务未在后台运行的服务运行。此外,该服务在某些(LG的Nexus 5)手机后台运行,但在大多数手机(三星,Xioami)服务没有运行,当应用程序被关闭。当我转到设置>应用程序>运行,它总是显示为应用程序的运行和显示1服务1线程。我打电话从MainActivity.java服务
我要的服务始终在后台运行,即使应用程序被关闭。任何帮助将AP preciated。
下面是code服务TimeService.java
公共类TimeService扩展服务实现
ConnectionCallbacks,OnConnectionFailedListener {
// 不变
公共静态最后长NOTIFY_INTERVAL = 30 * 60 * 1000; // 30分钟//另一个线程,以避免崩溃运行
私人处理程序mHandler =新的处理程序();
//定时器处理
私人定时器mTimer = NULL;@覆盖
公共无效的onCreate(){
//如果取消已经存在
如果(mTimer!= NULL){
mTimer.cancel();
}其他{
//重新创建新
mTimer =新的Timer();
}
//计划任务
mTimer.scheduleAtFixedRate(新TimeDisplayTimerTask(),0,NOTIFY_INTERVAL);
} @覆盖
公众的IBinder onBind(意向意图){
返回null;
}
类TimeDisplayTimerTask扩展的TimerTask {
@覆盖
公共无效的run(){
//另一个线程上运行
mHandler.post(新的Runnable(){ @覆盖
公共无效的run(){
//在后台发送短信 sendSMS(数字,MSG) }
});
}
}
MainActivity.java
公共类MainActivity扩展活动
{
按钮btnIn;
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
btnIn =(按钮)findViewById(R.id.btnIn); btnIn.setOnClickListener(新View.OnClickListener(){ 布尔启用= TRUE;
@覆盖
公共无效的onClick(视图v){ 意向意图=新意图(MainActivity.this,TimeService.class);
startService(意向);
}
}
}
Android清单
<服务机器人:名字=。TimeService/>
我正在运行在单独的线程后台任务,而应该是在主线程中,这是原因应用程序不是在后台的一些手机上运行
类TimeDisplayTimerTask扩展的TimerTask {
@覆盖
公共无效的run(){
//在后台发送短信
sendSMS(数字,味精);
}
}
The service runs when I press home button or back button but the service is not running in background when app is closed. Moreover the service runs in background in some(LG Nexus 5) phones, but in most of the phones(Samsung,Xioami) service is not running when app is closed. When i goto Settings > Apps > Running, It always shows as app running and displays 1 service 1 thread. I am calling the service from MainActivity.java
I want the service always running in background even if the app is closed. Any help will be appreciated.
Here is code for service TimeService.java
public class TimeService extends Service implements
ConnectionCallbacks, OnConnectionFailedListener {
// constant
public static final long NOTIFY_INTERVAL = 30 * 60 * 1000; // 30 minutes
// run on another Thread to avoid crash
private Handler mHandler = new Handler();
// timer handling
private Timer mTimer = null;
@Override
public void onCreate() {
// cancel if already existed
if (mTimer != null) {
mTimer.cancel();
} else {
// recreate new
mTimer = new Timer();
}
// schedule task
mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(), 0, NOTIFY_INTERVAL);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
class TimeDisplayTimerTask extends TimerTask {
@Override
public void run() {
// run on another thread
mHandler.post(new Runnable() {
@Override
public void run() {
// Send message in background
sendSMS(number,msg)
}
});
}
}
MainActivity.java
public class MainActivity extends Activity
{
Button btnIn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnIn = (Button) findViewById(R.id.btnIn);
btnIn.setOnClickListener(new View.OnClickListener() {
boolean enable = true;
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, TimeService.class);
startService(intent);
}
}
}
Android manifest
<service android:name=".TimeService" />
I was running the background task in a separate thread, while it should be in the main thread, that was the reason app was not running in background in some Phones.
class TimeDisplayTimerTask extends TimerTask {
@Override
public void run() {
// Send message in background
sendSMS(number,msg);
}
}
这篇关于Android的服务在后台没有运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!