欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

kotlin實現強制下線功能

 更新時間:2018年06月01日 08:34:03   作者:代碼沒寫完  
這篇文章主要為大家詳細介紹了kotlin實現強制下線功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下

強制下線是需要關閉所有的活動,先創(chuàng)建一個類來管理所有的活動。

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>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Android中的二維碼生成與掃描功能

    Android中的二維碼生成與掃描功能

    二維碼在我們身邊真的非常普遍,今天小編給大家分享二維碼生成與掃描功能,依然使用目前比較流行的zxing方法,具體實現思路大家通過本文一起學習吧
    2017-01-01
  • 分享Android開發(fā)中最有效率最快的循環(huán)代碼

    分享Android開發(fā)中最有效率最快的循環(huán)代碼

    分享Android開發(fā)中最有效率最快的循環(huán)代碼,需要的朋友可以參考下
    2013-01-01
  • Android上傳文件到服務端并顯示進度條

    Android上傳文件到服務端并顯示進度條

    這篇文章主要為大家詳細介紹了Android上傳文件到服務端,并顯示進度條,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • Kotlin?泛型邊界型變及星投影使用詳解

    Kotlin?泛型邊界型變及星投影使用詳解

    這篇文章主要為大家介紹了Kotlin?泛型邊界型變及星投影使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-12-12
  • Android OkHttp的簡單使用和封裝詳解

    Android OkHttp的簡單使用和封裝詳解

    這篇文章主要介紹了Android OkHttp的簡單使用和封裝詳解的相關資料,Android OKHttp的簡單get、post的使用,再到它的封裝,需要的朋友可以參考下
    2016-12-12
  • Android異步上傳圖片到PHP服務器

    Android異步上傳圖片到PHP服務器

    這篇文章主要介紹了Android異步上傳圖片到PHP服務器的相關資料,需要的朋友可以參考下
    2015-12-12
  • kotlin使用建造者模式自定義對話框

    kotlin使用建造者模式自定義對話框

    這篇文章主要為大家詳細介紹了kotlin使用建造者模式自定義對話框的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • Android通過繼承Binder類實現多進程通信

    Android通過繼承Binder類實現多進程通信

    本篇文章主要介紹了Android通過繼承Binder類實現多進程通信,具有一定的參考價值,感興趣的小伙伴們可以參考一下。
    2017-03-03
  • Android的HTTP類庫Volley入門學習教程

    Android的HTTP類庫Volley入門學習教程

    這篇文章主要介紹了Android應用開發(fā)框架Volley的入門學習教程,Volley適合于輕量級的通信功能開發(fā),善于處理JSON對象,需要的朋友可以參考下
    2016-02-02
  • Android實現加載時提示“正在加載,請稍后”的方法

    Android實現加載時提示“正在加載,請稍后”的方法

    在現在的很多應用中,當在加載的時候,如果頁面動態(tài)數據較多,會有很長一段時間的空白頁面,如果加上這個頁面正在加載的提示,使得應用更加人性化。這篇文章就給大家分享了在 Android實現加載時提示“正在加載,請稍后”的方法,有需要的朋友們可以參考借鑒。
    2016-10-10

最新評論