1. 程式人生 > 程式設計 >Android 本地廣播和強制下線功能的實現程式碼

Android 本地廣播和強制下線功能的實現程式碼

一、使用本地廣播

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
CSDN:https://blog.csdn.net/weixin_44630050

到此這篇關於Android 本地廣播和強制下線功能的實現程式碼的文章就介紹到這了,更多相關Android 本地廣播和強制下線內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!