如何在Activity中与持续运行的后台服务进行通信?

时间:2014-12-18 05:32:36

标签: android android-intent android-service android-broadcast android-location

在我的项目中,有一项服务可以检索用户的位置。它在后台持续运行。我想在textview上设置位置。 所以,我有ManinActivity如下:

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MainActivity extends Activity {
    GPSTracker gps;
    double mylatitude, mylongitude;
    TextView txt_location;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        txt_location = (TextView)findViewById(R.id.current_location);

        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
        }              
    }

    public void btn_remember_Clicked(View v)
    {
        Intent i = new Intent(MainActivity.this,RememberActivity.class);
        startActivity(i);
    }

    public void btn_show_places_Clicked(View v)
    {
        Intent i = new Intent(MainActivity.this,ShowPlacesActivity.class);
        startActivity(i);
    }

    public static class PlaceholderFragment extends Fragment {
        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            return inflater.inflate(R.layout.fragment_main, container, false);    
        }
    }
}

我的LocationService课程如下:

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class LocationService extends Service
{
    public static final String BROADCAST_ACTION = "Hello World";
    private static final int TWO_MINUTES = 1000 * 60 * 2;
    public LocationManager locationManager;
    public MyLocationListener listener;
    public Location previousBestLocation = null;

    Intent intent;    

    @Override
    public void onCreate()
    {
        super.onCreate();
        intent = new Intent(BROADCAST_ACTION);
    }

    @Override
    public int onStartCommand(Intent intent,int flags, int startId)
    {
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        listener = new MyLocationListener();
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 4000, 0, listener);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 4000, 0, listener);
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent)
    {
        return null;
    }

    protected boolean isBetterLocation(Location location, Location currentBestLocation) {
        if (currentBestLocation == null) {
            // A new location is always better than no location
            return true;
        }

        // Check whether the new location fix is newer or older
        long timeDelta = location.getTime() - currentBestLocation.getTime();
        boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;
        boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
        boolean isNewer = timeDelta > 0;

        // If it's been more than two minutes since the current location, use the new location
        // because the user has likely moved
        if (isSignificantlyNewer) {
            return true;
            // If the new location is more than two minutes older, it must be worse
        } else if (isSignificantlyOlder) {
            return false;
        }

        // Check whether the new location fix is more or less accurate
        int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy());
        boolean isLessAccurate = accuracyDelta > 0;
        boolean isMoreAccurate = accuracyDelta < 0;
        boolean isSignificantlyLessAccurate = accuracyDelta > 200;

        // Check if the old and new location are from the same provider
        boolean isFromSameProvider = isSameProvider(location.getProvider(),
                    currentBestLocation.getProvider());

        // Determine location quality using a combination of timeliness and accuracy
        if (isMoreAccurate) {
            return true;
        } else if (isNewer && !isLessAccurate) {
            return true;
        } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) {
            return true;
        }
        return false;
    }

    /** Checks whether two providers are the same */
    private boolean isSameProvider(String provider1, String provider2) {
        if (provider1 == null) {
            return provider2 == null;
        }
        return provider1.equals(provider2);
    }

    @Override
    public void onDestroy() {
        // handler.removeCallbacks(sendUpdatesToUI);
        super.onDestroy();
        Log.v("STOP_SERVICE", "DONE");
        locationManager.removeUpdates(listener);
    }

    public static Thread performOnBackgroundThread(final Runnable runnable) {
        final Thread t = new Thread() {
            @Override
            public void run() {
                try {
                    runnable.run();
                } catch(Exception e) {
                    System.out.print(e);
                }
            }
        };
        t.start();
        return t;
    }

    public class MyLocationListener implements LocationListener {
        public void onLocationChanged(final Location loc) {
            Log.i("**************************************", "Location changed");
            if(isBetterLocation(loc, previousBestLocation)) {
                loc.getLatitude();
                loc.getLongitude();
                intent.putExtra("Latitude", loc.getLatitude());
                intent.putExtra("Longitude", loc.getLongitude());
                intent.putExtra("Provider", loc.getProvider());
                sendBroadcast(intent);        
            }
        }

        public void onProviderDisabled(String provider) {
            Toast.makeText( getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT ).show();
        }

        public void onProviderEnabled(String provider) {
            Toast.makeText(getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show();
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    }
}

我的LocationBroadcast课程如下:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class LocationBroadcast extends BroadcastReceiver{
    @Override
    public void onReceive(Context ctx, Intent intent) {
        ctx.startService(new Intent(ctx, LocationService.class));
    }
}

我想在txt_location中的textview MainActivity上设置位置。我对服务以及我们如何在Activity中进行沟通知之甚少。如何在MainActivity中获得经度和纬度?请帮忙。

更新

我刚刚根据答案更新了我的代码:

public class LocationService extends Service
{
    private static final int TWO_MINUTES = 1000 * 60 * 2;
    public LocationManager locationManager;
    public MyLocationListener listener;
    public Location previousBestLocation = null;

    Intent i;

    @Override
    public void onCreate()
    {
        super.onCreate();
        i = new Intent("LOCATION_CHANGED");
    }

    @Override
    public int onStartCommand(Intent intent,int flags, int startId)
    {
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        listener = new MyLocationListener();
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 4000, 0, listener);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 4000, 0, listener);
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent)
    {
        return null;
    }

    protected boolean isBetterLocation(Location location, Location currentBestLocation) {
        if (currentBestLocation == null) {
            // A new location is always better than no location
            return true;
        }

        // Check whether the new location fix is newer or older
        long timeDelta = location.getTime() - currentBestLocation.getTime();
        boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;
        boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
        boolean isNewer = timeDelta > 0;

        // If it's been more than two minutes since the current location, use the new location
        // because the user has likely moved
        if (isSignificantlyNewer) {
            return true;
            // If the new location is more than two minutes older, it must be worse
        } else if (isSignificantlyOlder) {
            return false;
        }

        // Check whether the new location fix is more or less accurate
        int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy());
        boolean isLessAccurate = accuracyDelta > 0;
        boolean isMoreAccurate = accuracyDelta < 0;
        boolean isSignificantlyLessAccurate = accuracyDelta > 200;

        // Check if the old and new location are from the same provider
        boolean isFromSameProvider = isSameProvider(location.getProvider(),
                currentBestLocation.getProvider());

        // Determine location quality using a combination of timeliness and accuracy
        if (isMoreAccurate) {
            return true;
        } else if (isNewer && !isLessAccurate) {
            return true;
        } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) {
            return true;
        }
        return false;
    }

    /** Checks whether two providers are the same */
    private boolean isSameProvider(String provider1, String provider2) {
        if (provider1 == null) {
            return provider2 == null;
        }
        return provider1.equals(provider2);
    }

    @Override
    public void onDestroy() {
        // handler.removeCallbacks(sendUpdatesToUI);
        super.onDestroy();
        Log.v("STOP_SERVICE", "DONE");
        locationManager.removeUpdates(listener);
    }

    public static Thread performOnBackgroundThread(final Runnable runnable) {
        final Thread t = new Thread() {
            @Override
            public void run() {
                try {
                    runnable.run();
                } catch(Exception e) {
System.out.print(e);
                }
            }
        };
        t.start();
        return t;
    }

    public class MyLocationListener implements LocationListener
    {
        public void onLocationChanged(final Location loc)
        {
            Log.i("**************************************", "Location changed");
            if(isBetterLocation(loc, previousBestLocation)) {
                String newLocation = "Lat: " + loc.getLatitude() + " and Long: " + loc.getLongitude();
               i = new Intent("LOCATION_CHANGED");
                i.putExtra("location", newLocation);
                sendBroadcast(i);
            }
        }

        public void onProviderDisabled(String provider)
        {
            Toast.makeText( getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT ).show();
        }


        public void onProviderEnabled(String provider)
        {
            Toast.makeText(getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show();
        }

        public void onStatusChanged(String provider, int status, Bundle extras)
        {
        }
    }
}

