欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-04-28 21:01:51
一、使用本地广播1.本地广播一个举例package com.example.broadcasttest2;import android.app.activity;import android.cont...

一、使用本地广播

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

到此这篇关于android 本地广播和强制下线功能的实现代码的文章就介绍到这了,更多相关android 本地广播和强制下线内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!