Android 常见问题二:创建MyApplication

一、MyApplication单例

java 常见单例模式单例模式
import android.util.Log;

public class Car {

    /**
     * 饿汉式
     * 线程安全
     */
    private static Car car=new Car();
    public static Car getInstance(){
        return car;
    }

    /**
     * 懒汉式
     * 线程安全,效率稍低
     */
    private static Car car1;
    public static synchronized Car getInstance1(){
        if(car1==null){
            car1=new Car();
        }
        return car1;
    }

    /**
     * 双锁模式/双重校验
     * 效率比懒汉式高,线程安全
     * volatile修饰符:会将变量存到主存中,每次创建刷新,都会更新主存中的变量,相当于变量单例
     * 无volatile修饰时,如果线程不同,变量会存到当前线程所在的栈中
     */
    private volatile static Car car2;
    public static Car getInstance2(){
        if(car2==null){
            synchronized (Car.class){
                if(car2==null){
                    car2=new Car();
                }
            }
        }
        return car2;
    }

    public void drive(){
        Log.i("DL","单例开车");
    }
}
MyApplication单例
 private static final MyApplication app=new MyApplication();
    public static MyApplication getInstance(){
        return app;
    }
kotlin MyApplication单例

1、手动设置

 companion object{
        private var instance:MyApplication? = null
        fun instance() = instance!!

        //2、声明延迟初始化属性
//        private lateinit var instance:MyApplication
//        fun instance() = instance
    }

    override fun onCreate() {
        super.onCreate()
        instance=this
    }

2、Delegates代理
kotlin提供了一个系统工具自动校验,这个工具便是Delegates的notNull方法。该方法返回非空校验的行为,只要将指定属性的读写行为委托给这个非空校验行为,那么开发者就无需手工进行非空判断了

 companion object{
        private var instance:MyApplication by Delegates.notNull()
        fun instance() = instance
    }

    override fun onCreate() {
        super.onCreate()
        instance=this
    }

二、界面管理

class MyApplication : Application() {

	 private val activitys = ArrayList()
	
	 fun addActivity(activity:Activity){
	        activitys.add(activity)
	    }
	
	    fun removeActivity(activity:Activity){
	        activitys.remove(activity)
	    }
	
	    fun removeAllActivity(){
	        activitys.map {
	            it.finish()
	        }
	    }
	
	    fun getCurrentActivity():Activity = activitys.get(activitys.size-1)
 }

三、全局异常处理

1、MyApplication实现Thread.UncaughtExceptionHandler
2、oncreate 方法中 Thread.setDefaultUncaughtExceptionHandler(this)
3、重写方法uncaughtException

  /**
     * 全局未知异常处理
     */
    override fun uncaughtException(t: Thread, e: Throwable) {
        val b:Boolean  = handException(e)
    }

    private fun handException(e: Throwable): Boolean {
        if(e == null) return false
        //写入错误日志
        Thread{
            var out: FileWriter? = null

            try {
                Looper.prepare()
                Toast.makeText(applicationContext, e.toString(), Toast.LENGTH_LONG)
                    .show()
                val file = File(getExternalFilesDir(null), "error.txt")
                if(!file.exists()){
                    val createNewFile = file.createNewFile()
                }
                out = FileWriter(file, true)
                out.write("\r\n") //换行
                out.write(e.toString())
            }catch (e: Exception){

            }finally {
                if(out!=null){
                    out.close()
                }
            }
            try {
                Thread.sleep(5000)
                removeAllActivity()
                restartApp()
            } catch (e: InterruptedException) {
                e.printStackTrace()
            }
            Looper.loop()
        }.start()

        return true
    }

    /**
     * 重启app
     */
    private fun restartApp() {
        val intent = Intent(this, SplashActvity::class.java)
        @SuppressLint("WrongConstant") val restartIntent = PendingIntent.getActivity(
            applicationContext, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK
        )
        //退出程序
        val mgr = getSystemService(ALARM_SERVICE) as AlarmManager
        mgr[AlarmManager.RTC, System.currentTimeMillis() + 1000] = restartIntent // 1秒钟后重启应用

        //结束进程之前可以把你程序的注销或者退出代码放在这段代码之前
        Process.killProcess(Process.myPid())
    }

MyApplication代码

package com.zqq.shell

import android.annotation.SuppressLint
import android.app.Activity
import android.app.AlarmManager
import android.app.Application
import android.app.PendingIntent
import android.content.Intent
import android.os.Looper
import android.os.Process
import android.widget.Toast
import com.zqq.shell.activity.SplashActvity
import java.io.File
import java.io.FileWriter

class MyApplication : Application(),Thread.UncaughtExceptionHandler {

    private val activitys = ArrayList()

    companion object{
        private var instance:MyApplication? = null
        fun instance() = instance!!

        //2、声明延迟初始化属性
//        private lateinit var instance:MyApplication
//        fun instance() = instance
    }

//    companion object{
//        private var instance:MyApplication by Delegates.notNull()
//        fun instance() = instance
//    }

    override fun onCreate() {
        super.onCreate()
        instance=this
        Thread.setDefaultUncaughtExceptionHandler(this)
    }


    fun addActivity(activity: Activity){
        activitys.add(activity)
    }

    fun removeActivity(activity: Activity){
        activitys.remove(activity)
    }

    fun removeAllActivity(){
        activitys.map {
            it.finish()
        }
    }

    fun getCurrentActivity():Activity = activitys.get(activitys.size - 1)

    /**
     * 全局未知异常处理
     */
    override fun uncaughtException(t: Thread, e: Throwable) {
        val b:Boolean  = handException(e)
    }

    private fun handException(e: Throwable): Boolean {
        if(e == null) return false
        //写入错误日志
        Thread{
            var out: FileWriter? = null

            try {
                Looper.prepare()
                Toast.makeText(applicationContext, e.toString(), Toast.LENGTH_LONG)
                    .show()
                val file = File(getExternalFilesDir(null), "error.txt")
                if(!file.exists()){
                    val createNewFile = file.createNewFile()
                }
                out = FileWriter(file, true)
                out.write("\r\n") //换行
                out.write(e.toString())
            }catch (e: Exception){

            }finally {
                if(out!=null){
                    out.close()
                }
            }
            try {
                Thread.sleep(5000)
                removeAllActivity()
                restartApp()
            } catch (e: InterruptedException) {
                e.printStackTrace()
            }
            Looper.loop()
        }.start()

        return true
    }

    /**
     * 重启app
     */
    private fun restartApp() {
        val intent = Intent(this, SplashActvity::class.java)
        @SuppressLint("WrongConstant") val restartIntent = PendingIntent.getActivity(
            applicationContext, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK
        )
        //退出程序
        val mgr = getSystemService(ALARM_SERVICE) as AlarmManager
        mgr[AlarmManager.RTC, System.currentTimeMillis() + 1000] = restartIntent // 1秒钟后重启应用

        //结束进程之前可以把你程序的注销或者退出代码放在这段代码之前
        Process.killProcess(Process.myPid())
    }

}

测试异常处理
Android 常见问题二:创建MyApplication_第1张图片
Android 常见问题二:创建MyApplication_第2张图片

你可能感兴趣的:(java)