Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用wifi获取位置,但不使用gps_Android_Service_Gps_Android Wifi_Smsmanager - Fatal编程技术网

Android 使用wifi获取位置,但不使用gps

Android 使用wifi获取位置,但不使用gps,android,service,gps,android-wifi,smsmanager,Android,Service,Gps,Android Wifi,Smsmanager,我遇到了一个问题,我正在开发一个应用程序,它将始终在后台运行,并在一段时间后通过短信发送gps坐标,如果手机重新启动,该应用程序将自动启动,而无需任何启动活动。我突然遇到一个问题,当gps启用时,我的应用程序在android kitkat上获取位置,当gps禁用时,它在jelly bean中工作。我真的不知道该怎么办。请告诉我哪里错了,并建议我如何修复它 我的接受者班 public class AlarmReceiver extends BroadcastReceiver{ long time

我遇到了一个问题,我正在开发一个应用程序,它将始终在后台运行,并在一段时间后通过短信发送gps坐标,如果手机重新启动,该应用程序将自动启动,而无需任何启动活动。我突然遇到一个问题,当gps启用时,我的应用程序在android kitkat上获取位置,当gps禁用时,它在jelly bean中工作。我真的不知道该怎么办。请告诉我哪里错了,并建议我如何修复它

我的接受者班

public class AlarmReceiver extends BroadcastReceiver{

long time = 5* 1000; 
long distance = 10; 

@Override
public void onReceive(final Context context, Intent intent) {

    System.out.println("alarm receiver....");
    Intent service = new Intent(context, MyService.class);
    context.startService(service);

    //Start App On Boot Start Up
    if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
    Intent App = new Intent(context, MainActivity.class);
    App.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(App);
    }

    LocationManager locationManager = (LocationManager)context
            .getSystemService(Context.LOCATION_SERVICE);

    Criteria criteria = new Criteria();
    String provider = locationManager.getBestProvider(criteria, true);
    locationManager.requestLocationUpdates(provider, time, distance, locationListener);

    Location location = locationManager.getLastKnownLocation(provider);
    TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    String device_id = tm.getDeviceId();  // returns IMEI number 

    String phoneNo = "+923333333";
    String Text = "Latitude = " + location.getLatitude() +" Longitude = " + location.getLongitude() + " Device Id: " + device_id;

    try {
    SmsManager smsManager = SmsManager.getDefault();
    smsManager.sendTextMessage(phoneNo, null, Text, null, null);
    Log.i("Send SMS", "");

    Toast.makeText(context, "message sent", Toast.LENGTH_SHORT).show();

    } catch (Exception e) {
    Toast.makeText(context, "SMS faild, please try again.",
    Toast.LENGTH_LONG).show();
    e.printStackTrace();
 } 
    this.abortBroadcast();
}

}


所有必需的权限都添加到清单文件中。

尝试这种方式,希望这将帮助您解决问题。 在希望通过wifi和internet获取位置的地方使用此类

叫这个方法,, //*** 新的GetCurLocation(getActivity(),0,true,null,this,2000)

并实现“SetOnLocationFoundListner”,您可以通过wifi和internet找到位置。谢谢

