Android实现登录功能
首先,我们要先解决一个问题:
关于一个程序开启了多个活动,那么按下返回键会逐层返回上一个活动,想要做到一个退出程序的效果,是很不理想的,那么如何解决呢?
我们要先写一个活动控制器,然后通过这个活动控制器来进行启动和关闭以及关闭全部活动的操作
活动控制器代码如下:
package com.example.loginactivity;
import android.app.Activity;
import java.util.ArrayList;
import java.util.List;
public class ActivityCollector {
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();
}
}
activities.clear();
}
}
下面实现强制下线功能:
效果如下:
当点击强制登录后,结束所有活动,跳转到MainActivity中:
强制下线实现逻辑:
所有的活动继承BaseActivity,在BaseActivity中定义一个广播接收器,接收到的处理是 (弹窗:结束所有活动,跳转到首页活动),记得调用Destroy()
注意:这里的广播的注册和销毁分别在onResume()和onPause()中,而不是onCreate()和onDestroy(),为的是保证栈顶的活动接收到这条强制下线广播
分析使用静态还是动态注册:
由于广播接收器里面需要弹出一个对话框来阻塞用户正常操作,静态注册是没有办法在onReceive()方法中弹出对话框这猴子那个UI的
每次开启首页活动时发送广播
代码如下:
BaseActivity.java:
package com.example.logintestactivity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.PersistableBundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class BaseActivity extends AppCompatActivity {
private ForceOfflinereceiver receiver;
@Override
public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
ActivityCollector.addActivity(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
ActivityCollector.removeActivity(this);
}
@Override
protected void onResume() {
super.onResume();
IntentFilter filter=new IntentFilter();
filter.addAction("com.example.logintestactivity.FORCE_OFFLINE");
receiver=new ForceOfflinereceiver();
registerReceiver(receiver,filter);
}
@Override
protected void onPause() {
super.onPause();
if (receiver!=null){
unregisterReceiver(receiver);
receiver=null;
}
}
class ForceOfflinereceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
AlertDialog.Builder builder=new AlertDialog.Builder(context);
builder.setTitle("重复登录");
builder.setMessage("您的用户在别处登录,请重新登录");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
ActivityCollector.finishAll();
Intent intent=new Intent(context,MainActivity.class);
context.startActivity(intent);
}
});
builder.show();
}
}
}
MainActivity.java:
package com.example.logintestactivity;
import androidx.appcompat.app.AppCompatActivity;
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 MainActivity extends BaseActivity implements View.OnClickListener {
private EditText et_account;
private EditText et_password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_account = findViewById(R.id.account);
et_password = findViewById(R.id.password);
Button btn_login=findViewById(R.id.btn_login);
btn_login.setOnClickListener(this);
Button btn_relogin=findViewById(R.id.btn_relogin);
btn_relogin.setOnClickListener(this);
}
public void relogin(){
Intent intent=new Intent("com.example.logintestactivity.FORCE_OFFLINE");
sendBroadcast(intent);
}
public void check_real(){
String account=et_account.getText().toString();
String password=et_password.getText().toString();
Toast.makeText(this,"用户名:"+account+"密码:"+password,Toast.LENGTH_SHORT).show();
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btn_login:
check_real();
break;
case R.id.btn_relogin:
relogin();
break;
}
}
}
搞定