Java 后台Android活动-记录GPS
我正在开发一个应用程序,其中我有记录GPS位置的活动。它注册LocationListener更新,并将记录的位置存储到SQLite数据库。我的问题是,当用户隐藏我的应用程序或运行另一个应用程序时,我的应用程序和录制活动进入后台,可能会被系统杀死。因此,不存储GPS数据。我希望我的应用程序和录制活动仍然记录GPS位置,即使应用程序转到后台。 如何做到这一点Java 后台Android活动-记录GPS,java,android,android-activity,gps,Java,Android,Android Activity,Gps,我正在开发一个应用程序,其中我有记录GPS位置的活动。它注册LocationListener更新,并将记录的位置存储到SQLite数据库。我的问题是,当用户隐藏我的应用程序或运行另一个应用程序时,我的应用程序和录制活动进入后台,可能会被系统杀死。因此,不存储GPS数据。我希望我的应用程序和录制活动仍然记录GPS位置,即使应用程序转到后台。 如何做到这一点 谢谢使用Android的其他组件,如BroadcastReceiver或服务,它们可以在后台运行,无需任何GUI public class U
谢谢使用Android的其他组件,如
BroadcastReceiver
或服务
,它们可以在后台运行,无需任何GUI
public class UploadData extends BroadcastReceiver
{
private static LocationManager locationManager = null;
private static LocationListener locationListener = null;
@Override
public void onReceive(Context context, Intent intent)
{
// Create and AlarmManager that will periodically fetch the GPS
}
}
在服务的情况下
public class myservice extends Service
{
LocationManager locationManager = null;
LocationListener loclistener = null;
// Bind it, Its important
public IBinder onBind(Intent intent)
{
return null;
}
@Override
public void onCreate()
{
// Use AlarmManager to fetch the GPS periodically.
}
}
A对于你打算做的事情来说是最好的
让舒尔在启动服务时保持粘性您可以编写代码以获取服务中的位置,而不是活动。