1. Define Interface
public interface NetEvevt {
public void onNetChange(int netMobile);
}
2. Implement a callback interface
public class MainActivity extends AppCompatActivity implements NetEvevt{
// 定义静态变量
public static NetEvevt evevt;
CustomerReceiver customerReceiver=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 赋值给变量
evevt=this;
if(customerReceiver==null){
customerReceiver=new CustomerReceiver();
IntentFilter filter = new IntentFilter("com.denganzhi.greendao.CustomerReceiver.action");
registerReceiver(customerReceiver, filter);
}
}
// 实现接口,重写方法
@Override
public void onNetChange(int netMobile) {
Toast.makeText(MainActivity.this,"netMobile:"+netMobile,Toast.LENGTH_SHORT).show();
}
public void methodClick(View view){
Intent intent = new Intent("com.denganzhi.greendao.CustomerReceiver.action");
sendBroadcast(intent);
}
@Override
protected void onDestroy() {
super.onDestroy();
if(customerReceiver!=null){
unregisterReceiver(customerReceiver);
customerReceiver=null;
}
}
}
3. broadcast transmission
CustomerReceiver the extends the BroadcastReceiver {class public
public NetEvevt evevt = MainActivity.evevt;
@Override
public void the onReceive (the Context context, the Intent Intent) {
// the TODO: This Method The IS Called When Receiving the BroadcastReceiver IS
// Broadcast AN the Intent.
// Toast. makeText (context, "receive", Toast.LENGTH_LONG) the .Show ();
// get a class implementation method, a method invocation
IF (evevt = null!) {
evevt.onNetChange (110);
}
}
// custom interface
public interface NetEvevt {//
// public void onNetChange (int netMobile);
//}
}