Android實現(xiàn)強制下線功能的示例代碼
一、回顧
- 上次連載寫了兩個類,一個類
ActivityCollector.java用于管理所有的活動;一個類是BaseActivity.java作為所有活動的父類; - 還有一個放在layout目錄中的登錄界面
login.xml
二、登錄頁面的活動
接下來寫一個登錄頁面的活動,繼承自BaseActivity.java
package com.example.broadcastbestpractice;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class LoginActivity extends BaseActivity{
private EditText accountEdit;
private EditText passwordEdit;
private Button login;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
accountEdit = (EditText)findViewById(R.id.account);
passwordEdit = (EditText)findViewById(R.id.password);
login = (Button) findViewById(R.id.login);
login.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
String account =accountEdit.getText().toString();
String password = passwordEdit.getText().toString();
//如果賬號是admin,密碼是12345,就認(rèn)為登錄成功
if(account.contentEquals("admin") && password.equals("12345")) {
Intent intent = new Intent(LoginActivity.this,MainActivity.class);
startActivity(intent);
finish();
}else {
Toast.makeText(LoginActivity.this,"account or password is invalid",Toast.LENGTH_SHORT).show();
}
}
});
}
}
- 使用
findViewById方法分別獲取輸入框以及登錄按鈕的實例 - 然后設(shè)置點擊事件:先判斷賬號和密碼對不對,對了就是用intent實例進入到主活動中;錯了就重新進入登錄頁面,并且打印出一條提示語。
- 接著改造一下主界面,當(dāng)然加上強制下線功能就行,不要其他花里胡哨的。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/force_offline" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Send force offline broadcast" /> </LinearLayout>
- 非常簡單,就是加了一個按鈕
- 接下來修改主活動的邏輯
package com.example.broadcastbestpractice;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
@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 OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("com.example.broadcastbestpractice.FORCE_OFFLINE");
sendBroadcast(intent);
}
});
}
}
- 在點擊事件里面,我們發(fā)送了
com.example.broadcastbestpractice.FORCE_OFFLINE廣播,用于通知程序強制用戶下線。 - 這說明強制用戶下線的功能,應(yīng)該寫在接收器中,不寫在具體某個活動中,這樣發(fā)出“下線”廣播的時候,就能完成下線操作了。
- 接下來創(chuàng)建一個廣播接收器
package com.example.broadcastbestpractice;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.WindowManager;
import android.content.DialogInterface.OnClickListener;;
public class ForceOfflineReceiver extends BroadcastReceiver{
@Override
public void onReceive(final Context context,Intent intent) {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
dialogBuilder.setTitle("Warning");
dialogBuilder.setMessage("You are forced to be offline,Please try to login again.");
dialogBuilder.setCancelable(false);
//下面這句改了
dialogBuilder.setPositiveButton("OK", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int which) {
ActivityCollector.finishAll();//銷毀所有的活動
Intent intent = new Intent(context,LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);//重新啟動LoginActivity
}
});
AlertDialog alertDialog = dialogBuilder.create();
//需要設(shè)置AlertDialog的類型,保證廣播接收器中可以正常彈出
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
alertDialog.show();
}
}

25.1
三、源碼:
BroadcastBestPractice
https://github.com/ruigege66/Android/tree/master/BroadcastBestPractice
到此這篇關(guān)于Android實現(xiàn)強制下線功能的示例代碼的文章就介紹到這了,更多相關(guān)android 強制下線內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android 動畫之AlphaAnimation應(yīng)用詳解
本節(jié)講解AlphaAnimation 動畫,窗口的動畫效果,淡入淡出什么的,有些游戲的歡迎動畫,logo的淡入淡出效果就使用AlphaAnimation,具體的祥看本文,需要的朋友可以參考下2012-12-12
Android開發(fā)基于Drawable實現(xiàn)圓角矩形的方法
這篇文章主要介紹了Android開發(fā)基于Drawable實現(xiàn)圓角矩形的方法,結(jié)合實例形式分析了Drawable的功能、相關(guān)圖形繪制函數(shù)與使用方法,需要的朋友可以參考下2017-10-10
Android自定義網(wǎng)絡(luò)連接工具類HttpUtil
這篇文章主要介紹了Android自定義網(wǎng)絡(luò)連接工具類HttpUtil,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11
Android多點觸控技術(shù)實戰(zhàn) 針對圖片自由縮放和移動
這篇文章主要為大家詳細(xì)介紹了Android多點觸控技術(shù)實戰(zhàn),自由地對圖片進行縮放和移動,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10
Flutter 使用fluro的轉(zhuǎn)場動畫進行頁面切換
在實際應(yīng)用中,我們常常會對不同的頁面采取不同的轉(zhuǎn)場動畫,以提高頁面切換過程中的用戶體驗。例如,微信的掃碼后在手機上確認(rèn)登錄頁面就是從底部彈出的,而大部分頁面的跳轉(zhuǎn)都是從右向左滑入。通過這種形式區(qū)分不同的轉(zhuǎn)場場景,從而給用戶更多的趣味性以提高用戶體驗。2021-06-06

