如何将BroadcastReceiver中的数据传送到另一个类BroadcastReceiver

时间:2015-03-18 10:53:55

标签: android broadcastreceiver

我在我的应用程序中使用BroadcastReceiver,在这里我收到当前位置并放在这个类中。因此我得到当前位置,因此我的问题是如何将此位置字符串从此类发送到另一个类。我必须在另一个类的listview中加载这个字符串数据,所以如何做到这一点请提前帮助谢谢。我的代码是

public class AlarmReceiver extends BroadcastReceiver implements LocationListener {
    MyTrip trip_method;
    double latitude, longitude;
    Context context;
    private static final long MIN_DISTANCE_FOR_UPDATE = 10;
    private static final long MIN_TIME_FOR_UPDATE = 1000 * 60 * 2;
    protected LocationManager locationManager;
    Location location;

    @Override
    public void onReceive(Context context, Intent intent) {
        trip_method = new MyTrip();

        this.context = context;
        locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        Find_location();
        // For our recurring task, we'll just display a message
        Toast.makeText(context, "I'm running", Toast.LENGTH_SHORT).show();
    }

    public void Find_location() {
        // Location nwLocation;
        try {
            location = getLocation(LocationManager.NETWORK_PROVIDER);
            System.out.println("nwLocation-------" + location);
            if (location != null) {
                latitude = location.getLatitude();
                longitude = location.getLongitude();
                System.out.println("latitude-------" + latitude + "longitude-----" + longitude);

                Toast.makeText(
                        context,
                        "Mobile Location (NW): \nLatitude: " + latitude
                            + "\nLongitude: " + longitude,
                        Toast.LENGTH_LONG).show();
                // To display the current address in the UI
                (new GetAddressTask()).execute(location);
            } else {
                trip_method.showSettingsAlert("NETWORK");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Location getLocation(String provider) {
        if (locationManager.isProviderEnabled(provider)) {
            locationManager.requestLocationUpdates(provider,
                    MIN_TIME_FOR_UPDATE, MIN_DISTANCE_FOR_UPDATE, this);
            if (locationManager != null) {
                location = locationManager.getLastKnownLocation(provider);
                return location;
            }
        }
        return null;
    } 
    /* *//**//*
     * Following is a subclass of AsyncTask which has been used to get
     * address corresponding to the given latitude & longitude.
     *//**//**/
    private class GetAddressTask extends AsyncTask<Location, Void, String> {

        Context mContext;

        /* public GetAddressTask(Context context) {
            super();
            mContext = context;
        }*/


        /* *//**//*
         * When the task finishes, onPostExecute() displays the address.
         *//**//**/
        @Override
        protected void onPostExecute(String address) {
            // Display the current address in the UI

            Toast.makeText(context.getApplicationContext(),
                    "address---" + address,
                    Toast.LENGTH_LONG).show();
        }

        @Override
        protected String doInBackground(Location... params) {

            String Address = null;
            String Location = null;
            Geocoder geocoder;
            List<android.location.Address> addresses;
            geocoder = new Geocoder(context, Locale.getDefault());
            try {
                addresses = geocoder.getFromLocation(latitude, longitude, 1);

                Address = addresses.get(0).getAddressLine(0) + ", " + addresses.get(0).getAddressLine(1) + ", " + addresses.get(0).getAddressLine(2);
                Location = addresses.get(0).getAddressLine(2);
                System.out.println("Location----" + Location);
               //This "Location" value i have to send to another activity
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            // Return the text
            return Address;
           /* } else {
                return "No address found";
            }*/
        }
    }// AsyncTask class
}

1 个答案:

答案 0 :(得分:0)

在适当的代码中使用它:

在你的头等舱:

Intent intent = new Intent(this, another_activity.class);
intent.putExtra("Location", Location.toString);
startService(intent)

在你的第二堂课:

 String Location = getActivity().getIntent.getStringExtra("Location") 

现在您已将字符串从一个类传递到另一个类。在您需要的地方使用Location变量。