您好,登錄后才能下訂單哦!
強制下線是需要關閉所有的活動,先創建一個類來管理所有的活動。
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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。