android位置基础通知

时间:2014-01-15 19:46:26

标签: java android service push-notification

我想创建一个Android应用

将在应用安装后发送设备位置

每15分钟

最好的方法是什么?

1)我可以在App的主Activity中创建一个计时器。

但是当应用程序在后台时它将被停止。所以我理解我必须创建一项服务。

但是,如何在设备启动时注册它(在打开设备之后和之后永远)?

2)我看到了这个推送通知tutorial,我认为它不符合我的需求,因为服务器在广播中发送通知,不是吗?

我希望根据用户的当前位置向用户发送不同的通知。

我希望用户主动发送GPS位置,然后接收自定义推送到他的位置

(基于GEO的推送通知)

你会使用回答here的人吗?

3 个答案:

答案 0 :(得分:1)

如果您想在基于时间的时间间隔内执行某些操作,建议您查看AlarmManager

Alarms (based on the AlarmManager class) give you a way to perform time-based operations outside the lifetime of your application.

http://developer.android.com/training/scheduling/alarms.html

答案 1 :(得分:1)

您可以通过制作广播接收器来监听启动已完成来启动该服务。然后开始服务。至于每隔15分钟获取一次位置,您可以使用以下代码每15分钟获取一次位置: -

locationManager.requestLocationUpdates(usedLocationService, updateTime, updateDistance, this);

updateTime替换为90000 15分钟。您应该在service

中实施locationManager

答案 2 :(得分:0)

如果你使用google map android api,那么就有一个接口调用 locationOnChange(LatLng points),每隔X min或sec调用一次(X由你定义),在得到LatLng点时基本上得到你当前的位置。然后,如果我是你,我会使用NotificationManager将通知与更新点推送到我的手机

以下是我要为此做的代码。

    //At OnCreate


    mLocationClient = new LocationClient(this, this, this);
        mLocationRequest = LocationRequest.create();
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        mLocationRequest.setInterval(UPDATE_INTERVAL);
        mLocationRequest.setFastestInterval(FASTEST_INTERVAL);

mLocationClient.requestLocationUpdates(mLocationRequest, this)

//Google Map interface

    @Override
    public void onLocationChanged(Location location)

//inside I would use the notificationManager to push the location
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                    context).setSmallIcon(R.drawable.btn_star)
                    .setContentTitle("My notification~~~~")
                    .setContentText("Hello World!~~~~")
                    .setStyle(new NotificationCompat.BigTextStyle().bigText(s));

            NotificationManager mNotificationManager = (NotificationManager) context
                    .getSystemService(Context.NOTIFICATION_SERVICE);

            mNotificationManager.notify(123, mBuilder.build());