我创建了一个后台意图服务来更新firebase数据库中的数据。 当我的应用程序在前台时,数据会正确更新。但是当我的应用程序被杀死时,数据不会在firebase数据库中更新。
服务在清单文件中声明
<service
android:name=".service.MyIntentService"
android:enabled="true"
android:exported="true"></service>
Intent服务类在我的应用程序位于前台时正常工作,但在应用程序处于后台时却无法正常工作。
import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.softwebsolutions.datetime.DateTime;
import com.softwebsolutions.devicemanagement.bean.WifiStatus;
import com.softwebsolutions.devicemanagement.utils.Utility;
/**
* An {@link IntentService} subclass for handling asynchronous task requests in
* a service on a separate handler thread.
* <p>
* TODO: Customize class - update intent actions, extra parameters and static
* helper methods.
*/
public class MyIntentService extends IntentService {
// TODO: Rename actions, choose action names that describe tasks that this
// IntentService can perform, e.g. ACTION_FETCH_NEW_ITEMS
private static final String ACTION_FOO =
"com.softwebsolutions.devicemanagement.service.action.FOO";
private static final String ACTION_BAZ =
"com.softwebsolutions.devicemanagement.service.action.BAZ";
// TODO: Rename parameters
private static final String EXTRA_PARAM1 =
"com.softwebsolutions.devicemanagement.service.extra.PARAM1";
private static final String EXTRA_PARAM2 =
"com.softwebsolutions.devicemanagement.service.extra.PARAM2";
private static final String EXTRA_PARAM3 =
"com.softwebsolutions.devicemanagement.service.extra.PARAM3";
private static final String TAG = MyIntentService.class.getSimpleName();
public MyIntentService() {
super("MyIntentService");
}
// TODO: Customize helper method
public static void startActionFoo(Context context, String param1, String param2, String param3) {
Intent intent = new Intent(context, MyIntentService.class);
intent.setAction(ACTION_FOO);
intent.putExtra(EXTRA_PARAM1, param1);
intent.putExtra(EXTRA_PARAM2, param2);
intent.putExtra(EXTRA_PARAM3, param3);
context.startService(intent);
}
@Override protected void onHandleIntent(Intent intent) {
if (intent != null) {
final String action = intent.getAction();
final String wifiMac = intent.getStringExtra(EXTRA_PARAM1);
final String strSSID = intent.getStringExtra(EXTRA_PARAM2);
final String macAddress = intent.getStringExtra(EXTRA_PARAM3);
handleActionFoo(wifiMac, strSSID, macAddress, getApplicationContext());
}
}
private void handleActionFoo(final String wifiMac, final String strSSID,
final String macAddress, final Context context) {
Log.e(TAG, "onReceive.......service........");
DatabaseReference mDatabaseTmp =
FirebaseDatabase.getInstance().getReference("Android").child("wifiList").child(wifiMac);
mDatabaseTmp.addValueEventListener(new ValueEventListener() {
@Override public void onDataChange(DataSnapshot dataSnapshot) {
Log.e(TAG, "onReceive.......addValueEventListener");
if (dataSnapshot != null) {
Log.e(TAG, "onReceive.......dataSnapshot...NOT NULL");
String floorName = "Not detect";
if (dataSnapshot.getValue() != null) {
floorName = dataSnapshot.getValue().toString();
Log.e(TAG, "onReceive: ----------->" + floorName);
}
}
}
@Override public void onCancelled(DatabaseError databaseError) {
}
});
mDatabaseTmp.addListenerForSingleValueEvent(new ValueEventListener() {
@Override public void onDataChange(DataSnapshot dataSnapshot) {
Log.e(TAG, "onReceive.......dataSnapshot...");
if (dataSnapshot != null) {
Log.e(TAG, "onReceive.......dataSnapshot...NOT NULL");
String floorName = "Not detect";
if (dataSnapshot.getValue() != null) {
floorName = dataSnapshot.getValue().toString();
}
String currentDate =
DateTime.getInstance().getCurrentDateTime(" yyyy-MM-dd'T'HH:mm:ss.SSSS'Z'");
Log.e(TAG, "onReceive.......dataSnapshot..."
+ currentDate
+ " Floor Name -------->"
+ floorName);
String deviceId = Utility.getDeviceID(context);
WifiStatus wifiStatus = new WifiStatus();
wifiStatus.setDeviceId(deviceId);
wifiStatus.setName(strSSID);
wifiStatus.setMacAddress(macAddress);
wifiStatus.setDate(currentDate);
wifiStatus.setStatus(WifiStatus.STATUS_CONNECTED);
wifiStatus.setFloorName(floorName);
Utility.updateWifiStatus(context, wifiStatus);
} else {
Log.e(TAG, "onReceive.......dataSnapshot...NULL");
}
}
@Override public void onCancelled(DatabaseError databaseError) {
}
});
}
}
答案 0 :(得分:1)
IntentService只有在handleIntent()为下一个意图提供服务时才会保持活动状态,并且没有更多待处理的意图。您可以将每个意图视为&#34;命令&#34;到服务,它将运行只要它完成该命令。完成最后一个命令后,它会自行停止。因此,它通常不会持续运行很长时间。如果您希望IntentService能够长时间保持运行,那么您可能根本不想使用IntentService。
此外,如果应用处于前台(可见)或背景(不可见),Android服务并不关心。无论如何,它们都可以启动和停止。托管应用程序的过程可能会无限期地保持运行。
您还没有真正说明您尝试使用此服务所做的事情,因此无法说出您应该做什么。如果你希望一个监听器在服务启动&#34;时就处于活动状态,那么IntentService就不是正确的工具。您应该查看自定义实现。