我已在MainActivity收到广播意图如下:

public class MainActivity extends Activity {
    TextView txt_location;    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        txt_location = (TextView)findViewById(R.id.current_location);

        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
    }    

    public class LocationBroadcast extends BroadcastReceiver {
        @Override
        public void onReceive(Context ctx, Intent intent) {
            txt_location.setText(intent.getExtras().getString("location"));
        }
    }

    public static class PlaceholderFragment extends Fragment {
        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {    
            return inflater.inflate(R.layout.fragment_main, container, false);
        }
    }
}

enter image description here

如您所见,它没有显示任何位置更新。 Logcat没有显示任何错误。任何人都可以指导我这是什么错误吗?

2 个答案:

答案 0 :(得分:4)

BroadcastReceiver中创建MainActivity

BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context appContext, Intent intent) {
        .......
        //get location from Service and create Method for set location on textview txt_location

    }
};
registerReceiver

onResume

MainActivity

registerReceiver(broadcastReceiver, new IntentFilter(GET_LOCATION));

并在MainActivity

开始时声明这一点
private static final String GET_LOCATION = "com.pkg.location";

并在您的服务中将您的位置作为广告传递

Intent intent = new Intent(GET_LOCATION);
intent.putExtra("location", "your_location_got_from_service");
sendBroadcast(intent);

并且不要忘记unregisterReceiver

中的MainActivity
@Override
protected void onPause() {
    super.onPause();
    unregisterReceiver(broadcastReceiver);
}

答案 1 :(得分:2)

您可以使用BroadcastReceiver接收 LOCATION_CHANGED 的事件,并使用TextView方法更新onReceive

在您的活动中:

TextView textView = (TextView) findViewById(R.id.location_text);  //global variable

registerReceiver(locationChangedReceiver, new IntentFilter("LOCATION_CHANGED"));

//your receiver class as inner class of activity.
private BroadcastReceiver locationChangedReceiver= new BroadcastReceiver() {

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

        textView.setText(intent.getExtras().getString("location"));

    }
};

在您的服务中,您只需广播&#34; LOCATION_CHANGED&#34;,并将字符串位置作为意图的额外参数传递。

String newLocation = "Lat: " + loc.getLatitude() + " and Long: " + loc.getLongitude();
Intent i = new Intent("LOCATION_CHANGED");
i.putExtra("location", newLocation);
sendBroadcast(i);

如果你想保留一个单独的接收器类(不在活动中),你可以使用你的上下文来更新这样的视图:

public class LocationBroadcast extends BroadcastReceiver{
    @Override
    public void onReceive(Context ctx, Intent intent) {
        TextView textView = (TextView) ctx.findViewById(R.id.location_text);
        textView.setText(intent.getExtras().getString("location"));
    }

}

希望它有所帮助。