Android调用lib模块中类文件内的接口

时间:2016-09-19 09:28:33

标签: android interface

嗨,在我的lib模块中,我有这样的界面,

public interface ServiceData {
    void deviceName();
}

我在Activity中的Main app模块中实现了这个,如下所示,

MainActivity extends AppCompatActivity implements ServiceData

    @Override
    public void deviceName() {

    }

}

我必须在同样位于lib模块的类文件中调用它。我搜索并找到了像

这样的解决方案
ServiceData service = (ServiceData) context;

但它会抛出ClassCastException。如何在类文件中调用它?任何帮助将不胜感激,并提前感谢。

1 个答案:

答案 0 :(得分:1)

使用localbroadcastmanager  在lib

意图意图=新意图(" YOUR_INFO");

LocalBroadcastManager manager = LocalBroadcastManager.getInstance(context);
manager.sendBroadcast(intent);

In app.

IntentFilter intentFilter = new IntentFilter("YOUR_INFO");

LocalBroadcastManager manager = LocalBroadcastManager.getInstance(this);
manager.registerReceiver(broadcastReceiver, intentFilter);

private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        Log.d("MainActivity","your info" +"  "+action);
        if(action.equals("YOUR_INFO")) {

        }
    }
};