public class GetCurLocation implements LocationListener,
    GooglePlayServicesClient.ConnectionCallbacks,
    GooglePlayServicesClient.OnConnectionFailedListener {

public static LocationClient mLocationClient;
LocationRequest mLocationRequest;
public static LocationManager locationmanager;

float accuracy = 500;
Activity context;
boolean getLocRegularly = false;
int interval = 1000;
float Radius;
GoogleMap gmap;

SetOnLocationFoundListner OLF;

public interface SetOnLocationFoundListner {
    public void onLocationFound(Location location, boolean getLocRegularly,
            GoogleMap gmap);
}

public void RemoveUpdates() {
    try {
        if (mLocationClient != null)
            mLocationClient.removeLocationUpdates(this);
        if (locationmanager != null)
            locationmanager.removeUpdates(LocUpByLocMgr);
    } catch (Exception e) {
        e.printStackTrace();
    }

}

/*
 * radius should be in meters
 */
public GetCurLocation(Activity activity, int interval,
        boolean getLocRegularly, GoogleMap gmap,
        SetOnLocationFoundListner OLF, float Radius) {
    this.OLF = OLF;
    this.gmap = gmap;
    this.context = activity;
    this.getLocRegularly = getLocRegularly;
    this.interval = interval;
    this.Radius = Radius;
    if (servicesConnected()) {
        mLocationClient = new LocationClient(context, this, this);
        mLocationClient.connect();
        mLocationRequest = LocationRequest.create();
        mLocationRequest.setInterval(interval);
        mLocationRequest
                .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        mLocationRequest.setFastestInterval(interval);
    }

    locationmanager = (LocationManager) context
            .getSystemService(Context.LOCATION_SERVICE);
    Criteria cr = new Criteria();
    String provider = locationmanager.getBestProvider(cr, true);
    locationmanager.requestLocationUpdates(provider, interval, 10,
            LocUpByLocMgr);
}

private boolean servicesConnected() {
    int resultCode = GooglePlayServicesUtil
            .isGooglePlayServicesAvailable(context);
    if (ConnectionResult.SUCCESS == resultCode) {
        return true;
    } else {
        Dialog dialog = GooglePlayServicesUtil.getErrorDialog(resultCode,
                (Activity) context, 0);
        if (dialog != null) {
        }
        return false;
    }
}

@Override
public void onConnectionFailed(ConnectionResult result) {

}

@Override
public void onConnected(Bundle connectionHint) {
    try {

        Location location = mLocationClient.getLastLocation();

        Log.e("testing",
                location.getLatitude() + "," + location.getLongitude()
                        + "," + location.getAccuracy());
        if (location.getAccuracy() < Radius) {
            OLF.onLocationFound(location, getLocRegularly, gmap);
            locationmanager.removeUpdates(LocUpByLocMgr);
        } else
            mLocationClient.requestLocationUpdates(mLocationRequest, this);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Override
public void onDisconnected() {
}

@Override
public void onLocationChanged(Location location) {
    try {
        if (location.getAccuracy() > Radius) {
            Log.e("testing LC", location.getAccuracy()
                    + " Its Not Accurate");
        } else {
            Log.e("testing LC", location.getAccuracy() + " Its Accurate");
            try {
                OLF.onLocationFound(location, getLocRegularly, gmap);
                if (!getLocRegularly) {
                    mLocationClient.removeLocationUpdates(this);
                    locationmanager.removeUpdates(LocUpByLocMgr);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

}

android.location.LocationListener LocUpByLocMgr = new android.location.LocationListener() {

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onLocationChanged(Location location) {

        try {
            if (location.getAccuracy() > Radius) {
                Log.e("testing LM", location.getAccuracy()
                        + " Its Not Accurate");
            } else {
                Log.e("testing LM", location.getAccuracy()
                        + " Its Accurate");

                try {
                    OLF.onLocationFound(location, getLocRegularly, gmap);
                    if (!getLocRegularly) {
                        mLocationClient
                                .removeLocationUpdates(GetCurLocation.this);
                        locationmanager.removeUpdates(LocUpByLocMgr);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
};
公共类GetCurLocation实现LocationListener,
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener{
公共静态位置客户端mLocationClient;
位置请求mLocationRequest;
公共静态位置管理器位置管理器;
浮动精度=500;
活动语境;
布尔getLocRegulary=false;
整数区间=1000;
浮动半径;
谷歌地图;
SetOnLocationFoundListner OLF;
公共接口SetOnLocationFoundListner{
找到公共void onLocationFound(位置,布尔GetLocation,
谷歌地图(gmap);
}
public void RemoveUpdates(){
试一试{
if(mLocationClient!=null)
mLocationClient.RemovelocationUpdate(此);
如果(locationmanager!=null)
locationmanager.RemoveUpdate(LoopByLocMgr);
}捕获(例外e){
e、 printStackTrace();
}
}
/*
*半径应以米为单位
*/
公共GetCurLocation(活动,int间隔,
布尔值,谷歌地图gmap,
SetOnLocationFoundListner(浮动半径){
this.OLF=OLF;
this.gmap=gmap;
这个上下文=活动;
this.getLocRegulary=getLocRegulary;
这个。间隔=间隔;
这个。半径=半径;
if(servicesConnected()){
mLocationClient=新位置客户端(上下文,this,this);
mLocationClient.connect();
mLocationRequest=LocationRequest.create();
mlLocationRequest.setInterval(间隔);
M定位请求
.setPriority(位置请求。优先级高精度);
mlLocationRequest.SetFastTestInterval(间隔);
}
locationmanager=(locationmanager)上下文
.getSystemService(Context.LOCATION\u服务);
标准cr=新标准();
字符串提供程序=locationmanager.getBestProvider(cr,true);
locationmanager.RequestLocationUpdate(提供程序,间隔,10,
蝗虫;
}
私有布尔服务连接(){
int resultCode=GooglePlayServicesUtil
.isGooglePlayServicesAvailable(上下文);
if(ConnectionResult.SUCCESS==resultCode){
返回true;
}否则{
Dialog Dialog=GooglePlayServicesUtil.getErrorDialog(结果代码,
(活动)上下文,0);
如果(对话框!=null){
}
返回false;
}
}
@凌驾
连接失败的公共void(连接结果){
}
@凌驾
未连接的公共无效(捆绑连接提示){
试一试{
Location Location=mLocationClient.getLastLocation();
Log.e(“测试”,
location.getLatitude()+“,”+location.getLatitude()
+“,”+location.getAccurance());
if(location.getAccurance()半径){
Log.e(“测试LC”,location.getAccurance()
+“不准确”);
}否则{
Log.e(“测试LC”,location.getAccurance()+“其准确度”);
试一试{
OLF.onLocationFound(位置、GetLocation、gmap);
如果(!GetLocRegulary){
mLocationClient.RemovelocationUpdate(此);
locationmanager.RemoveUpdate(LoopByLocMgr);
}
}捕获(例外e){
e、 printStackTrace();
}
}
}捕获(例外e){
e、 printStackTrace();
}
}
android.location.LocationListener-LoopByLocMgr=新的android.location.LocationListener(){
@凌驾
public void onStatusChanged(字符串提供程序、int状态、Bundle extra){
//TODO自动生成的方法存根
}
@凌驾
公共无效onProviderEnabled(字符串提供程序){
//TODO自动生成的方法存根
}
@凌驾
公共无效onProviderDisabled(字符串提供程序){
//TODO自动生成的方法存根
}
@凌驾
已更改位置上的公共无效(位置){
试一试{
if(location.getAccurance()>半径){
Log.e(“测试LM”,location.getAccurance()
+“不准确”);
}否则{
Log.e(“测试LM”,location.getAccurance()
+“其准确性”);
试一试{
OLF.onLocationFound(位置、GetLocation、gmap);
如果(!GetLocRegulary){
mLocationClient
.RemovelocationUpdate(GetCurLocation.this);
locationmanager.RemoveUpdate(LoopByLocMgr);
public class GetCurLocation implements LocationListener,
    GooglePlayServicesClient.ConnectionCallbacks,
    GooglePlayServicesClient.OnConnectionFailedListener {

public static LocationClient mLocationClient;
LocationRequest mLocationRequest;
public static LocationManager locationmanager;

float accuracy = 500;
Activity context;
boolean getLocRegularly = false;
int interval = 1000;
float Radius;
GoogleMap gmap;

SetOnLocationFoundListner OLF;

public interface SetOnLocationFoundListner {
    public void onLocationFound(Location location, boolean getLocRegularly,
            GoogleMap gmap);
}

public void RemoveUpdates() {
    try {
        if (mLocationClient != null)
            mLocationClient.removeLocationUpdates(this);
        if (locationmanager != null)
            locationmanager.removeUpdates(LocUpByLocMgr);
    } catch (Exception e) {
        e.printStackTrace();
    }

}

/*
 * radius should be in meters
 */
public GetCurLocation(Activity activity, int interval,
        boolean getLocRegularly, GoogleMap gmap,
        SetOnLocationFoundListner OLF, float Radius) {
    this.OLF = OLF;
    this.gmap = gmap;
    this.context = activity;
    this.getLocRegularly = getLocRegularly;
    this.interval = interval;
    this.Radius = Radius;
    if (servicesConnected()) {
        mLocationClient = new LocationClient(context, this, this);
        mLocationClient.connect();
        mLocationRequest = LocationRequest.create();
        mLocationRequest.setInterval(interval);
        mLocationRequest
                .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        mLocationRequest.setFastestInterval(interval);
    }

    locationmanager = (LocationManager) context
            .getSystemService(Context.LOCATION_SERVICE);
    Criteria cr = new Criteria();
    String provider = locationmanager.getBestProvider(cr, true);
    locationmanager.requestLocationUpdates(provider, interval, 10,
            LocUpByLocMgr);
}

private boolean servicesConnected() {
    int resultCode = GooglePlayServicesUtil
            .isGooglePlayServicesAvailable(context);
    if (ConnectionResult.SUCCESS == resultCode) {
        return true;
    } else {
        Dialog dialog = GooglePlayServicesUtil.getErrorDialog(resultCode,
                (Activity) context, 0);
        if (dialog != null) {
        }
        return false;
    }
}

@Override
public void onConnectionFailed(ConnectionResult result) {

}

@Override
public void onConnected(Bundle connectionHint) {
    try {

        Location location = mLocationClient.getLastLocation();

        Log.e("testing",
                location.getLatitude() + "," + location.getLongitude()
                        + "," + location.getAccuracy());
        if (location.getAccuracy() < Radius) {
            OLF.onLocationFound(location, getLocRegularly, gmap);
            locationmanager.removeUpdates(LocUpByLocMgr);
        } else
            mLocationClient.requestLocationUpdates(mLocationRequest, this);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Override
public void onDisconnected() {
}

@Override
public void onLocationChanged(Location location) {
    try {
        if (location.getAccuracy() > Radius) {
            Log.e("testing LC", location.getAccuracy()
                    + " Its Not Accurate");
        } else {
            Log.e("testing LC", location.getAccuracy() + " Its Accurate");
            try {
                OLF.onLocationFound(location, getLocRegularly, gmap);
                if (!getLocRegularly) {
                    mLocationClient.removeLocationUpdates(this);
                    locationmanager.removeUpdates(LocUpByLocMgr);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

}

android.location.LocationListener LocUpByLocMgr = new android.location.LocationListener() {

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onLocationChanged(Location location) {

        try {
            if (location.getAccuracy() > Radius) {
                Log.e("testing LM", location.getAccuracy()
                        + " Its Not Accurate");
            } else {
                Log.e("testing LM", location.getAccuracy()
                        + " Its Accurate");

                try {
                    OLF.onLocationFound(location, getLocRegularly, gmap);
                    if (!getLocRegularly) {
                        mLocationClient
                                .removeLocationUpdates(GetCurLocation.this);
                        locationmanager.removeUpdates(LocUpByLocMgr);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
};