Java Android服务在应用程序重新打开和关闭时停止
我试图了解android服务是如何工作的 因此,我创建了一个带有活动和服务的简单应用程序,代码如下: MainActivity.xml:Java Android服务在应用程序重新打开和关闭时停止,java,android,service,Java,Android,Service,我试图了解android服务是如何工作的 因此,我创建了一个带有活动和服务的简单应用程序,代码如下: MainActivity.xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" andr
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Service"
android:id="@+id/buttonStart"
android:layout_marginTop="60dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="startButtonClickHandler"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop Service"
android:id="@+id/buttonStop"
android:layout_below="@+id/buttonStart"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:enabled="false"
android:onClick="stopButtonClickHandler" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/textView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:text="Counter is: 0" />
</RelativeLayout>
ExampleService.java
package com.example.wellsaid.provaservice;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.os.SystemClock;
import android.util.Log;
public class ExampleService extends Service {
/*--------------------- CODICE NUOVO-------------------------------*/
final static String RETURN_COUNTER = "RETURN_COUNTER";
/*----------------------------------------------------------------*/
@Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
Intent intent = new Intent(this, ExampleService.class);
intent.putExtra("count",thread.getCounter());
startService(intent);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
ExampleThread thread = null;
private static boolean running = false;
public static boolean isRunning() { return running; }
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
running = true;
if(thread == null) {
int counter = intent.getExtras().getInt("count");
thread = new ExampleThread(counter);
thread.start();
}
else{
/*-----------------------CODICE NUOVO ----------------------*/
Intent send = new Intent();
send.setAction(RETURN_COUNTER);
send.putExtra("count",thread.getCounter());
sendBroadcast(send);
/*----------------------------------------------------------*/
}
return startId;
}
@Override
public void onDestroy() {
running = false;
thread.kill();
super.onDestroy();
}
private class ExampleThread extends Thread {
boolean stopped;
int counter = 0;
public ExampleThread(int counter){
this.counter = counter;
}
public void start(){
stopped = false;
super.start();
}
public int getCounter(){ return counter; }
public void kill(){
stopped = true;
}
public void run(){
while(!stopped) {
try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); }
counter++;
Log.i("Service", "Counter is: " + counter);
}
}
}
}
这就是我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.wellsaid.provaservice" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".ExampleService" />
</application>
</manifest>
我尝试过:1)打开应用程序,用开始按钮启动服务,用另一个按钮停止服务。全部正常2)打开应用程序,启动服务并关闭应用程序。和所有OK(服务仍在运行)3)打开应用程序,启动服务,重新打开应用程序并获取计数器值。全部正常4)打开应用程序,启动服务,关闭应用程序,重新打开应用程序并停止服务。全部正常(关闭后重新启动服务)
当我打开、启动服务、关闭应用程序、重新打开以检查计数器编号,然后再次关闭应用程序时,问题就会出现。一段时间后,服务停止,logcat中没有消息:(
在应用程序管理中的android设备上,我可以看到我的应用程序处于重新启动状态,在很长一段时间后,它会重新启动,然后再次停止,继续。。。。
我已经在三星galaxy express股票(android 4.1.2)和三星galaxy tab 2 10.1上测试了cyanogenmod 10.1.3(android 4.2.2)您通读了吗
注意以下几点:
对于已启动的服务,有两种附加的主要模式
它们可以决定运行的操作,具体取决于它们的值
从onStartCommand()返回:START\u STICKY用于
根据需要显式启动和停止,而启动\u不\u
或START\u REDELIVER\u意图用于只应
在处理发送给它们的任何命令时保持运行
从开始
文档:
从onStartCommand返回的常量(Intent,int,int):如果
服务的进程在启动时(从返回后)被终止
onStartCommand(Intent,int,int)),然后将其保持在启动状态
但不要保留此交付意图。稍后系统将尝试
重新创建服务。因为它处于已启动状态,它将
保证在创建
新服务实例;如果没有任何挂起的启动命令
如果已交付到服务,则将以空意图调用它
对象,因此您必须注意检查此项
这种模式对于将被显式启动和启动的事物是有意义的
停止以运行任意时间段,如服务
播放背景音乐
您好,谢谢您的回复。是的,我已经阅读了该页面,但是返回START\u STICKY对我没有帮助,现在我正在尝试返回START\u STICKY并在if(thread==null)上添加startForeground语句例如,构造函数中的块服务类似乎可以工作:D我已经完成了在主主题中编写的所有测试,它仍然在运行,没有错误。谢谢:)我很高兴您能够让它工作。如果我的回答对你有任何帮助,你可以接受。否则,为了让一个问题有一个被接受的答案,请在另一个答案中详细说明你做了什么并接受它。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.wellsaid.provaservice" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".ExampleService" />
</application>
</manifest>