本系列将使用 Android Studio 将《第一行代码》(书中讲解案例使用Eclipse)刷一遍,旨在为想入坑 Android 开发,并选择 Android Studio 作为开发 IDE 的同学开路。
PS:其实我就是现学现卖,希望我能坚持刷完。
系列目录在此:
Android 内置了很多系统级别的广播,通过监听这些广播可以得到相关的系统状态信息。例如:
下面是监听网络变化的例子,新建一个 BroadcastTest 项目。
class NetworkChangeReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent){ Toast.makeText(context, "network changes", Toast.LENGTH_SHORT).show(); } }
private IntentFilter intentFilter; private NetworkChangeReceiver networkChangeReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); intentFilter = new IntentFilter(); intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE"); networkChangeReceiver = new NetworkChangeReceiver(); registerReceiver(networkChangeReceiver, intentFilter); }
@Override protected void onDestroy(){ super.onDestroy(); unregisterReceiver(networkChangeReceiver); }
下面是监听系统启动的例子。
public class BootCompleteReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent){ Toast.makeText(context, "Boot complete", Toast.LENGTH_SHORT).show(); } }
<receiver android:name=".MainActivity$BootCompleteReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"/> </intent-filter> </receiver>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
下面是在程序中发送自定义广播并接收的例子。
public static class MyBroadcastReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent){ Toast.makeText(context, "received in MyBroadcastReceiver", Toast.LENGTH_SHORT).show(); } }
<receiver android:name=".MainActivity$MyBroadcastReceiver"> <intent-filter> <action android:name="com.example.broadcasttest.MY_BROADCAST"/> </intent-filter> </receiver>
让 MyBroadcastReceiver 接收一条 com.example.broadcasttest.MY_BROADCAST
的广播。
Button button = (Button)findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent("com.example.broadcasttest.MY_BROADCAST"); sendBroadcast(intent); } });
前面用到的都是全局广播,可以被其他任何程序接收到,会引起一些安全性问题。下面是一个发送接收本地广播的例子。
public static class LocalReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent){ Toast.makeText(context, "received local broadcast", Toast.LENGTH_SHORT).show(); } }
private IntentFilter intentFilter; private LocalReceiver localReceiver; private LocalBroadcastManager localBroadcastManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); localBroadcastManager = localBroadcastManager.getInstance(this); Button button = (Button)findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent("com.example.broadcasttest.LOCAL_BROADCAST"); localBroadcastManager.sendBroadcast(intent); } }); intentFilter = new IntentFilter(); intentFilter.addAction("com.example.broadcasttest.LOCAL_BROADCAST"); localReceiver = new LocalReceiver(); localBroadcastManager.registerReceiver(localReceiver, intentFilter); }
@Override protected void onDestroy(){ super.onDestroy(); localBroadcastManager.unregisterReceiver(localReceiver); }
PS:本地广播无法通过静态注册的方式接收。
最近家里事情太多,除我之外的大人小孩都生病了。有大病也有小病,反正就是各种跑医院,深深的感觉到健康的重要。
附送段子一个:
今天有人问我去过高消费场所么?
我想了想问他:“医院算么?”
尽管我试图在本文中尽量完整的进行描述,但受限于篇幅和我的个人水平,本文难免有所遗漏,欢迎在评论中指出。