为broadcastreceiver类中的服务类创建的对象不起作用

时间:2015-02-26 17:29:12

标签: android

我正在开发应用程序,我在接收smsword时检索移动设备的当前位置,然后将手机的位置作为短信发送给发送者。我创建了一个单独的服务类。当我想调用方法时服务类我已经为服务类定义了对象并试图检索它们。问题是没有错误。但是在创建对象之后,在它之后写的语句不会被执行。 这是我在broadcastreceiverclass中的代码

    if (message.equals(locatecode)) {
       Intent serviceIntent = new Intent(context,LocationService.class);
       context.startService(serviceIntent); //start service for get location
       LocationService appLocationService = new LocationService(context);
       Toast.makeText(context,"After Locationservice object creation", Toast.LENGTH_LONG).show();Location gpsLocation = appLocationService.getLocation(LocationManager.NETWORK_PROVIDER);
                    if (gpsLocation != null) {
                        double latitude = gpsLocation.getLatitude();
                        double longitude = gpsLocation.getLongitude();
                        Toast.makeText(context,"Mobile Location (GPS): \nLatitude: " + latitude+ "\nLongitude: " + longitude,Toast.LENGTH_LONG).show();
                        try {
                            SmsManager smsManager = SmsManager.getDefault();
                            smsManager.sendTextMessage(senderNum, null, m1 + "latitude is" + latitude + "longitude is" + longitude, null, null);
                            Toast toast = Toast.makeText(context, "SMS sent.", Toast.LENGTH_LONG);
                            toast.show();
                        } catch (Exception e) {
                            Toast.makeText(context, "SMS failed, please try again.", Toast.LENGTH_LONG).show();
                        }
                    }
                    else {
                        Toast.makeText(context,"location cannot be retrieved",Toast.LENGTH_LONG).show();
                    }
my locationservice class is as follows:

   public class LocationService extends Service implements LocationListener{
   protected LocationManager locationManager;
    Location location; 
    public void onCreate() {
    Toast.makeText(this,"inside create changed", Toast.LENGTH_LONG).show();
    locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
     Criteria criteria=new Criteria();
     String provider=locationManager.getBestProvider(criteria,true);
     Toast.makeText(this,provider, Toast.LENGTH_LONG).show();
     if (locationManager.isProviderEnabled(provider)) {
         locationManager.requestLocationUpdates(provider,0,0,this);
         if (locationManager != null) {
             location = locationManager.getLastKnownLocation(provider);                
            if(location!=null){
                Double latitude=location.getLatitude();
                Double longitude=location.getLongitude();                            
            }
        }
    }

}
public Location getLocation(String provider) {
    if (locationManager.isProviderEnabled(provider)) {
        locationManager.requestLocationUpdates(provider,0,0,this);
        if (locationManager != null) {
            location = locationManager.getLastKnownLocation(provider);
            return location;
        }
    }
    return null;
}
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onLocationChanged(Location location) {
    Toast.makeText(this,"inside nlocationchanged",Toast.LENGTH_LONG).show();
    if(location!=null){
        Double latitude=location.getLatitude();
        Double longitude=location.getLongitude();             Toast.makeText(this,"latitude1"+latitude+"longitude1"+longitude,Toast.LENGTH_LONG).show();
    }
}

0 个答案:

没有答案