您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
强制下线是需要关闭所有的活动,先创建一个类来管理所有的活动。
class ActivityCollector { //var activities :MutableList<Activity>=MutableList<Activity>() companion object{ val activities = ArrayList<Activity>() fun addActivity( activity:Activity){ activities.add(activity) } fun removeActivity(activity:Activity){ activities.remove(activity) } fun finsishAll(){ for(activity:Activity in activities){ if(!activity.isFinishing){ activity.finish() } } activities.clear() } } }
然后建立一个所有活动的父类
open class BaseActivity : AppCompatActivity(){ var activityCollector=ActivityCollector() lateinit var receiver:ForceOfflineReceiver override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) { super.onCreate(savedInstanceState, persistentState) ActivityCollector.addActivity(this) //activityCollector.addActivity(this) } override fun onResume() { super.onResume() var inetnefilter:IntentFilter=IntentFilter() inetnefilter.addAction("FORCE_FOOLINE") receiver=ForceOfflineReceiver() registerReceiver(receiver,inetnefilter) } override fun onPause() { super.onPause() if(receiver!=null){ unregisterReceiver(receiver) // receiver=null } } override fun onDestroy() { super.onDestroy() ActivityCollector.removeActivity(this) } open class ForceOfflineReceiver : BroadcastReceiver(){ override fun onReceive(context: Context, intent: Intent?) { var builder :AlertDialog.Builder=AlertDialog.Builder(context) builder.setTitle("Warning") builder.setMessage("you are forced to be offline .please try to login again") builder.setCancelable(false) builder.setPositiveButton("ok",object :DialogInterface.OnClickListener{ override fun onClick(dialog: DialogInterface?, which: Int) { ActivityCollector.finsishAll() var intent=Intent(context,LoginActivity::class.java) context.startActivity(intent) } }) builder.show() } } }
建立一个登录的布局文件
<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:layout_gravity="center_vertical" android:textSize="18sp" android:text="account:" /> <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:layout_gravity="center_vertical" android:textSize="18sp" android:text="password:" /> <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>
然后编写一个登录的活动,这里写了一个简单的登录,密码正确的话就进入主页面,否则给出提示。
class LoginActivity:BaseActivity(){ lateinit var accountEdid:EditText lateinit var passwordEdit :EditText lateinit var login:Button override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.login) accountEdid=findViewById(R.id.account) passwordEdit=findViewById(R.id.password) login=findViewById(R.id.login) login.setOnClickListener(View.OnClickListener { var account =accountEdid.text.toString() var password=passwordEdit.text.toString() if(account.equals("admin")&&password.equals("123")){ var intent:Intent=Intent(this, MainActivity::class.java) startActivity(intent) finish() }else{ Toast.makeText(this,"账号或密码错误",Toast.LENGTH_SHORT).show() } }) } }
然后修改一下mainactivity的代码
class MainActivity : BaseActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) var forceOffline :Button =findViewById(R.id.force_offline) forceOffline.setOnClickListener(View.OnClickListener { var intent:Intent=Intent("FORCE_FOOLINE") sendBroadcast(intent) }) } }
最后对AndroidManifest.xml修改,把主活动设置更换一下。
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> </activity> <activity android:name=".LoginActivity" android:launchMode="singleTask"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。