Android的广播机制非常灵活,Android的每一个应用程序都可以对自己感兴趣的广播进行注册,这样该程序就只会接收到自己所关心的广播内容,这些广播可能是来自系统的,也可能是来自于其他应用程序的。
Android提供了一套完整的API,允许应用程序自由地发送和接收广播。发送广播借助Intent,而接收广播的方法利用广播接收器Broadcast Receiver。
Android 内置了很多系统级别的广播,我们可以在应用程序中通过监听这些广播来得到各种系统的状态信息。比如手机开机完成后会发出一条广播,电池的电量发生变化会发出一条广播,时间或时区发生改变也会发出一条广播等。而想要接收到这些广播,需要使用广播接收器。
public class MainActivity extends AppCompatActivity {
private IntentFilter intentFilter;
private NetworkChangeReceiver networkChangeReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intentFilter = new IntentFilter();
intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
networkChangeReceiver = new NetworkChangeReceiver();
registerReceiver(networkChangeReceiver, intentFilter);
}
class NetworkChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "network changes", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(networkChangeReceiver);
}
}
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();
}
}
}
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
动态注册的广播接收器虽然可以自由地控制注册和注销,但是必须在程序启动之后才能接收到广播。如果想让程序在未启动的情况下就能接收到广播,就需要静态注册了。现在我们让程序接收一条开机广播,当收到这个条广播时就可以在onReceive()方法里执行相应的逻辑,从而实现开机启动的功能。
public class BootCompleteReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"Boot Complete",Toast.LENGTH_LONG).show();
}
}
<receiver
android:name=".BootCompleteReceiver"
android:enabled="true"
android:exported="true">receiver>
* 说明:在标签内出现了一个新的标签,所有静态的广播接收器都是在这里进行注册的。它的用法其实和标签很相似,都是通过android:name来指定具体注册哪一个广播接收器,而 enaled和exported属性则是根据我们刚才勾选的状态自动生成的。
<receiver
android:name=".BootCompleteReceiver"
android:enabled="true"
android:exported="true">receiver>
<receiver
android:name=".BootCompleteReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
intent-filter>
receiver>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
以上学习的是系统广播,现在我们学习如何在程序中发送自定义广播。广播分为标准广播和有序广播,本节我们就将通过实践的方式来看一下这两种广播具体的区别。
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"received in MyBroadcastReceiver",Toast.LENGTH_SHORT).
show();
}
}
<receiver
android:name=".MyBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.beidou.broadcasttest.MY_BROADCAST">
action>
intent-filter>
receiver>
* 这里让MyBroadcastReceiver接收一条值为com.beidou.broadcasttest.MY_BROADCAST的广播,因此待会在发送广播的时候,我们需要发出这样的一条广播。
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent("com.beidou.broadcasttest.MY_BROADCAST");
sendBroadcast(intent);
}
});
* 在点击事件中,我们加入了发送自定义广播的逻辑。
1. 首先构建出了一个Intent对象,并把要发送的广播的值传入。
2. 然后调用了Context的sendBroadcast方法将广播发送出去,这样所有监听com.com.beidou.broadcasttest.MY_BROADCAST这条广播的广播接收器就会受到信息。
3. 由于广播是使用Intent进行传递的,因此你还可以在Intent中携带数据传递给广播接收器。
1.新创建 BroadcastTest2 项目,点击AS-File-New-New Project进行创建.
2.新建AnotherBroadcastReceiver,代码:
public class AnotherBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"received in AnotherBroadcastReceiver",
Toast.LENGTH_SHORT).show();
}
}
3.修改配置文件
<receiver
android:name=".AnotherBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.beidou.broadcasttest.MY_BROADCAST">action>
intent-filter>
receiver>
4.证明了应用程序发出的广播是可以被其他的程序接收到的。
Intent intent = new Intent("com.beidou.broadcasttest.MY_BROADCAST");
sendOrderedBroadcast(intent,null);
1.发送有序广播只需改动一行代码,即将sendBroadcast()方法改成sendOrderedBroadcast方法。接收两个参数,第一个参数仍然是Intent,第二个参数是一个与权限相关的字符串,这里传入null即可。
2.重新运行程序,两个应用程序都可以接收到这条广播,但是这个时候的广播接收器是有先后顺序的,而且前面的广播接收器还可以将广播截断,以阻止其继续广播。
3.设定广播接收器的先后顺序,在配置文件里面
<receiver
android:name=".MyBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="100">
<action android:name="com.beidou.broadcasttest.MY_BROADCAST">action>
intent-filter>
receiver>
* 通过android:priority=”100”,给MyBroadcastReceiver的优先级设成了100,以保证它一定会在AnotherBroadcastReceiver之前收到广播.
4. MyBroadcastReceiver获得了接收广播的优先权,那么MyBroadcastReceiver就可以选择是否允许广播继续传递了。修改MyBroadcastReceiver代码:
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"received in MyBroadcastReceiver",Toast.LENGTH_LONG).
show();
abortBroadcast();
}
}
* 在onReceive方法中调用了abortBroadcast方法,就表示将这条广播截断,后面的广播接收器将无法再接收到这条广播。
前面我们发送和接收的广播全部属于系统全局广播,即发出的广播既可以被其他任何应用程序接收到,并且我们可以接收到来自于其他任何应用程序的广播。这样就存在安全性问题。
比如:我们发送的一些携带关键性数据的广播有可能被其他应用程序截获,或者其他的程序不停地向我们的广播接收器里发送各种垃圾广播。
为此,Android引入了一套本地广播机制:使用这个广播机制发出的广播只能够在应用程序的内部进行传递,并且广播接收器也只能接收来自本应用程序发出的广播。
public class MainActivity extends AppCompatActivity {
private IntentFilter intentFilter;
private LocalReceiver localReceiver;
private LocalBroadcastManager localBroadcastManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
localBroadcastManager = LocalBroadcastManager.getInstance(this); //获取实例
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent("com.beidou.broadcasttest.LOCAL_BROADCAST");
localBroadcastManager.sendBroadcast(intent);//发送广播
}
});
intentFilter = new IntentFilter();
intentFilter.addAction("com.beidou.broadcasttest.LOCAL_BROADCAST");
localReceiver = new LocalReceiver();
localBroadcastManager.registerReceiver(localReceiver, intentFilter);//注册广播
}
class LocalReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "received local broadcast", Toast.LENGTH_SHORT).
show();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
localBroadcastManager.unregisterReceiver(localReceiver);//注销广播
}
}
强制下线功能比较常见:比如QQ在别处登录了,就会将你强制挤下线。
逻辑:只需要在界面上弹出一个对话框,让用户无法进行任何其他操作,必须要点击对话框中的确定按钮,然后回到登录界面即可。
疑问:当我们被通知需要强制下线时可能正处于任何一个界面,难道需要在每个界面上都编写一个弹出对话框的逻辑?那到不用,借助广播知识,可以轻松实现这一个功能。
public class ActivityCollector {
public static List activities = new ArrayList<>();
public static void addActivity(Activity activity) {
activities.add(activity);
}
public static void removeActivity(Activity activity) {
activities.remove(activity);
}
public static void finishAll() {
for (Activity activity : activities) {
if (!activity.isFinishing()) {
activity.finish();
}
}
}
}
public class BaseActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityCollector.addActivity(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
ActivityCollector.removeActivity(this);
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal">
<TextView
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Account:"
android:textSize="18sp" />
<EditText
android:id="@+id/account"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1" />
LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal">
<TextView
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Password:"
android:textSize="18sp" />
<EditText
android:id="@+id/password"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:inputType="textPassword" />
LinearLayout>
<Button
android:id="@+id/login"
android:layout_width="match_parent"
android:layout_height="60dp"
android:text="login" />
LinearLayout>
public class LoginActivity extends BaseActivity {
private EditText accountEdit;
private EditText passwordEdit;
private Button login;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
accountEdit = (EditText) findViewById(R.id.account);
passwordEdit = (EditText) findViewById(R.id.password);
login = (Button) findViewById(R.id.login);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String account = accountEdit.getText().toString();
String password = passwordEdit.getText().toString();
//如果账号是admin 且密码是123456,就认为登录成功
if (account.equals("admin") && password.equals("123456")) {
startActivity(new Intent(LoginActivity.this, MainActivity.class));
finish();
} else {
Toast.makeText(LoginActivity.this,
"account or password is invalid", Toast.LENGTH_LONG).show();
}
}
});
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/force_offline"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send force offline broadcast" />
LinearLayout>
public class MainActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button forceoffline = (Button) findViewById(R.id.force_offline);
forceoffline.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent("com.beidou.broadcastbestpractice.
FORCE_OFFLINE");
sendBroadcast(intent);
}
});
}
}
public class BaseActivity extends AppCompatActivity {
private ForceOfflineReceiver receiver;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityCollector.addActivity(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
ActivityCollector.removeActivity(this);
}
@Override
protected void onResume() {
super.onResume();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.beidou.broadcastbestpractice.FORCE_OFFLINE");
receiver = new ForceOfflineReceiver();
registerReceiver(receiver,intentFilter);
}
@Override
protected void onPause() {
super.onPause();
if (receiver!=null){
unregisterReceiver(receiver);
receiver = null;
}
}
class ForceOfflineReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Warning");
builder.setMessage("You are forced to be offline.
Please try to login again");
builder.setCancelable(false);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
ActivityCollector.finishAll(); //销毁所有的活动
Intent intent = new Intent(context,LoginActivity.class);
context.startActivity(intent);//重新启动LoginActivity
}
});
builder.show();
}
}
}
<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">activity>
<activity android:name=".LoginActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
intent-filter>
activity>
application>
* 综上:当账号和密码正确的时候调转到主页面MainActivity,在主页面里面发出一个广播(BaseActivity里面已经加入了注册和取消广播的代码),所以广播发出后,BaseActivity里面的广播接收器就会接收到广播,与此同时弹出对话框,点击OK,进行强制所有活动下线的操作,并重新打开登录页面。
1.配置身份
git config –global user.name “FUkaiqiang”
git config –global user.email “[email protected]
2.验证身份
git config –global user.name
git config –global user.emai
3.创建代码仓库(进入项目存放的目录)
cd D:
cd Test
cd BroadcastBestPractice
git init
* 此时会生成一个.git文件夹,用来记录所有的Git操作的
4.查看列表
ls -al
5. 提交本地代码(先用add把想要的代码先添加进来,而conmit则是真正地去执行提交操作)
提交单个文件 git add build.gradle
提交整个目录 git add .
提交:git commit -m “First commit” (m不可少)