如何从 BroadcastReceiver 获取数据到另一个 class BroadcastReceiver

How to get the data from the BroadcastReceiver to another class BroadcastReceiver

我在我的应用程序中使用 BroadcastReceiver,我在这里接收当前位置和位置 class.hence 我正在获取当前位置,因此我的问题是如何从这个 class 到另一个 class 。我必须将此字符串数据加载到另一个 class 的列表视图中,所以如何做到这一点请提前致谢。我的代码是

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
}

在您的代码中适当使用它:

在你的第一个 class:

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

在你的第二个 class:

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

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