Android強(qiáng)制下線功能實(shí)現(xiàn)的代碼示例
強(qiáng)制下線的應(yīng)用場(chǎng)景很多, 比如我們好多賬號(hào)都有搶登的顯現(xiàn), 會(huì)被擠下線.
詳細(xì)實(shí)現(xiàn):
其實(shí)實(shí)現(xiàn)強(qiáng)制下線功能的思路也比較簡(jiǎn)單, 只需要在界面彈出一個(gè)對(duì)話框, 讓用戶無(wú)法進(jìn)行其他操作, 必須點(diǎn)擊對(duì)話框中的確定按鈕, 然后回到登錄界面即可.
但是又有一個(gè)問題, 我們通知用戶強(qiáng)制下線, 用戶可能正處于任何一個(gè)界面, 難道需要在每個(gè)界面上都編寫一個(gè)彈出對(duì)話框的邏輯? 當(dāng)然不是! 我們可以借助本章中所學(xué)的廣播知識(shí)來(lái)輕松實(shí)現(xiàn)這一功能.
public class ActivityConllector { public static List<Activity> 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(); } } }
創(chuàng)建BaseActivity類作為所有活動(dòng)的父類, 代碼如下:
public class BaseActivity extends AppCompatActivity{ @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); ActivityConllector.addActivity(this); } @Override protected void onDestroy() { super.onDestroy(); ActivityConllector.removeActivity(this); } }
首先編寫布局文件activity_login文件:
<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:text="User Name" android:textSize="18sp" android:layout_gravity="center_vertical"/> <EditText android:id="@+id/account" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_gravity="center_vertical" /> </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:text="Password" android:textSize="18sp" android:layout_gravity="center_vertical"/> <EditText android:id="@+id/password" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_gravity="center_vertical" android:inputType="textPassword"/> </LinearLayout> <Button android:id="@+id/login" android:layout_width="match_parent" android:layout_height="60dp" android:text="Login"/> </LinearLayout>
LoginActivity.java中代碼:
public class LoginActivity extends BaseActivity implements View.OnClickListener{ private EditText userName; private EditText passWord; private Button login; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); userName = (EditText)findViewById(R.id.account); passWord = (EditText)findViewById(R.id.password); login = (Button) findViewById(R.id.login); login.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.login: String username = userName.getText().toString(); String password = passWord.getText().toString(); if(username.equals("admin") && password.equals("123")){ Intent intent = new Intent(LoginActivity.this, MainActivity.class); startActivity(intent); finish(); } else { Toast.makeText(this, "用戶名或者密碼錯(cuò)誤!", Toast.LENGTH_SHORT).show(); } } } }
我們這時(shí)可以理解成, 登錄界面之后, 進(jìn)入的activity_main.xml就是主界面. 主界面沒有其他功能, 只有一個(gè)用于顯示的TextView和一個(gè)用作下線功能的按鈕
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="這是主頁(yè)面" android:layout_gravity="center_horizontal"/> <Button android:id="@+id/force_offline" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="強(qiáng)制下線"/> </LinearLayout>
MainActivity.java中代碼:
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 v) { Intent intent = new Intent("com.junzaivip.broadcastbestpractice.FORCE_OFFLINE"); sendBroadcast(intent); } }); } }
我們按鈕的點(diǎn)擊事件里面發(fā)送了一條廣播, 廣播的值為com.junzaivip.broadcastbestpractice.FORCE_OFFLINE, 這條廣播就是用于通知程序強(qiáng)制用戶下線的.
也就是說強(qiáng)制用戶下線的邏輯并不是寫在MainActivity里的, 而是應(yīng)該寫在接收這條廣播的接收器里面. 這樣的好處是, 強(qiáng)制下線的功能就不會(huì)依附于任何的界面, 不管是在程序的任何地方, 只需要發(fā)出一條這樣的廣播,就可以完成強(qiáng)制下線的操作了.
那么, 需要?jiǎng)?chuàng)建一個(gè)廣播接收器, 用于接收這條強(qiáng)制下線的廣播, 唯一的問題就是, 應(yīng)該在哪里創(chuàng)建呢? 由于廣播接收器里面需要彈出一個(gè)對(duì)話框來(lái)阻塞用戶的正常操作. 但如果創(chuàng)建的是一個(gè)靜態(tài)注冊(cè)的廣播接收器, 是沒有辦法在onReceive()方法里彈出對(duì)話框這樣的UI控件的, 而我們顯然不能也不可能在每個(gè)活動(dòng)中注冊(cè)一個(gè)動(dòng)態(tài)的廣播接收器.
那么到底應(yīng)該怎么辦呢? 其實(shí)很明顯, 只需要在BaseActivity中動(dòng)態(tài)注冊(cè)一個(gè)廣播接收器就可以了. 因?yàn)樗械幕顒?dòng)都繼承自BaseActivity的.
BaseActivity中的代碼:
public class BaseActivity extends AppCompatActivity{ @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); ActivityConllector.addActivity(this); } @Override protected void onDestroy() { super.onDestroy(); ActivityConllector.removeActivity(this); } class ForceOffLineReceiver extends BroadcastReceiver{ @Override public void onReceive(final Context context, final Intent intent) { AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle("warning"); builder.setMessage("您被強(qiáng)制下線, 請(qǐng)您重新登錄!"); builder.setCancelable(false); // 將對(duì)話框設(shè)置為不可取消 // 給按鈕添加注冊(cè)監(jiān)聽 builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 點(diǎn)擊按鈕所調(diào)用的方法 ActivityConllector.finishAll();//銷毀所有的活動(dòng) Intent intent1 = new Intent(context, LoginActivity.class); context.startActivity(intent); } }); builder.show(); } } }
我們需要設(shè)置主活動(dòng)為L(zhǎng)oginActivity, 而不再是MainActivity, 模擬訪問一個(gè)程序首先在登錄頁(yè)面.
修改AndroidManifest.xml文件:
<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>
運(yùn)行效果如下:
源碼: BroadcastBestPractice_jb51.rar
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解Android 基于TCP和UDP協(xié)議的Socket通信
這篇文章主要介紹了詳解Android 基于TCP和UDP協(xié)議的Socket通信,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2017-11-11Android實(shí)現(xiàn)3D層疊式卡片圖片展示
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)3D層疊式卡片圖片展示,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10Android scrollToTop實(shí)現(xiàn)點(diǎn)擊回到頂部(兼容PullTorefreshScrollview)
當(dāng)頁(yè)面滑動(dòng)到底部,出現(xiàn)回到頂部的按鈕相信對(duì)大家來(lái)說并不陌生,下面這篇文章主要介紹了關(guān)于Android scrollToTop實(shí)現(xiàn)點(diǎn)擊回到頂部,并兼容PullTorefreshScrollview的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒。2017-03-03Android編程實(shí)現(xiàn)換膚功能實(shí)例
這篇文章主要介紹了Android編程實(shí)現(xiàn)換膚功能的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android換膚的具體步驟與相關(guān)技巧,需要的朋友可以參考下2015-12-12Android之解析JSON數(shù)據(jù)示例(android原生態(tài),F(xiàn)astJson,Gson)
本篇文章主要介紹了Android之解析JSON數(shù)據(jù)示例,主要使用android原生態(tài)代碼解析,F(xiàn)astJson,Gson三種方法,有興趣的可以了解一下。2017-02-02Android 代碼寫控件代替XML簡(jiǎn)單實(shí)例
這篇文章主要介紹了Android 代碼寫控件代替XML簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-05-05Android添加圖片到ListView或者RecyclerView顯示
這篇文章主要介紹了Android添加圖片到ListView或者RecyclerView顯示的相關(guān)資料,需要的朋友可以參考下2016-08-08Android實(shí)現(xiàn)的仿淘寶購(gòu)物車demo示例
這篇文章主要介紹了Android實(shí)現(xiàn)的仿淘寶購(gòu)物車demo示例,結(jié)合實(shí)例形式分析了Android購(gòu)物車的功能、布局及邏輯實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-07-07