Use kotlin refactor Application

This commit is contained in:
anrannn
2022-09-30 23:06:33 +08:00
parent 89d8291254
commit b431183f13
6 changed files with 58 additions and 72 deletions
+1 -1
View File
@@ -5,7 +5,7 @@
<uses-permission android:name="android.permission.INTERNET" />
<application
android:name=".MyApplication"
android:name=".YandViewApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
@@ -1,72 +1,58 @@
package com.lsp.view;
package com.lsp.view
import android.app.Application;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.app.Application
import androidx.lifecycle.DefaultLifecycleObserver
import android.content.ServiceConnection
import android.content.ComponentName
import android.content.Context
import android.os.IBinder
import com.lsp.view.service.DownloadService.DownloadBinder
import com.google.android.material.color.DynamicColors
import android.content.Intent
import com.lsp.view.service.DownloadService
import androidx.lifecycle.LifecycleOwner
import androidx.annotation.NonNull;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import com.google.android.material.color.DynamicColors;
import com.lsp.view.service.DownloadService;
public class MyApplication extends Application implements DefaultLifecycleObserver{
private static Context context;
private static DownloadService.DownloadBinder downloadBinder;
private final ServiceConnection connection = new ServiceConnection(){
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
downloadBinder = (DownloadService.DownloadBinder) iBinder;
class YandViewApplication : Application(), DefaultLifecycleObserver {
private val connection: ServiceConnection = object : ServiceConnection {
override fun onServiceConnected(componentName: ComponentName, iBinder: IBinder) {
downloadBinder = iBinder as DownloadBinder
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
}
};
@Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
DynamicColors.applyToActivitiesIfAvailable(this);
Intent serviceIntent = new Intent(this, DownloadService.class);
bindService(serviceIntent, connection, Context.BIND_AUTO_CREATE);
statusBarHeight();
override fun onServiceDisconnected(componentName: ComponentName) {}
}
@Override
public void onDestroy(@NonNull LifecycleOwner owner) {
DefaultLifecycleObserver.super.onDestroy(owner);
unbindService(connection);
override fun onCreate() {
super<Application>.onCreate()
context = applicationContext
DynamicColors.applyToActivitiesIfAvailable(this)
val serviceIntent = Intent(this, DownloadService::class.java)
bindService(serviceIntent, connection, BIND_AUTO_CREATE)
statusBarHeight()
}
public static Context getContext() {
return context;
}
public static DownloadService.DownloadBinder getDownloadBinder() {
return downloadBinder;
override fun onDestroy(owner: LifecycleOwner) {
super.onDestroy(owner)
unbindService(connection)
}
//计算状态栏高度
public int statusBarHeight() {
int height = 0;
int resourceId = getResources().getIdentifier(
"status_bar_height",
"dimen",
"android"
);
fun statusBarHeight(): Int {
var height = 0
val resourceId = resources.getIdentifier(
"status_bar_height",
"dimen",
"android"
)
if (resourceId > 0) {
height = getResources().getDimensionPixelSize(resourceId);
height = resources.getDimensionPixelSize(resourceId)
}
return height;
return height
}
}
companion object {
var context: Context? = null
private set
var downloadBinder: DownloadBinder? = null
private set
}
}
@@ -11,7 +11,7 @@ import com.google.android.flexbox.*
import com.google.android.material.appbar.AppBarLayout
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.lsp.view.MyApplication
import com.lsp.view.YandViewApplication
import com.lsp.view.R
import com.lsp.view.activity.BaseActivity
import com.lsp.view.activity.main.MainActivity
@@ -28,7 +28,7 @@ class FavTagActivity : BaseActivity() {
setSupportActionBar(toolbar)
val appbar = findViewById<AppBarLayout>(R.id.appbar)
val nowHeight = appbar.layoutParams.height
appbar.layoutParams.height = (application as MyApplication).statusBarHeight()+nowHeight
appbar.layoutParams.height = (application as YandViewApplication).statusBarHeight()+nowHeight
supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.setHomeButtonEnabled(true)
@@ -21,7 +21,7 @@ import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.google.android.material.appbar.AppBarLayout
import com.google.android.material.navigation.NavigationView
import com.google.android.material.snackbar.Snackbar
import com.lsp.view.MyApplication
import com.lsp.view.YandViewApplication
import com.lsp.view.R
import com.lsp.view.activity.BaseActivity
import com.lsp.view.activity.favtag.FavTagActivity
@@ -60,7 +60,7 @@ class MainActivity : BaseActivity() {
setSupportActionBar(toolbar)
val appbar = findViewById<AppBarLayout>(R.id.appbar)
val nowHeight = appbar.layoutParams.height
appbar.layoutParams.height = (application as MyApplication).statusBarHeight()+nowHeight
appbar.layoutParams.height = (application as YandViewApplication).statusBarHeight()+nowHeight
//到达底部,加载更多数据
adapter.setLoadMoreListener(object : PostAdapter.OnLoadMoreListener {
@@ -5,7 +5,7 @@ import android.view.MenuItem
import androidx.appcompat.widget.Toolbar
import androidx.preference.PreferenceFragmentCompat
import com.google.android.material.appbar.AppBarLayout
import com.lsp.view.MyApplication
import com.lsp.view.YandViewApplication
import com.lsp.view.R
import com.lsp.view.activity.BaseActivity
@@ -20,7 +20,7 @@ class SettingsActivity : BaseActivity() {
val appbar = findViewById<AppBarLayout>(R.id.appbar)
val nowHeight = appbar.layoutParams.height
appbar.layoutParams.height = (application as MyApplication).statusBarHeight()+nowHeight
appbar.layoutParams.height = (application as YandViewApplication).statusBarHeight()+nowHeight
if (savedInstanceState == null) {
supportFragmentManager
+7 -7
View File
@@ -11,7 +11,7 @@ import android.os.Message
import android.widget.Toast
import androidx.core.content.FileProvider
import com.bumptech.glide.Glide
import com.lsp.view.MyApplication
import com.lsp.view.YandViewApplication
import java.io.File
import kotlin.concurrent.thread
@@ -65,11 +65,11 @@ object Util {
}
fun download(file_url: String?, file_ext: String?,md5: String?){
Toast.makeText(MyApplication.getContext(), "开始保存", Toast.LENGTH_SHORT).show()
Toast.makeText(YandViewApplication.context, "开始保存", Toast.LENGTH_SHORT).show()
if (file_url != null) {
if (file_ext != null) {
MyApplication.getDownloadBinder().downloadPic(file_url, file_ext,handler,md5)
YandViewApplication.downloadBinder?.downloadPic(file_url, file_ext,handler,md5)
}
}
}
@@ -79,19 +79,19 @@ object Util {
super.handleMessage(msg)
when(msg.what){
CallBackStatus.OK.ordinal ->{
Toast.makeText(MyApplication.getContext(), "保存成功", Toast.LENGTH_SHORT).show()
Toast.makeText(YandViewApplication.context, "保存成功", Toast.LENGTH_SHORT).show()
}
CallBackStatus.DOWNLOADERROR.ordinal -> {
Toast.makeText(MyApplication.getContext(), "下载异常", Toast.LENGTH_SHORT).show()
Toast.makeText(YandViewApplication.context, "下载异常", Toast.LENGTH_SHORT).show()
}
CallBackStatus.MD5COMPAREERROR.ordinal -> {
Toast.makeText(MyApplication.getContext(), "文件下载异常,MD5对比失败", Toast.LENGTH_SHORT).show()
Toast.makeText(YandViewApplication.context, "文件下载异常,MD5对比失败", Toast.LENGTH_SHORT).show()
}
CallBackStatus.FILEEXISTS.ordinal -> {
Toast.makeText(MyApplication.getContext(), "文件已经存在", Toast.LENGTH_SHORT).show()
Toast.makeText(YandViewApplication.context, "文件已经存在", Toast.LENGTH_SHORT).show()
}
}