继承在 Android 中是如何工作的?
Android 没有什么特别之处。它完全按照应有的方式工作。
例子?
让我们创建一个BaseActivity
监听BroadcastReceiver
(SCAN_RESULTS_AVAILABLE)。
所以常见的功能是,
- 注册
BroadcastReceiver
一个onCreate()
BroadcastReceiver
取消注册onDestroy()
基类:
public class BaseActivity extends AppCompatActivity{
private WifiScanReceiver wifiScanReceiver;
@Override
public void onCreate(Bundle sis)
{
super.onCreate(sis);
// Don't call setContentView() because no layout is needed
}
@Override
public void onDestroy(){
super.onDestroy();
unregisterReceiver(wifiScanReceiver);
}
private void registerMyReceiver(){
wifiScanReceiver = new WifiScanReceiver();
registerReceiver(wifiScanReceiver, new
IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
manager.startScan();
}
}
儿童班:
public class BlahBlahActivity extends BaseActivity{
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState); // this will call BaseActvity method which will call AppcompatActivty method
setContentView(R.layout.yourlayout);
}
}