Android学习笔记
参考《第一行代码 第二版》
关于广播:即系统之间相互传递消息
有了接收者自然也存在发送者。即广播的发送。
广播发送的两种方法
发送标准广播
标准广播即给需要接收该广播类型的,所有广播接收者,进行发送,他们即广播接收者,几乎同时收到广播。
发送与自定义广播的发送相同这里就不在赘述。
发送有序广播
有序广播即对接收广播的广播接收者进行先后顺序的区分,让他们有序的接收广播。
一建立广播接受者
切记要进行注册,并写入接听的广播类型。
二创建Intent()对象的实例,使用构造函数进行初始化对象,在此传入想要发送的广播类型。(与XML注册文件类型相同)
三调用sendOrdeBroadacst()方法(只需记住send+O+B);
四进行有序广播的编写,即进行优先度的考虑。
在XML注册文件中队形的广播接收者中进行写入<intent-filter android:priority="100">,100代表对优先级的描述。
完场以上四步,即可发送并接受自定义的有序广播类型。
有序广播的发送和标准广播的发送大致类似。仅仅是发送广播的方法不同,和要对XML注册文件进行优先级的考虑与编写。
附加对广播信息的拦截,仅适用于有序广播
PS:abortBroadacst()使用广播接收者调用该方法,对收到的广播进行拦截。使其他广播接收者无法接受该广播
下面附上一个练习也是《第一行代码》书上的对应练习。
首先建立静态广播接收者
package com.example.jackson.receiver_4;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class MyReceiver extends BroadcastReceiver {
public MyReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"发送自定义广播",Toast.LENGTH_SHORT).show();
//使用广播来接技术,代码如下
//-------------------abortBroadcast();
}
}
其次在主类实现,发送有序广播;
package com.example.jackson.receiver_4;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=(Button) findViewById(R.id.bu_1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent("c");
sendOrderedBroadcast(intent,null);
}
});
;
}
}
xml配置文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.jackson.receiver_4">
<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=".MyReceiver"
android:enabled="true"
android:exported="true">
<!--设置级别,和接收广播类型-->
<intent-filter android:priority="100">
<action android:name="c"/>
</intent-filter>
</receiver>
</application>
</manifest>
最后是UI界面代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.jackson.receiver_4.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bu_1"
android:text="发送有序广播"/>
</RelativeLayout>