Android学习笔记
参考《第一行代码 第二版》
关于广播:即系统之间相互传递消息
自定义广播即自定义消息类型。
一 创建广播接收者,接收广播。
在XML文件中注册广播接收者,注明自定义广播类型
二 使用Intent类创建实例调用构造方法初始化对象并传入广播类型(自定义)。
即 Intent i=new Intent(“c”);=A;
调用sendBroadcast()=B;方法并将初始化的对象传入即 B(A);
以上便完成了自定义广播的发送。
下面附上一个练习也是《第一行代码》书上的对应练习。
java部分代码
package com.example.jackson.receiver_3;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
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("e");
//添加自定义广播类型
sendBroadcast(intent);
//发送自定义广播
Toast.makeText(MainActivity.this,"自定义广播已发送",Toast.LENGTH_SHORT).show();
}
});
}
}
下面这UI部分的xml代码和界面截图;
<?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_3.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Bu_1"
android:text="发送自定义广播"
/>
</RelativeLayout>
我们创建了广播发送的工具,自然需要一个广播接收者。在这里我们使用静态注册广播接收者。
下面附上java代码和XML配置文件
//----------------------------使用静态注册广播接收者
package com.example.jackson.receiver_3;
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, "自定义广播1已收到", Toast.LENGTH_SHORT).show();
}
}
//-----------------------------以下是mxl配置文件的代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.jackson.receiver_3">
<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>
<!--添加需要接收的广播类型,在这里我们可以自定义内容-->
<action android:name="e"/>
</intent-filter>
</receiver>
</application>
</manifest>
在这里我们需要注意的是,我们在xml配置文件中添加的自定义接收广播的内容,应和在发送自定义广播时添加的广播类型,完全相同,这句话有点绕口。
简言之就是发送的信号要和接收的信号相同,这样才能接收成功。