Android 本地广播和强制下线功能的实现代码

来自:网络
时间:2020-10-14
阅读:

一、使用本地广播

1.本地广播一个举例

package com.example.broadcasttest2;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
 
 private IntentFilter intentFilter;
 
 private LocalReceiver localReceiver;
 
 private LocalBroadcastManager localBroadcastManager;
 
 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);
 localBroadcastManager = LocalBroadcastManager.getInstance(this);//获取实例
 
 Button button = (Button) findViewById(R.id.button);
 button.setOnClickListener(new OnClickListener() {
 @Override
 public void onClick(View v) {
 //下面这行代码是用来全局广播的
// Intent intent = new Intent("com.example.broadcasttest.MY_BROADCAST");
 //下面这行代码是用来本地广播的(也就是app内部传递广播)
 Intent intent = new Intent("com.example.broadcasttest.LOCAL_BROADCAST");
 localBroadcastManager.sendBroadcast(intent);//发送本地广播 
// sendBroadcast(intent);
 //我们关闭上面一行代码,然后写另一种方法sendOrderedBroadcast,发送有序广播
 sendOrderedBroadcast(intent,null);
 }
 });
 intentFilter = new IntentFilter();
 intentFilter.addAction("com.example.broadcasttest.LOCAL_BROACAST");
 localReceiver = new LocalReceiver();
 localBroadcastManager.registerReceiver(localReceiver,intentFilter);//注册本地广播监听器
 
 
 }
 
 @Override
 protected void onDestroy() {
 super.onDestroy();
// unregisterReceiver(networkChangeReceiver);
 localBroadcastManager.unregisterRecerver(localReceiver);
 
 }
 
 class NetworkChangeReceiver extends BroadcastReceiver{
 @Override
 public void onReceive(Context context,Intent intent) {
 ConnectivityManager connectionMananger = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
 NetworkInfo networkInfo = connectionMananger.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();
 }
 
 }
 }
 
 class LocalReceiver extends BroadcastReceiver {
 @Override
 public void onReceive(Context context,Intent intent) {
 Toast.makeText(context,"received local broadcast",Toast.LENGTH_SHORT).show();
 }
 }
}

通过LocalBroadcastManagergetInstance()方法来获取一个实例,注册广播是用的registerReceiver()方法,传入的参数一个是localReceiver的内部实例,一个是IntentFilter实例。最后在onCreate方法中调用发送广播的方法。最后我们在按钮的点击事件里面发送一条com.example.broadcasttest.LOCAL_BROADCAST广播,然后在LocalReceiver里面去接收这条广播。

注意点:本地广播是无法通过静态注册的方式来接收的,其实这也完全可以理解,因为静态注册主要就是为了让程序在未启动的时候也能收到广播,而发送本地广播的时候,我们的程序就已经启动了,因为也完全不需要使用静态注册的功能。

2.使用本地广播的优势

(1)程序内部传递,防止机密数据泄露;
(2)其他程序无法将广播发送到我们的程序内部,防止安全漏洞的隐患;
(3)发送本地广播更加高效。

二、实现强制下线功能

1.实现强制下线的逻辑

无论在我们在程序的哪个活动界面,一旦收到强制下线的广播,就跳出来一个界面,必须点击“确认“,然后直接跳转到登录界面。

2.首先创建一个类用于管理所有的活动

package com.example.broadcastbestpractice;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;

public class ActivityCollector {
 
 public static List<Activity> activities = new ArrayList<Activity>();
 
 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();
 }
 }
 }
}

3.创建一个基类BaseActivity作为所有活动的父类

package com.example.broadcastbestpractice;

import android.app.Activity;
import android.os.Bundle;

public class BastActivity extends Activity {
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 ActivityCollector.addActivity(this);
 }
 
 @Override
 public void onDestroy() {
 super.onDestroy();
 ActivityCollector.removeActivity(this);
 }
}

4.接着需要建立一个登录页面的布局

<?xml version="1.0" encoding="utf-8"?>
<!-- 
 android:shrinkColumns设置允许被收缩的列序号
 android:strectColumns设置允许被拉伸的列序号 -->
<TableLLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:strectColumns = "1" >
 
 <TableRow>
  <TextView
   android:layout_height = "wrap_content"
   android:text="Account:" />
  
  <EditText
   android:id="@+id/account"
   android:layout_height="wrap_content"
   android:hint="Input your account" />  
 </TableRow>
 
 <TableRow>
  <TextView
   android:layout_height="wrap_content"
   android:text="Password:" />
  
  <EditTest
   android:id="@+id/password"
   android:layout_height="wrap_content"
   android:inputType="textPassword" />  
 </TableRow>
 
 <TableRow>
  <Button
   android:id="@+id/login"
   android:layout_height="wrap_content"
   android:layout_span="2"
   android:text="Login" />
 </TableRow>

</TableLayout>

后面的我们下次连载接着写。

三、源码:

BroadcastTest2
BroadcastBestPractice
https://github.com/ruigege66/Android/tree/master/BroadcastTest2
https://github.com/ruigege66/Android/tree/master/BroadcastBestPractice

返回顶部
顶部