即使手机休眠,也能在后台运行上传进程

3
我开发了一个安卓应用程序,它使用融合定位(LocationClient)每5秒钟确定用户的位置,并将这些数据发送到我的服务器。当应用正在运行时,整个过程每5秒钟重复一次。我使用AsyncTask类在后台上传数据。
问题: 当用户关闭应用程序或手机休眠时,数据上传停止。
我的要求: 即使用户关闭应用程序或手机进入睡眠状态,我也希望应用程序持续向我的服务器发送位置数据。这个过程应该在一个单独的线程上运行,因为我不想让这个过程影响到我的UI线程的响应能力。
目前的情况: 我听说过服务、intent服务和alarmmanager,但我不知道该使用哪一个。我还听说过wakelocks可以强制CPU不进入睡眠状态。请注意,我不想一直让屏幕保持亮着,因为这会耗费电池电量。
请问我如何让我的应用程序一直向服务器发送数据?

“我不想一直让屏幕保持开启状态,因为这会消耗电池。” -- 如果你关心用户的电池寿命,你就不会在使用GPS和每5秒与服务器通信时一直让设备运行。 - CommonsWare
AlarmManager是在手机休眠时运行服务的绝佳解决方案。网上有很多教程... - longwalker
@CommonsWare 我知道我的应用程序会消耗电池,但如果一直让手机的显示屏保持唤醒状态,它将消耗更多电量。这是一个GPS跟踪应用程序,所以我必须始终保持互联网和GPS的唤醒状态。 - Rakesh
@longwalker,你建议我在我的应用程序中使用AlarmManager吗?我不知道如何实现它。如果有任何教程链接将对我很有帮助。 - Rakesh
如果手机的位置没有显著变化,您不需要上传任何内容 - 这将节省电量。如果手机静止了几个5秒钟的时间段,您可能可以延长GPS检查的时间段。 - Martin James
1
@Rakesh 是的,这是一个不错的选择,但正如Martin James所建议的那样,只有在位置发生重大变化时才应更新它。否则会耗尽你的电池。我的建议是使用一个算法来检测更改位置的距离。这个链接应该会对你有很大帮助:http://developer.android.com/guide/topics/location/strategies.html。持续运行你的后台服务,并仅在位置发生重大变化时更新。希望这可以帮到你。 - longwalker
2个回答

5

在这里,您可以创建一个 Service 并使用 AlarmManager 按照需要每 5/10 秒调用此服务... 在您的 MainActivity 中。

public static AlarmManager alarm;
    public static PendingIntent pintent;

  // write this code on button click

            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.SECOND, 10);

            Intent intent = new Intent(this, MyService.class);


            pintent = PendingIntent.getService(this, 0, intent, 0);


            alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 5000, pintent);

 // button click functionality over


    // write this code outside onCreate()
    protected ServiceConnection mConnection = new ServiceConnection() {

            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                // TODO Auto-generated method stub

            }
        };

我的服务

public class MyService extends Service {
    public static int counter = 0;


    public MyService() {

    }

    @Override
    public IBinder onBind(Intent intent) {
        return new  Binder() ;
    }
    @Override
    public void onCreate() {
        Toast.makeText(this, "First Service was Created", Toast.LENGTH_SHORT).show();
       }

    @Override
    public void onStart(Intent intent, int startId) {

        counter++;
        Toast.makeText(this, " First Service Started" + "  " + counter,               Toast.LENGTH_SHORT).show();



    }

    @Override
    public void onDestroy() {
        Toast.makeText(this, "Service Destroyed", Toast.LENGTH_SHORT).show();
        }

    public void onTaskRemoved (Intent rootIntent){

        MainActivity.alarm.cancel(MainActivity.pintent);
        this.stopSelf();
       }

将此添加到

清单

中。
 <application
        ....
        <activity
         .....
          </activity>
     <service
            android:name=".MyService"
            android:enabled="true"
            android:exported="true" >
        </service>
      </application>

谢谢你的答复Anil。我有一个问题不太清楚。我应该使用闹钟管理器还是依赖于locationrequest的间隔时间来触发服务?代码 request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);request.setInterval(5000);request.setFastestInterval(5000);mLocationClient.requestLocationUpdates(request,this);我正在使用.requestLocationUpdates每5秒触发异步任务,该任务会将数据发送到服务器。 - Rakesh
我不知道伙计... :( - Looking Forward

0
你可以创建一个由你的应用程序启动的服务。它将在后台运行,并一直工作,直到用户在任务管理器中关闭它(如果你的代码中没有调用onDestroy函数)。

我听说服务在手机睡眠时会冻结,这意味着服务类中的代码在CPU进入睡眠状态时将不会执行。我该如何让服务在手机睡眠时仍然能够执行? - Rakesh
我认为Anil的回答是正确的。我的意思是你需要使用AlarmManager。 我认为你还需要这个权限<uses-permission android:name="android.permission.WAKE_LOCK" /> - ahmed_khan_89

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接