如何从广播接收器中的数据得到另一个类广播接收器 [英] How to get the data from the BroadcastReceiver to another class BroadcastReceiver

查看:161
本文介绍了如何从广播接收器中的数据得到另一个类广播接收器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用我的应用程序的BroadcastReceiver,我在这里接收当前的位置和发生在这个class.hence我得到的当前位置,因此,我的问题是如何从这类发送此位置字符串到另一个类。我必须加载在ListView这串数据在另一个类,这样怎么能做到这一点,请帮助在此先感谢。我的code是

 公共类AlarmReceiver扩展广播接收器实现LocationListener的{
    MyTrip trip_method;
    双纬度,经度;
    上下文语境;
    私有静态最后长MIN_DISTANCE_FOR_UPDATE = 10;
    私有静态最后长MIN_TIME_FOR_UPDATE = 1000 * 60 * 2;
    受保护的LocationManager的LocationManager;
    地点位置;    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){
        trip_method =新MyTrip();        this.context =背景;
        的LocationManager =(的LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
        Find_location();
        //对于我们定期任务,我们只显示一条消息
        Toast.makeText(背景下,我跑,Toast.LENGTH_SHORT).show();
    }    公共无效Find_location(){
        //位置nwLocation;
        尝试{
            位置=的getLocation(LocationManager.NETWORK_PROVIDER);
            的System.out.println(nwLocation -------+位置);
            如果(位置!= NULL){
                纬度= location.getLatitude();
                经度= location.getLongitude();
                的System.out.println(纬度-------+纬度+经度-----+经度);                Toast.makeText(
                        的背景下,
                        移动位置(NW):\\ nLatitude:+纬度
                            +\\ nLongitude:+经度,
                        Toast.LENGTH_LONG).show();
                //要在UI显示当前地址
                (新GetAddressTask())执行(位置);
            }其他{
                trip_method.showSettingsAlert(网络);
            }
        }赶上(例外五){
            e.printStackTrace();
        }
    }    公共场所的getLocation(字符串提供商){
        如果(locationManager.isProviderEnabled(供应商)){
            locationManager.requestLocationUpdates(供应商,
                    MIN_TIME_FOR_UPDATE,MIN_DISTANCE_FOR_UPDATE,这一点);
            如果(的LocationManager!= NULL){
                位置= locationManager.getLastKnownLocation(供应商);
                返回的位置;
            }
        }
        返回null;
    }
    / * // ** // *
     *下面是已用来获得的AsyncTask的子类
     *对应于给定纬度和放大器地址;经度。
     * // ** // ** /
    私有类GetAddressTask扩展的AsyncTask<位置,太虚,字符串> {        上下文mContext;        / *公共GetAddressTask(上下文的背景下){
            超();
            mContext =背景;
        } * /
        / * // ** // *
         *当任务完成后,onPostExecute()显示的地址。
         * // ** // ** /
        @覆盖
        保护无效onPostExecute(字符串地址){
            //在UI中显示的当前地址            Toast.makeText(context.getApplicationContext(),
                    解决---+地址,
                    Toast.LENGTH_LONG).show();
        }        @覆盖
        保护字符串doInBackground(位置... PARAMS){            字符串地址= NULL;
            字符串位置= NULL;
            地理codeR地理codeR;
            清单< android.location.Address>地址;
            地理codeR =新的地缘codeR(背景下,Locale.getDefault());
            尝试{
                地址=地理coder.getFromLocation(纬度,经度,1);                地址= addresses.get(0).getAddressLine(0)+,+ addresses.get(0).getAddressLine(1)+,+ addresses.get(0).getAddressLine(2);
                位置= addresses.get(0).getAddressLine(2);
                的System.out.println(位置----+区域);
               //这个位置的价值我要发送到另一个活动
            }赶上(IOException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }
            //返回文本
            退货地址;
           / *}其他{
                返回找不到地址;
            } * /
        }
    } // AsyncTask的类
}


解决方案

在您的code酌情使用这样的:

在你的第一类:

 意向意图=新意图(这一点,another_activity.class);
intent.putExtra(位置,Location.toString);
startService(意向)

在你的第二个类:

 字符串位置= getActivity()。getIntent.getStringExtra(位置)

现在你已经通过字符串从一类到另一个。使用在你需要它的位置的变量。

I am using the BroadcastReceiver in my application ,here i am receiving the current location and place in this class.hence i am getting the current location,hence my question is how to send this location string from this class to another class .i have to load this string data in the listview in another class so how can do this please help thanks in advance. My code is

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
}

解决方案

Use this in your code as appropriate:

In your First class:

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

In your Second class:

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

Now you have passed string from one class to another. Use the Location variable where you need it.

这篇关于如何从广播接收器中的数据得到另一个类广播接收器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