如何在用户在家或办公室时获得通知

时间:2015-02-06 05:58:28

标签: android notifications gps

我是Android的新手,我将开发一个小应用程序,用户可以获得“欢迎来到办公室”的通知,如果他的位置是他的办公室,如果他的位置是他的家,他的家也一样。请告诉我该怎么做才能做到这一点。

3 个答案:

答案 0 :(得分:2)

创建地理围栏 当用户进入该区域和出口区域时,地理围栏工作完美。 为未决意图提供唯一ID。

答案 1 :(得分:1)

使用以下代码

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;

import com.gs.mobile.Constants;
import com.gs.mobile.R;

public class MyLocation {
    private Timer timer1;
    private LocationManager lm;
    private LocationResult locationResult;
    private boolean gps_enabled=false;
    private boolean network_enabled=false;
    private AlertDialog.Builder dialog;

    public boolean getLocation(final Context context, LocationResult result){
        //I use LocationResult callback class to pass location value from MyLocation to user code.
        locationResult=result;
        if(lm==null)
            lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

        //exceptions will be thrown if provider is not permitted.
        try{gps_enabled=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);}catch(Exception ex){}
        try{network_enabled=lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);}catch(Exception ex){}

        //don't start listeners if no provider is enabled
        if(!gps_enabled && !network_enabled){
            dialog = new AlertDialog.Builder(context);
            dialog.setMessage(context.getResources().getString(R.string.gps_network_not_enabled));

            dialog.show();

        }
         if(gps_enabled)
            lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
        if(network_enabled)
            lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
        timer1=new Timer();
        timer1.schedule(new GetLastLocation(), 20000);
        return true;
    }

    LocationListener locationListenerGps = new LocationListener() {
        public void onLocationChanged(Location location) {
            timer1.cancel();
            locationResult.gotLocation(location);
            lm.removeUpdates(this);
            lm.removeUpdates(locationListenerNetwork);
        }
        public void onProviderDisabled(String provider) {}
        public void onProviderEnabled(String provider) {}
        public void onStatusChanged(String provider, int status, Bundle extras) {}
    };

    LocationListener locationListenerNetwork = new LocationListener() {
        public void onLocationChanged(Location location) {
            timer1.cancel();
            locationResult.gotLocation(location);
            lm.removeUpdates(this);
            lm.removeUpdates(locationListenerGps);
        }
        public void onProviderDisabled(String provider) {}
        public void onProviderEnabled(String provider) {}
        public void onStatusChanged(String provider, int status, Bundle extras) {}
    };

    class GetLastLocation extends TimerTask {
        @Override
        public void run() {
             lm.removeUpdates(locationListenerGps);
             lm.removeUpdates(locationListenerNetwork);

             Location net_loc=null, gps_loc=null;
             if(gps_enabled)
                 gps_loc=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
             if(network_enabled)
                 net_loc=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

             //if there are both values use the latest one
             if(gps_loc!=null && net_loc!=null){
                 if(gps_loc.getTime()>net_loc.getTime())
                     locationResult.gotLocation(gps_loc);
                 else
                     locationResult.gotLocation(net_loc);
                 return;
             }

             if(gps_loc!=null){
                 locationResult.gotLocation(gps_loc);
                 return;
             }
             if(net_loc!=null){
                 locationResult.gotLocation(net_loc);
                 return;
             }
             locationResult.gotLocation(null);
        }
    }

    public static abstract class LocationResult{
        public abstract void gotLocation(Location location);
    }
}//MyLocation-class

从活动中调用此内容如下所示

/* Used to Get Device GPS Location
     * <uses-permission android:name=�android.permission.ACCESS_FINE_LOCATION�>
     */
    public static LocationResult locationResult = new LocationResult(){

        @Override
        public void gotLocation(Location location) {
            // TODO Auto-generated method stub

        }
    };

    public static void getGPS(Activity _activity){
        MyLocation myLocation = new MyLocation();
        myLocation.getLocation(_activity, locationResult);

    }//getGPS()

    public static void getGPS(Activity _activity,LocationResult locationResult){
        MyLocation myLocation = new MyLocation();
        myLocation.getLocation(_activity, locationResult);

    }//getGPS()

每次当lat和lang改变时你将调用public void gotLocation(Location location)方法,你可以在那里检查该位置是否属于你的预期。

如果您愿意,即使您可以使用网址http://maps.googleapis.com/maps/api/geocode/xml?address=37.4188514,-122.0874526&sensor=true

您可以通过发送到此Google服务来获取该lat的名称。

答案 2 :(得分:0)

为此,您应该使用GPS来设置有关位置的通知。但请确保你sholud给用户提供保存位置forice and home或其他任何你想要保存的位置

希望它能帮到你