Android的:如何使用LocalBroadcastManager [英] Android: How to use LocalBroadcastManager

查看:158
本文介绍了Android的:如何使用LocalBroadcastManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到答案如何在扩展类中使用 LocalBroadcastManager BaseAdapter 但毕竟做花了几个小时研究如何做,我没有找到正确的答案。我需要从片段调用我的方法适配器内部,但我不知道该怎么做。有人可以解释我,我应该怎么办呢?感谢您的答复和帮助。

I was trying to find answer how to use LocalBroadcastManager in class that extends BaseAdapter but after hours spent by doing research how to do it I didn't find right answer. I need to call method from Fragment inside my adapter, but I don't know how to do it. Can someone explain me how should I do it? Thank you for response and help.

推荐答案

我只能回答关于LocalBroadcastManager。

I can only answer about LocalBroadcastManager.


  1. 添加支持库到你的SDK,然后到你的Andr​​oid项目。

  2. 显然,导入支持库中的Java类,等等。

  3. 声明你LocalBroadcastManager和实例吧。此外,您还可以声明为静态,并用它在你的(整个)应用程序。所以,你不必再比如一个新的每个活动等。

  1. Add support library to your SDK and then to your Android project.
  2. Obviously, import the support library in your java class, etc.
  3. Declare your LocalBroadcastManager and instance it. Also, you can declare it static and use it throughout your (entire) app. So you don't have to instance a new one for every activity, etc.

public static LocalBroadcastManager mBroadcaster;
mBroadcaster = LocalBroadcastManager.getInstance(yourAppContextHere);


  • 在每一次活动,服务等,注册和注销根据每个生命周期的接收器。每个接收机可以有不同的过滤器。例如,在onResume(或的onCreate),并在onPause(或的onDestroy)::

  • In every activity, service, etc, register and unregister a Receiver according to each's lifecycle. Each receiver can have different filters. For example in onResume (or onCreate) and onPause (or onDestroy)::

    IntentFilter mFilter = new IntentFilter(MY_ACTION_1);
    mFilter.addAction(MY_ACTION_2);
    
    mBroadcaster.registerReceiver(localBluetoothReceiver, mFilter);
    mBroadcaster.unregisterReceiver(localBluetoothReceiver);
    

    和,最后,发送广播和与接收器接收

    And, finally, sending broadcasts and receiving with the receiver:

    Intent sendCmdIntent = new Intent("your.package.name.your.action.name");
    sendCmdIntent.putExtra(key, value);
    mBroadcaster.sendBroadcast(sendCmdIntent);
    
    
    private BroadcastReceiver localBluetoothReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            Do whatever depending on the action and on the extra stuff you put in the intent.
    


  • 所有这一切都是从内存报价,随意编辑它!

    All this is quoted from memory, feel free to edit it!

    这篇关于Android的:如何使用LocalBroadcastManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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