---《一周快速上手Kotlin For Android》简介
目前Kotlin已正式成为Android的官方语言,作为Android开发者来说,学习和了解Kotlin也是属于理所当然的事情,兴许你觉得Java对于你来说才是真正的开发”利器”,使用Java你能发挥更高的效率,当然,如果如此你还是可以继续使用Java来开发Android。但对于Kotlin来说,开发Android会变得更简单一些,因为Kotlin其实就是Android开发中的语法糖,采用更简短的代码,描述更多的逻辑而已。开发语言本身就没有对错和最优,每个开发语言被开发出来的时代背景不同,需要解决的主要问题也不同。选择适合你自己的就可以。
BroadcastReceiver工作过程
Java 发送广播:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn_send = findViewById(R.id.btn_send);
btn_send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction("ACTION_TEST");
intent.putExtra("VALUE", 123);
sendBroadcast(intent);
}
});
}
}
Kotlin 发送广播:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val btn_send = findViewById<Button>(R.id.btn_send)
btn_send.setOnClickListener {
val intent = Intent()
intent.action = "ACTION_TEST"
intent.putExtra("VALUE", 123)
sendBroadcast(intent)
}
}
}
Java接收广播:
public class Main2Activity extends AppCompatActivity {
private static final String TAG = "Main2Activity";
private MyReceiver myReceiver;
private IntentFilter intentFilter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Log.d(TAG, "onCreate");
myReceiver = new MyReceiver();
intentFilter = new IntentFilter();
intentFilter.addAction("ACTION_TEST");
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
@Override
protected void onResume() {
super.onResume();
registerReceiver(myReceiver, intentFilter);
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(myReceiver);
}
private class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("ACTION_TEST")){
int value = intent.getIntExtra("VALUE", 0);
Log.i(TAG, "onReceive value: " + value);
}
}
}
}
Kotlin接收广播:
class Main2Activity : AppCompatActivity() {
private var myReceiver: MyReceiver? = null
private var intentFilter: IntentFilter? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
Log.d(TAG, "onCreate")
myReceiver = MyReceiver()
intentFilter = IntentFilter()
intentFilter!!.addAction("ACTION_TEST”)
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
}
override fun onResume() {
super.onResume()
registerReceiver(myReceiver, intentFilter)
}
override fun onDestroy() {
super.onDestroy()
unregisterReceiver(myReceiver)
}
private inner class MyReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == "ACTION_TEST") {
val value = intent.getIntExtra("VALUE", 0)
Log.i(TAG, "onReceive value: " + value)
}
}
}
companion object {
private val TAG = "Main2Activity"
}
}