简体   繁体   中英

android location base notifications

I want to create an android app

that will send the device location after the app's installation

and every 15 minutes

What is the best way to do so?

1) I can create a timer inside the App's main Activity.

but then it will be stopped when the app is on the background. So I understand I have to create a service.

But then, how do I register it to work from device start up (after turning the device on and forever after)?

2) I saw this push notification tutorial , I think it doesn't fit my needs as the server sends the notification in broadcast, no?

I want different notifications to users based on their current location.

I want the user to actively send GPS location and then receive custom push to his location

(GEO based push notification)

Would you use something like the guy answered here ?

If you want to do something on a time-based interval, I suggest looking into 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

You can start the service by making a broadcast Receiver which listens to boot completed . Then start service in it. As far as getting location every 15 minutes is concerned you can use following code to get location every 15 minutes :-

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

replace updateTime with 90000 for 15 minutes. You should implement locationManager in your service .

If you are using google map android api, then there is a interface calls locationOnChange(LatLng points), this will be called every X min or sec ( X is defined by you ), that bascially gets your current location at giving LatLng point. Then if I was you, I would use NotificationManager to push the notification with the updated point to my phone

Here is the code I would do for this.

    //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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM