我有一项服务,当用户更改他/她的位置时会发送通知。此服务工作正常,但当用户关闭应用程序时,问题就出现了。
即使应用程序已关闭,我如何才能使服务仍然存在?
我的服务是:
public class LocationService extends Service implements LocationListener {
public final static int MINUTE = 1000 * 60;
boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
boolean canGetLocation = false;
Location location; // location
double latitude = 0; // latitude
double longitude = 0; // longitude
String provider;
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1 * MINUTE;
// Declaring a Location Manager
protected LocationManager locationManager;
// Binder given to clients
private final IBinder mBinder = new LocalBinder();
/**
* Class used for the client Binder. Because we know this service always
* runs in the same process as its clients, we don't need to deal with IPC.
*/
public class LocalBinder extends Binder {
public LocationService getService() {
// Return this instance of LocalService so clients can call public
// methods
return LocationService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
public Location getLocation() {
try {
locationManager = (LocationManager) getBaseContext().getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled. DEFAULT COORDINATES
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES,
this);
Log.d("Network", "Network Enabled");
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES,
this);
Log.d("GPS", "GPS Enabled");
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
Log.i("LOCATION", "Latitude: " + latitude + "- Longitude: " + longitude);
return location;
}
@Override
public void onLocationChanged(Location arg0) {
NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = null;
intent = new Intent(this, CompleteSurveyActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_launcher).setAutoCancel(true)
.setContentIntent(contentIntent).setContentTitle(this.getString(R.string.app_name)).setContentText("text");
// mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify((int) System.currentTimeMillis() % Integer.MAX_VALUE, mBuilder.build());
double longitude = location.getLongitude();
double latitude = location.getLatitude();
Log.i("LOCATION", "Latitude: " + latitude + "- Longitude: " + longitude);
}
@Override
public void onProviderDisabled(String arg0) {
}
@Override
public void onProviderEnabled(String arg0) {
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
}
}
我从这里打来电话:
public class MyActivity extends Activity {
LocationService mService;
boolean mBound = false;
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className, IBinder service) {
// We've bound to LocalService, cast the IBinder and get
// LocalService instance
LocalBinder binder = (LocalBinder) service;
mService = binder.getService();
mBound = true;
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
mBound = false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity);
exampleButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
initService();
}
});
}
public void initService() {
if (mBound)
mService.getLocation();
}
@Override
protected void onStart() {
super.onStart();
// Bind to LocalService
Intent intent = new Intent(this, LocationService.class);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}
@Override
protected void onStop() {
super.onStop();
// Unbind from the service
if (mBound) {
unbindService(mConnection);
mBound = false;
}
}
}
的Manifest.xml
<service android:name=".LocationService" android:enabled="true"></service>
答案 0 :(得分:9)
与@ sven-menschner所说的相反,我认为未绑定的Service
正是您所需要的,因为绑定服务受绑定/取消绑定机制的影响,这会破坏您的服务。这就是我要做的事情:
在您的清单文件中,定义您的服务:
<service
android:name=".YourService"
android:enabled="true"
android:exported="true"
android:description="@string/my_service_desc"
android:label="@string/my_infinite_service">
<intent-filter>
<action android:name="com.yourproject.name.LONGRUNSERVICE" />
</intent-filter>
</service>
注意:有一个已经实施的操作列表,但您可以为启动服务的意图定义自己的操作。只需创建一个单例类并定义字符串,为它们分配一个唯一的String
。 “enabled”设置为true只是为了实例化服务,导出设置为true只是在您需要其他应用程序向您的Service
发送意图的情况下。如果没有,您可以安全地将最后一个设置为false。
以下步骤将从您的活动开始提供服务。这可以通过以下方式轻松完成:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent servIntent = new Intent("com.yourproject.name.LONGRUNSERVICE");
startService(servIntent);
...
}
}
最后一步是定义Service
初始化。密切关注onBind()
方法。由于您不希望绑定它,只需返回null
即可。它会是这样的:
public class MyService extends Service {
@Override
public IBinder onBind(Intent intent) {
// This won't be a bound service, so simply return null
return null;
}
@Override
public void onCreate() {
// This will be called when your Service is created for the first time
// Just do any operations you need in this method.
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
}
即使您关闭主Activity
,您的服务也会继续运行。还剩下一步:为了帮助您Service
无法完成,请将其作为前台服务运行(在您的服务中执行此操作)。这基本上会在状态栏中创建一个通知图标。这并不意味着您的主Activity也在运行(这就是您不想要绑定服务的原因),因为活动和服务具有不同的生命周期。为了帮助该服务运行这么长时间,请尽量保持堆尽可能低,这样可以避免Android SO杀死它。
还有一个声明:您无法测试服务是否仍在运行以杀死DVM。如果你杀了DVM,你就会杀死一切,也就是服务。
答案 1 :(得分:2)
Android服务有两种:启动和绑定。你正在寻找后者。 The documentation展示了如何使用它,下面有一个很好的生命周期图。
您需要先使用bindService()
调用startService()
,而不是使用{{1}}一步启动和绑定服务。然后它会一直运行,直到你停止它,即使该应用程序已关闭。但是不要忘记在正确的时间停止服务以避免内存问题等。
HTH