如何实现强制下线
2017-11-24 本文已影响11人
AJI大侠
如何实现账号强制下线
- 强制用户下线这个功能大家应该见过很多了,到底怎么实现呢现在我们一起来做一做
- 建立activity的管理类
public class ActivityCollector
{
public static List<Activity> activityList = new ArrayList<>();
public static void addActivity(Activity activity)
{
activityList.add(activity);
}
public static void removeActivity(Activity activity)
{
activityList.remove(activity);
}
public static void finshAllActivity()
{
for(Activity activity : activityList)
{
if (!activity.isFinishing())
{
activity.finish();
}
}
}
}
- 建立基本的activity,BaseActivity,所有的Activity都继承它,在oncreate , onResume,onDestroy添加以下代码,会注意到这里注册了一个广播,之所以要在BaseActivity里面注册是是因为以后的每个Activity都会继承BaseActivity,这个就可以保证每个界面都可以接收这个广播
public class BaseActivity extends AppCompatActivity
{
private ForceOffineReceiver forceOffineReceiver;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
ActivityCollector.addActivity(this);
}
@Override
protected void onResume()
{
super.onResume();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.zz.mybreadcast.FORCE_OFFINE");
forceOffineReceiver = new ForceOffineReceiver();
registerReceiver(forceOffineReceiver , intentFilter);
}
@Override
protected void onDestroy()
{
super.onDestroy();
ActivityCollector.removeActivity(this);
if (forceOffineReceiver != null)
{
unregisterReceiver(forceOffineReceiver);
}
}
}
-
建立一个登陆的界面,界面很简单,界面我就不贴了
1511504182(1).png
public class LoginActivity extends BaseActivity
{
private EditText login_view;
private EditText password_view;
private Button login_button;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
initView();
initData();
}
private void initView()
{
login_view = findViewById(R.id.login_view);
password_view = findViewById(R.id.password_view);
login_button = findViewById(R.id.login_button);
}
private void initData()
{
login_button.setOnClickListener(v ->
{
String loginStr = login_view.getText().toString().trim();
String passwordStr = password_view.getText().toString().trim();
if(loginStr.equals("admin") && passwordStr.equals("123456"))
{
startActivity(new Intent(this , MainActivity.class));
finish();
}
else
{
Toast.makeText(LoginActivity.this , "账号或者密码错误!" , Toast.LENGTH_LONG);
}
});
}
}
- 建立广播,代码如下
public class ForceOffineReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("提示");
builder.setCancelable(false);
builder.setMessage("您的账号在另外一台手机已经登录!");
builder.setPositiveButton("确定" , new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which)
{
ActivityCollector.finshAllActivity();
context.startActivity(new Intent(context , LoginActivity.class));
}
});
builder.show();
}
}
- 最后是MainActivity,这个类里面只需模仿发送下线广播就行
public class MainActivity extends BaseActivity
{
private Button main_button;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
main_button = findViewById(R.id.main_button);
// lambda表达式
main_button.setOnClickListener(v -> {
Intent intent = new Intent("com.zz.mybreadcast.FORCE_OFFINE");
sendBroadcast(intent);
});
}
}
- 别忘记在配置文件里面注册哦!
博客