Skip to content

Commit

Permalink
add isNetworkAvailable
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanCheen committed Mar 15, 2022
1 parent a31066e commit 29b3d12
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
7 changes: 7 additions & 0 deletions app/src/main/java/me/yifeiyuan/onepiece/dev/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@ package me.yifeiyuan.onepiece.dev

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import me.yifeiyuan.onepiece.pandora.ktx.copyTextToClipboard
import me.yifeiyuan.onepiece.pandora.ktx.start

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

}

fun testContext() {
start<MainActivity>()
}
}
1 change: 1 addition & 0 deletions pandora/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="me.yifeiyuan.onepiece.pandora">

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
package me.yifeiyuan.onepiece.pandora.ktx

import android.app.Activity
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.net.ConnectivityManager
import android.os.Build
import android.view.View
import android.view.inputmethod.InputMethodManager
import androidx.annotation.RequiresApi
import java.io.BufferedReader
import java.io.InputStreamReader

/**
* Created by 程序亦非猿 on 2021/4/7.
*/

inline fun <reified A : Activity> Context.start(intentConfig: Intent.() -> Unit = {}) {
startActivity(Intent(this, A::class.java).apply(intentConfig))
}

fun Context.toPixel(dip: Int): Int {
val scale: Float = resources.displayMetrics.scaledDensity
return (dip * scale + 0.5).toInt()
Expand All @@ -21,6 +30,10 @@ fun Context.getScreenHeight(): Int {
return resources.displayMetrics.heightPixels
}

fun Context.getScreenWidth(): Int {
return resources.displayMetrics.widthPixels
}

/**
* @param fileName 例如 foo.json
*/
Expand Down Expand Up @@ -55,13 +68,28 @@ fun Context?.copyTextToClipboard(text: CharSequence) {
clipboardManager.setPrimaryClip(clip)
}

//inline fun <reified S>Context.getSystemService():S{
// return getSystemService(S::class.java)
//}

fun Context?.showSoftInput(view: View, flag: Int = InputMethodManager.SHOW_FORCED) {
if (this == null) {
return
}
val imm: InputMethodManager =
getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager

imm.showSoftInput(view, flag)
}

/**
* 判断网络是否连接
* @return 是否有网络可用
*/
fun Context.isNetworkAvailable(): Boolean {
val manager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
if (manager.activeNetworkInfo != null) {
return manager.activeNetworkInfo!!.isAvailable
}
return false
}

0 comments on commit 29b3d12

Please sign in to comment.