Android 自定义广播实例以及系统广播(基础)
2017-02-07 22:00
465 查看
今天讲讲Android的广播,Android自定义广播分为有序广播和无序广播,我们今天讲无序广播和系统广播。
自定义广播有发送者和接受者,发送者只要设置名字和传递数据,接受者接收,接收者接收需要注册,可以在清单文件中注册也可以用代码动态注册。
先贴发送:
接收者写两个一个动态一个静态
静态
清单文件
动态动态接收和静态是一样的,所以这里只贴注册广播接受者了
好了,接下来讲讲系统的广播,今天就讲一个小例子吧,就是监听手机的电话状态。在这里你先要在清单文件中设置权限,就是允许读取手机状态的权限。
接下来是监听的小例子了
接着你就可以测试啦。
今天的分享就到这里啦,谢谢大家的观看!
自定义广播有发送者和接受者,发送者只要设置名字和传递数据,接受者接收,接收者接收需要注册,可以在清单文件中注册也可以用代码动态注册。
先贴发送:
package com.zking.sun.android_14_sender; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; public class MainActivity extends AppCompatActivity { private EditText et_main_content; private Intent intent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_main_content = (EditText) findViewById(R.id.et_main_content); intent = new Intent(); //设置广播的名字 intent.setAction("com.zking.sun.android_14_sender.923"); } public void send(View view){ String content = et_main_content.getText().toString(); //携带数据 intent.putExtra("data",content); //发送广播 无序 // sendBroadcast(intent); //发送黏性广播 粘性广播是解决动态广播退出不能接收的问题的,如要用需在清单文件中设置权限 (已过时 //<!--添加发送黏性广播的权限--> // <uses-permission android:name="android.permission.BROADCAST_STICKY"></uses-permission> sendStickyBroadcast(intent); } }
接收者写两个一个动态一个静态
静态
package com.zking.sun.android_14_received01; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; /** * Created by sun on 2017/2/7. */ public class Receive01 extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if("com.zking.sun.android_14_sender.923".equals(intent.getAction())){ String data = intent.getStringExtra("data"); Log.i("data","I'm receive one :"+data); } } }
清单文件
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.zking.sun.android_14_received01"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme= b2c9 "@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!--配置广播接受者: 1.在清单文件中配置(静态广播) 2.使用java 代码(动态广播) --> <receiver android:name=".Receive01"> <intent-filter> <action android:name="com.zking.sun.android_14_sender.923"></action> </intent-filter> </receiver> </application> </manifest>
动态动态接收和静态是一样的,所以这里只贴注册广播接受者了
package com.zking.sun.android_15_received02; import android.content.Intent; import android.content.IntentFilter; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { private Receive02 receive02; private IntentFilter intentFilter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); intentFilter = new IntentFilter(); receive02 = new Receive02(); intentFilter.addAction("com.zking.sun.android_14_sender.923"); } @Override protected void onResume() { super.onResume(); //注册 registerReceiver(receive02,intentFilter); } @Override protected void onDestroy() { super.onDestroy(); //注销 unregisterReceiver(receive02); } }
好了,接下来讲讲系统的广播,今天就讲一个小例子吧,就是监听手机的电话状态。在这里你先要在清单文件中设置权限,就是允许读取手机状态的权限。
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.zking.sun.android_15_phone"> <!--读取电话状态权限--> <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".Phone_State"> <intent-filter> <action android:name="android.intent.action.PHONE_STATE"></action> </intent-filter> </receiver> </application> </manifest>
接下来是监听的小例子了
package com.zking.sun.android_15_phone; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.telephony.TelephonyManager; import android.util.Log; /** * Created by sun on 2017/2/7. */ public class Phone_State extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if("android.intent.action.PHONE_STATE".equals(intent.getAction())){ //获取电话号码 String number = intent.getStringExtra("incoming_number"); Log.i("test","电话号码 :"+number); //获取电话状态 //电话管理者 TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); int state = tm.getCallState(); switch (state) { case TelephonyManager.CALL_STATE_RINGING: Log.i("test","女神来电话了"); break; case TelephonyManager.CALL_STATE_OFFHOOK: Log.i("test","女神正在和你通电话哦"); break; case TelephonyManager.CALL_STATE_IDLE: Log.i("test","女神挂电话并送了个香吻给你"); break; } } } }
接着你就可以测试啦。
今天的分享就到这里啦,谢谢大家的观看!
相关文章推荐
- Android 自定义广播实例以及系统广播(基础)(二)
- Android 自定义广播和系统广播(电话状态)(基础)
- android 发送自定义广播以及接收自定义广播
- android 传感器实例 以及 自定义可复用控件的使用
- Android系统应用开发(二)有条件的获取所有app以及自定义
- 【android所有系统广播总结以及用法】 and 【EventBus 3.0 的用法】
- Android 从0开始自定义控件之 自定义 View 基础实例(十)
- Android学习篇章29-Broadcast广播基础-自定义广播(静态广播)
- Android 发送系统广播与自定义广播
- salesforce 零基础学习(五十)自定义View或者List以及查看系统原来的View或者List
- 实例讲解Android中的View类以及自定义View控件的方法
- android 调用系统分享功能以及实现自定义分享
- 非常实用的android动态注册广播并发送以及接收实例
- android接收adb发送的系统广播及自定义广播
- Android种使用Notification实现通知管理以及自定义通知栏实例(示例四)
- [Android实例] 教你自定义menu,将系统menu替换之
- android 系统Style,Theme,以及自定义模板(declare-style)
- Android自定义广播实例
- 调用android系统的图库以及截图来让用户自定义界面背景
- Android自定义 4000 广播(Broadcast)与系统广播