一、回顾
ActivityCollector.java
用于管理所有的活动;一个类是BaseActivity.java
作为所有活动的父类;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,就认为登录成功 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
方法分别获取输入框以及登录按钮的实例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); } }); } }
com.example.broadcastbestpractice.FORCE_OFFLINE
广播,用于通知程序强制用户下线。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(); //需要设置AlertDialog的类型,保证广播接收器中可以正常弹出 alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); alertDialog.show(); } }
25.1
三、源码:
BroadcastBestPractice
https://github.com/ruigege66/Android/tree/master/BroadcastBestPractice
到此这篇关于Android实现强制下线功能的示例代码的文章就介绍到这了,更多相关android 强制下线内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!