package com.developine.kotlinandroidutils
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
import android.app.AlertDialog
import android.app.NotificationManager
import android.content.Context
import android.view.WindowManager
import android.net.ConnectivityManager
import android.provider.Settings
import android.support.v4.app.NotificationCompat
import android.telephony.TelephonyManager
import android.content.DialogInterface
import android.os.Build
class MainActivity : AppCompatActivity() {
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
return super.onCreateOptionsMenu(menu)
}
override fun onDestroy() {
super.onDestroy()
}
override fun onPause() {
super.onPause()
}
override fun onResume() {
super.onResume()
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun hideSoftKeyboard() {
getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN )
}
fun isNetworkConnected(): Boolean {
/* you will also need to add this permission in Manifest.
<uses-permission android:name=”android.permission.ACCESS_NETWORK_STATE” /> */
val connectivityManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val activeNetworkInfo = connectivityManager.activeNetworkInfo return activeNetworkInfo != null &&
activeNetworkInfo.isConnected return true
}
fun putPrefrences(key: String, value: String) {
val sharedPref = this?.getPreferences(Context.MODE_PRIVATE) ?: return
with(sharedPref.edit())
{
putString(key, value) commit() }
}
// READ DATA FROM SHARED PREFRENCES ANDROID KOTLIN CODE
fun getPrefrences(key: String): String {
val sharedPref = this?.getPreferences(Context.MODE_PRIVATE) ?: return
val defaultValue = “no record found”
val highScore = sharedPref.getString(key, defaultValue) return highScore }
// CLEAR APPLICATION SHARED PREFRENCES ANDROID KOTLIN CODE
fun clearPrefrences() {
val sharedPref = this?.getPreferences(Context.MODE_PRIVATE) ?: return
sharedPref.edit().clear().commit()
}
fun displayNotification() {
val mBuilder = NotificationCompat.Builder(this, “”) .setSmallIcon(R.drawable.notification_icon) .setContentTitle(“Notification title”) .setContentText(“Notification Content”) .setPriority(NotificationCompat.PRIORITY_DEFAULT)
val mNotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
mNotificationManager.notify(1, mBuilder.build())
}
fun getDeviceUniqueID() {
var androidID = Settings.Secure.getString(this.contentResolver, Settings.Secure.ANDROID_ID)
}
// GET DEVICE IMEI NUMBER ANDROID KOTLIN CODE
fun getDeviceIMEI() {
/* * You also need to add below permission in Manifest file.
<uses-permission android:name=”android.permission.READ_PHONE_STATE”/> * */
val telephonyManager = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager telephonyManager.deviceId
}
val builder: AlertDialog.Builder
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{ builder = AlertDialog.Builder(this, android.R.style.Theme_Material_Dialog_Alert) }
else {
builder = AlertDialog.Builder(this)
}
builder.setTitle(“Delete entry”) .setMessage(“Are you sure you want to delete this entry?”)
.setPositiveButton(android.R.string.yes, DialogInterface.OnClickListener { dialog, which -> // continue with delete }) .setNegativeButton(android.R.string.no, DialogInterface.OnClickListener {
dialog, which -> // do nothing }) .
setIcon(android.R.drawable.ic_dialog_alert) .show()
}
}
Recommended Reading: