如何将结果数据从广播接收器发送到活动

Bob*_*obs 10 android response broadcastreceiver android-activity

我有一个调用广播接收器的Activity.广播接收器等待并收听GPS.当监听器获得新点时,我想将该新点发送给Activity.如何将数据从广播接收器发送到活动?

我的Activity中需要一个监听器,等待来自Broadcast Receiver的响应.我怎样才能做到这一点?

小智 22

您可以通过活动呼叫接收器.如果您不想在活动中添加接收器的逻辑,则可以使用抽象接收器.

你抽象接收者:

public abstract class SmsReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
           //Add you receiver logic here
           ...
           ...
           onNewPosition();
        }

    protected abstract void onNewPosition();

}
Run Code Online (Sandbox Code Playgroud)

在您的活动中:

public class MyActivity extends Activity {

    private smsReceiver smsReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map_one_position);
        smsReceiver = new smsReceiver() {

            // this code is call asyncrously from the receiver
            @Override
            protected void onNewPosition() {
            //Add your activty logic here
            }

        };
        IntentFilter intentFilter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
        intentFilter.setPriority(999);
        this.registerReceiver(smsReceiver, intentFilter);

    }

     @Override
        protected void onPause() {
            super.onPause();
            this.unregisterReceiver(this.smsReceiver);
        }

}
Run Code Online (Sandbox Code Playgroud)

我希望它会对你有所帮助......

  • 你将如何实例化抽象类? (9认同)

Bob*_*obs 4

我为我的接收器定义了一个侦听器并在活动中使用它,它现在运行完美。以后有可能出现什么问题吗?

public interface OnNewLocationListener {
public abstract void onNewLocationReceived(Location location);
Run Code Online (Sandbox Code Playgroud)

}

在我的接收器类中,其名称为 ReceiverPositioningAlarm:

// listener ----------------------------------------------------

static ArrayList<OnNewLocationListener> arrOnNewLocationListener =
        new ArrayList<OnNewLocationListener>();

// Allows the user to set an Listener and react to the event
public static void setOnNewLocationListener(
        OnNewLocationListener listener) {
    arrOnNewLocationListener.add(listener);
}

public static void clearOnNewLocationListener(
        OnNewLocationListener listener) {
    arrOnNewLocationListener.remove(listener);
}

// This function is called after the new point received
private static void OnNewLocationReceived(Location location) {
    // Check if the Listener was set, otherwise we'll get an Exception when
    // we try to call it
    if (arrOnNewLocationListener != null) {
        // Only trigger the event, when we have any listener
        for (int i = arrOnNewLocationListener.size() - 1; i >= 0; i--) {
            arrOnNewLocationListener.get(i).onNewLocationReceived(
                    location);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的活动方法之一中:

OnNewLocationListener onNewLocationListener = new OnNewLocationListener() {
        @Override
        public void onNewLocationReceived(Location location) {
            // do something

            // then stop listening
            ReceiverPositioningAlarm.clearOnNewLocationListener(this);
        }
    };

    // start listening for new location
    ReceiverPositioningAlarm.setOnNewLocationListener(
            onNewLocationListener);
Run Code Online (Sandbox Code Playgroud)

  • 我担心你保留了强有力的参考资料,这可能会很糟糕 (6认同)