第一行代码Android学习(六)
2016-08-16 10:29
381 查看
第一行代码Android学习:第六部分主要涉及到接收系统广播、发送自定义广播、使用本地广播
DYHDM_05_00BroadcastTest
1.AndroidManifest.xml
2.activity_main.xml
3.BootCompleteReceiver.java
4.MainActivity.java
5.MyBoradcastReceiver.java
DYHDM_05_01BroadcastTest1
1.AndroidManifest.xml
2.MainActivity.java
3.AnotherBroadcastReceiver.java
代码下载地址
DYHDM_05_00BroadcastTest
1.AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.dyhdm_05_00broadcasttest" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.dyhdm_05_00broadcasttest.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".BootCompleteReceiver" > <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver> <receiver android:name=".MyBoradcastReceiver" > <intent-filter android:priority="100"> <action android:name="com.example.dyhdm_05_00broadcasttest.MY_BROADCAST" /> </intent-filter> </receiver> </application> </manifest>
2.activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <Button android:id="@+id/bt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="Send Broadcast" /> </RelativeLayout>
3.BootCompleteReceiver.java
/* * @Title: BootCompleteReceiver.java * @Description: TODO * @author: 张志安 * @date: 2016-8-16 上午9:07:07 * */ package com.example.dyhdm_05_00broadcasttest; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; /** * TODO 静态注册实现开机启动 需要修改AndroidManifest.xml * * <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> * <receiver android:name=".BootCompleteReceiver" > * <intent-filter> * <action android:name="android.intent.action.BOOT_COMPLETED" /> * </intent-filter> * </receiver> * * @author 张志安 * @date: 2016-8-16 上午9:07:07 */ public class BootCompleteReceiver extends BroadcastReceiver { /** * 重载方法 */ @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "Boot Complete", Toast.LENGTH_SHORT).show(); } }
4.MainActivity.java
package com.example.dyhdm_05_00broadcasttest; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.Bundle; import android.support.v4.content.LocalBroadcastManager; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { private IntentFilter intentFilter; private NetworkChangeReceiver networkChangeReceiver; private LocalBroadcastManager localBroadcastManager; private LocalReceiver localReceiver; private Button bt; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 本地广播 localBroadcastManager = LocalBroadcastManager.getInstance(this); bt = (Button) findViewById(R.id.bt); bt.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 发送标准广播 // sendBroadcast(new // Intent("com.example.dyhdm_05_00broadcasttest.MY_BROADCAST")); // 发送有序广播 // 在AndroidManifest.xml中加android:priority="100"优先级较高可以先收到广播 // sendOrderedBroadcast(new // Intent("com.example.dyhdm_05_00broadcasttest.MY_BROADCAST"),null); // 发送本地广播 localBroadcastManager .sendBroadcast(new Intent( "com.example.dyhdm_05_00broadcasttest.LOCAL_BROADCAST")); } }); // 动态注册监听网络变化 // intentFilter = new IntentFilter(); // intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE"); // networkChangeReceiver = new NetworkChangeReceiver(); // registerReceiver(networkChangeReceiver, intentFilter); // 本地注册监听网络变化 intentFilter = new IntentFilter(); intentFilter .addAction("com.example.dyhdm_05_00broadcasttest.LOCAL_BROADCAST"); localReceiver = new LocalReceiver(); localBroadcastManager.registerReceiver(localReceiver, intentFilter); } /** * 重载方法 */ @Override protected void onDestroy() { super.onDestroy(); // 取消广播接收器的注册 // unregisterReceiver(networkChangeReceiver); // 取消本地广播接收器的注册 localBroadcastManager.unregisterReceiver(localReceiver); } /** * TODO 网络状态改变的广播接收器 * * @author 张志安 * @date: 2016-8-16 上午9:45:43 */ class NetworkChangeReceiver extends BroadcastReceiver { /** * 重载方法 */ @Override public void onReceive(Context context, Intent intent) { ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connectivityManager .getActiveNetworkInfo(); if (networkInfo != null && networkInfo.isAvailable()) { Toast.makeText(context, "network is available", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(context, "network is unavailable", Toast.LENGTH_SHORT).show(); } } } /** * TODO 本地广播的广播接收器 * * @author 张志安 * @date: 2016-8-16 上午9:48:01 */ class LocalReceiver extends BroadcastReceiver { /** * 重载方法 */ @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "received local broadcast", Toast.LENGTH_SHORT).show(); } } }
5.MyBoradcastReceiver.java
/* * @Title: MyBoradcastReceiver.java * @Description: TODO * @author: 张志安 * @date: 2016-8-16 上午9:17:38 * */ package com.example.dyhdm_05_00broadcasttest; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; /** * TODO 联系发送广播需要用到的广播接收器 * @author 张志安 * @date: 2016-8-16 上午9:17:38 */ public class MyBoradcastReceiver extends BroadcastReceiver { /** * 重载方法 */ @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "received in MyBoradcastReceiver", Toast.LENGTH_SHORT).show(); //将广播截断 abortBroadcast(); } }
DYHDM_05_01BroadcastTest1
1.AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.dyhdm_05_01broadcasttest1" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.dyhdm_05_01broadcasttest1.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".AnotherBroadcastReceiver" > <intent-filter> <action android:name="com.example.dyhdm_05_00broadcasttest.MY_BROADCAST" /> </intent-filter> </receiver> </application> </manifest>
2.MainActivity.java
package com.example.dyhdm_05_01broadcasttest1; import android.app.Activity; import android.os.Bundle; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
3.AnotherBroadcastReceiver.java
/* * @Title: AnotherBroadcastReceiver.java * @Description: TODO * @author: 张志安 * @date: 2016-8-16 上午9:28:51 * */ package com.example.dyhdm_05_01broadcasttest1; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; /** * TODO 从另一个项目中接受广播 * * @author 张志安 * @date: 2016-8-16 上午9:28:51 */ public class AnotherBroadcastReceiver extends BroadcastReceiver { /** * 重载方法 */ @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "received in AnotherBoradcastReceiver", Toast.LENGTH_SHORT).show(); } }
代码下载地址
相关文章推荐
- 第一行代码学习笔记1——Android简介
- Android系统架构(Android第一行代码学习笔记1)
- Android第一行代码学习笔记四----数据存储
- 第二行代码学习笔记——第一章:开始启程——你的第一行Android代码
- Android第一行代码学习笔记九----高级技巧
- 第一行代码-Android 学习笔记(一)
- 【第一行代码-Android】学习(一)及在studio的迁移(4)intent学习
- Android第一行代码学习笔记五----内容提供器
- Android第一行代码学习笔记一----android架构
- Android 第一行代码(第二版)学习笔记
- 【第一行代码-Android】学习(一)及在studio的迁移(1)平台搭建
- 第一行代码Android学习(九)
- Android项目下目录分析(Android第一行代码学习笔记3)
- Android第一行代码学习笔记六----手机多媒体
- 第一行代码学习笔记-第一章 开始启程-1.android简介
- 【第一行代码-Android】学习(一)及在studio的迁移(2)创建活动及布局
- 第一行代码Android学习(五)
- 第一行代码Android学习(八)
- Android第一行代码学习笔记
- Android第一行代码学习笔记八----网络技术