Android开发 新建一个类继承BroadcastReceiver

public class WiFiDirectBroadcastReceiver extends BroadcastReceiver {
 
    private WifiP2pManager manager;
    private Channel channel;
   private WiFiDirectActivity activity;
 
public WiFiDirectBroadcastReceiver(WifiP2pManagermanager, Channel channel,
            WiFiDirectActivity activity) {
        super();
        this.manager = manager;
        this.channel = channel;
        this.activity = activity;
   }
 @Override
    public void onReceive(Context context, Intent intent) {
//添加实际处理代码
}
}

首先,WiFiDirectBroadcastReceiver继承于BroadcastReceiver,需要有onReceive方法的执行。

其次,需要对函数WiFiDirectBroadcastReceiver进行定义,函数定义中通过自定义参数,方便实例化过程中将activity中的必要参数传递进来

新建一个class执行callback时也具有类似的方法

例如:

public class Beamsend implements CreateNdefMessageCallback,
        OnNdefPushCompleteCallback {
	    NfcAdapter mNfcAdapter;
	    Beam activity;
public Beamsend(NfcAdapter mNfcAdapter, Beam activity){
	    	super();
	        this.mNfcAdapter = mNfcAdapter;
	        this.activity = activity;// Register callback to set NDEF message
	        
	        mNfcAdapter.setNdefPushMessageCallback(this, activity);
	        // Register callback to listen for message-sent success
	        mNfcAdapter.setOnNdefPushCompleteCallback(this, activity);
    }
}

    原文作者:勤睿
    原文地址: https://blog.csdn.net/qqqinrui/article/details/46582211
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