【问题标题】:Android Activity in background - recording GPSAndroid Activity 在后台 - 记录 GPS
【发布时间】:2012-10-22 13:08:04
【问题描述】:

我正在开发具有记录 GPS 位置的 Activity 的应用程序。它注册 LocationListener 更新并将记录的 Location 存储到 SQLite 数据库。我的问题是当用户隐藏我的应用程序或运行另一个应用程序时,我的应用程序和录制活动进入后台并可能被系统杀死。因此,不存储 GPS 数据。我希望即使应用程序进入后台,我的应用程序和记录活动仍会记录 GPS 位置。 如何实现?

谢谢

【问题讨论】:

  • 您可以编写代码以在服务而不是活动中获取位置。

标签: java android android-activity gps


【解决方案1】:

Service 最适合您打算做的事情。

请确保您启动服务STICKY

【讨论】:

    【解决方案2】:

    使用 Android 的其他组件,例如 BroadcastReceiverService,它们可以在后台运行而无需任何 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.
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-01-20
      • 1970-01-01
      • 1970-01-01
      • 2017-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多