今天讲讲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="@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; } } }}接着你就可以测试啦。
今天的分享就到这里啦,谢谢大家的观看!
新闻热点
疑难解答