Is it possible to use Android Bundle to create and putString() in Activity then getString() in Service on button click?
If not what can i do?
Example
MainActivity.kt
val bundle = Bundle()
bundle.putString("MyString", "Message")
val mesg = Message.obtain(null, MyService.SEND_MESSAGE_FLAG)
mesg.obj = bundle
try {
myService!!.send(mesg)
} catch (e: RemoteException) {
}
Service
override fun handleMessage(msg: Message) {
when (msg.what) {
SEND_MESSAGE_FLAG -> {
val data = msg.data
val dataString = data.getString("MyString")
println(dataString)
val mesg = Message.obtain(null, SEND_MESSAGE_FLAG)
mesg.obj = dataString
try {
msg.replyTo.send(mesg)
} catch (e: RemoteException) {
Log.i(TAG, "Error: " + e.message)
}
}
}
super.handleMessage(msg)
}
You can add static method in your service:
companion object {
private const val EXTRA_KEY_MY_STR = "EXTRA_KEY_MY_STR"
fun startMyService(context: Context?, myStr: String?) {
if (context != null) {
val intent = Intent(context, MyService::class.java)
intent.putExtra(EXTRA_KEY_MY_STR, myStr)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(intent)
} else {
context.startService(intent)
}
}
}
}
then call it from your activity: MyService.startMyService(this, "MyString")
and then get string in your onHandleIntent(): val myStr = intent?.extras?.getString(EXTRA_KEY_MY_STR)
Related
Android 12 Location updates not working when app is on background. and work fine when app is on forground.
I also "allow all time" permission for for location. Acc to docs after this location updates start work on background. but in my case it stop updates when app is on foreground.
locationUpdatePendingIntent
private val locationUpdatePendingIntent: PendingIntent by lazy {
val intent = Intent(context, LocationUpdatesBroadcastReceiver::class.java)
intent.action = LocationUpdatesBroadcastReceiver.ACTION_PROCESS_UPDATES
PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_MUTABLE)
}
Firstly I use Location Request. inside Automatic Workmanager
val mLocationRequest = LocationRequest.Builder(Priority.PRIORITY_HIGH_ACCURACY, 0L)
.setWaitForAccurateLocation(false)
.build()
if (ActivityCompat.checkSelfPermission(
context.applicationContext,
Manifest.permission.ACCESS_FINE_LOCATION
) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
context,
Manifest.permission.ACCESS_COARSE_LOCATION
) != PackageManager.PERMISSION_GRANTED
) {
return Result.success();
}
fusedLocationProviderClient?.requestLocationUpdates(
mLocationRequest,
locationUpdatePendingIntent
)
then listen to location updates used inside Automatic Workmanager
override fun onLocationUpdate(location: Location) {
val executor: ExecutorService = Executors.newSingleThreadExecutor()
executor.execute {
lastLocation = location
updateLocation()
}
}
Location update broadcast (in this location update)
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.location.Location
import android.util.Log
import com.google.android.gms.location.LocationAvailability
import com.google.android.gms.location.LocationResult
class LocationUpdatesBroadcastReceiver: BroadcastReceiver() {
private val TAG = "LocationUpdatesBroadcas";
override fun onReceive(context: Context?, intent: Intent?) {
Log.d(TAG, "onReceive() context:$context, intent:$intent")
if (intent?.action == ACTION_PROCESS_UPDATES) {
// Checks for location availability changes.
LocationAvailability.extractLocationAvailability(intent)?.let { locationAvailability ->
if (!locationAvailability.isLocationAvailable) {
Log.d(TAG, "Location services are no longer available!")
}
}
LocationResult.extractResult(intent)?.let { locationResult ->
Log.e(TAG, "onReceive: lastLocation ${locationResult.locations.size}", )
if(locationResult.lastLocation != null){
ShareLocationToAutomaticWorker.INSTANCE?.reciveLocation(locationResult.lastLocation!!)
}
}
}
}
companion object {
const val ACTION_PROCESS_UPDATES =
"action." +
"PROCESS_UPDATES"
}
interface UpdatedLocation{
fun onLocationUpdate(location: Location)
}
}
class ShareLocationToAutomaticWorker(var updateLocation: LocationUpdatesBroadcastReceiver.UpdatedLocation){
private val TAG = "LocationUpdatesBroadcas"
fun reciveLocation( location:Location){
Log.e(TAG, "reciveLocation:--- ${location} " )
updateLocation.onLocationUpdate(location)
}
companion object {
var INSTANCE: ShareLocationToAutomaticWorker? = null
fun getInstance(updateLocation: LocationUpdatesBroadcastReceiver.UpdatedLocation): ShareLocationToAutomaticWorker {
return INSTANCE ?: synchronized(this) {
INSTANCE ?: ShareLocationToAutomaticWorker(
updateLocation )
.also { INSTANCE = it }
}
}
}
}
I fixed this issue by changing "AutomaticLocationWorker" to "AutomaticLocationBackgroundService:LifecycleService()" or change worker to lifecycleService.
private var fusedLocationProviderClient: FusedLocationProviderClient? = null
private val locationUpdatePendingIntent: PendingIntent by lazy {
val intent = Intent(this, LocationUpdatesBroadcastReceiver::class.java)
intent.action = LocationUpdatesBroadcastReceiver.ACTION_PROCESS_UPDATES
PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_MUTABLE)
}
fusedLocationProviderClient?.requestLocationUpdates(
mLocationRequest,
locationUpdatePendingIntent
)
LocationUpdatesBroadcastReceiver
class LocationUpdatesBroadcastReceiver: BroadcastReceiver() {
private val TAG = "LocationUpdatesBroadcas";
override fun onReceive(context: Context?, intent: Intent?) {
Log.d(TAG, "onReceive() context:$context, intent:$intent")
if (intent?.action == ACTION_PROCESS_UPDATES) {
// Checks for location availability changes.
LocationAvailability.extractLocationAvailability(intent)?.let { locationAvailability ->
if (!locationAvailability.isLocationAvailable) {
Log.d(TAG, "Location services are no longer available!")
}
}
LocationResult.extractResult(intent)?.let { locationResult ->
Log.e(TAG, "onReceive: lastLocation ${locationResult.locations.size}", )
if(locationResult.lastLocation != null){
ShareLocationToAutomaticWorker.INSTANCE?.reciveLocation(locationResult.lastLocation!!)
}
}
}
}
companion object {
const val ACTION_PROCESS_UPDATES =
"com.commuteoptm.bcos.background_services.action." +
"PROCESS_UPDATES"
}
interface UpdatedLocation{
fun onLocationUpdate(location: Location)
}
}
ShareLocationToAutomaticWorker
class ShareLocationToAutomaticWorker(var updateLocation: LocationUpdatesBroadcastReceiver.UpdatedLocation){
private val TAG = "LocationUpdatesBroadcas"
fun reciveLocation( location:Location){
Log.e(TAG, "reciveLocation:--- ${location} " )
updateLocation.onLocationUpdate(location)
}
companion object {
var INSTANCE: ShareLocationToAutomaticWorker? = null
fun getInstance(updateLocation: LocationUpdatesBroadcastReceiver.UpdatedLocation): ShareLocationToAutomaticWorker {
return INSTANCE ?: synchronized(this) {
INSTANCE ?: ShareLocationToAutomaticWorker(
updateLocation )
.also { INSTANCE = it }
}
}
}
}
I'm facing following Errors/Exceptions when receiving data from one App to another App in Kotlin (Android):
Class Not Found When Unmarshalling: com.example.Parcelable.User
mExtras=Bundle[{)}]
mParcelledData=null
mMap=size 0
Also, In MainActivtiy: if(intent.hasExtra("USER")) statement giving error and displaying Toast message written in Else statement.
Please help me how I can resolve these exceptions ? Thanks a Lot!
(SendingApp File) -> MainActivity.kt
btnSendData1.setOnClickListener {
val intent = Intent(Intent.ACTION_SEND).setType("text/plain")
intent.component =
ComponentName.unflattenFromString("com.example.receive")
val args = Bundle()
args.putParcelable(USER_KEY, User("Ahmer", "28"))
intent.putExtras(args)
Log.d("Ahmer", args.toString())
startActivity(Intent.createChooser(intent, "Share to..."))
}
(ReceivingApp File) -> MainActivity.kt
class MainActivity : AppCompatActivity() {
private lateinit var textView: TextView
#SuppressLint("SetTextI18n")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
textView = findViewById(R.id.tv_username)
when (intent?.action) {
Intent.ACTION_SEND -> {
if ("text/plain" == intent.type) {
handleSendText(intent)
} else {
Toast.makeText(this, "No Text", Toast.LENGTH_SHORT).show()
}
}
}
}
#SuppressLint("SetTextI18n")
private fun handleSendText(intent: Intent) {
if (intent.extras != null) {
if (intent.hasExtra("USER")) {
Log.d("Intent", intent.toString())
val bundle = intent.extras
val user = bundle?.getParcelable<User>("USER") as User
textView.text = "${user.name}${user.age}"
} else {
Toast.makeText(this, "Has Extras Null", Toast.LENGTH_SHORT).show()
}
} else {
Toast.makeText(this, "Extras Null", Toast.LENGTH_SHORT).show()
}
}
}
(ReceivingApp File) -> User.kt
package com.example.receive
import android.os.Parcelable
import kotlinx.android.parcel.Parcelize
#Parcelize
data class User(
var name: String,
var age: String
) : Parcelable
How to scan barcode lines with VARCHAR Chars For example OfficeEqp35023U11 In android Kotlin
Struggling to scan this asset image
Struggling to scan this asset image
class BarcodeScannerActivity : AppCompatActivity(), ZXingScannerView.ResultHandler {
private var mScannerView: ZXingScannerView? = null
lateinit var apiInterface: APIInterface
public override fun onCreate(state: Bundle?) {
super.onCreate(state)
setContentView(R.layout.activity_barcode_scanner)
ssoId = intent.getStringExtra("initiator")
val contentFrame = findViewById<View>(R.id.content_frame) as ViewGroup
mScannerView = ZXingScannerView(this)
contentFrame.addView(mScannerView)
}
public override fun onResume() {
super.onResume()
mScannerView!!.setResultHandler(this)
mScannerView!!.startCamera()
}
public override fun onPause() {
super.onPause()
mScannerView!!.stopCamera()
}
override fun handleResult(rawResult: Result) {
Toast.makeText(
this, "Contents = " + rawResult.text +
", Format = " + rawResult.barcodeFormat.toString(), Toast.LENGTH_SHORT
).show()
var result = rawResult.text
if (result.isDigitsOnly()) {
assetTag = result.filter { it.isLetterOrDigit() }
mScannerView!!.stopCamera()
apiInterface = APIClient.client!!.create(APIInterface::class.java)
val call: Call<Verifiedd> =
apiInterface.saveVerification(assetTag, ssoId, ssoId, "Verified", "Verified")
call.enqueue(object : Callback<Verifiedd> {
override fun onResponse(call: Call<Verifiedd>, response: Response<Verifiedd>) {
if (response.body() != null) {
lovelyProgressDialog?.dismiss()
Log.d("TAG", response.code().toString() + "")
var displayResponse = ""
val resource: Verifiedd = response.body()!!
responseCode = resource.responseCode
responseMessage = resource.responseMessage
if (responseMessage == "Data persisted successfully " || responseMessage.equals(
"Data persisted successfully "
)
) {
Toasty.normal(
this#BarcodeScannerActivity,
"",
Toasty.LENGTH_LONG
).show()
} else if (responseMessage == "" || responseMessage.equals(
""
)
) {
Toasty.normal(
this#BarcodeScannerActivity,
"Invalid Asset Verification status values",
Toasty.LENGTH_LONG
).show()
val intent =
Intent(this#BarcodeScannerActivity, BranchItemsActivity::class.java)
intent.flags =
Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
intent.putExtra(
"initiator", ssoId
)
startActivity(intent)
}
}
}
override fun onFailure(call: Call<Verifiedd>, t: Throwable) {
Toasty.normal(
this#BarcodeScannerActivity,
"Server Error",
Toasty.LENGTH_LONG
).show()
}
})
I am making an SOS application that, when pressing the power button three times, should make a call to a pre-defined number and send an SOS message to the specified numbers, the problem is that it sees that the power button is pressed and sends a message but does not make a call over the lock.
MyReceiver:
class MyReceiver(activity: Activity?) : BroadcastReceiver() {
private var activity: Activity? = null
var t1 = System.currentTimeMillis()
var t2 = System.currentTimeMillis()
var thread: Thread? = null
override fun onReceive(context: Context, intent: Intent) {
Log.v("onReceive", "Power button is pressed.")
val a = countPowerOff + 1
if (intent.action == Intent.ACTION_SCREEN_OFF) {
countPowerOff++
if (countPowerOff == 1) t1 = System.currentTimeMillis()
if (countPowerOff == 3) t2 = System.currentTimeMillis()
} else {
if (intent.action == Intent.ACTION_SCREEN_ON) {
if (countPowerOff == 3) {
Toast.makeText(context, "Clicked to call", Toast.LENGTH_LONG)
.show()
}
if (countPowerOff == 3 && t2 - t1 <= 5000) {
thread = Thread(Task())
thread!!.start()
}
if (countPowerOff == 3) {
countPowerOff = 0
}
}
}
}
internal inner class Task : Runnable {
override fun run() {
(activity as MainActivity).callFragmentMethod()
}
}
companion object {
var countPowerOff = 0
var starttime = 0
}
init {
this.activity = activity
}
}
Method for making a call and sending a message:
fun sendSos() {
val manager = context!!.getSystemService(Context.LOCATION_SERVICE) as LocationManager
if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
sosMassage =
"I need help, I'm at Latitude: ${currentLocation.latitude} and Longitude ${currentLocation.longitude} https://www.google.com/maps/search/?api=1&query=${currentLocation.latitude},${currentLocation.longitude}"
}
threadCall = Thread(callSOS())
threadCall!!.start()
threadMessage = Thread(sendSOS())
threadMessage!!.start()
}
internal inner class callSOS : Runnable {
override fun run() {
checkForCallSmsPermission()
val intent = Intent(Intent.ACTION_CALL)
val prefs = PreferenceManager.getDefaultSharedPreferences(requireContext())
val callPreferences = prefs.getString("call_preference", "1")
if (callPreferences == "1") {
intent.setData(Uri.parse("tel:103"))
} else {
intent.setData(Uri.parse("tel:${prefs.getString("contact_text_1", "103")}"))
}
startActivity(intent)
}
}
internal inner class sendSOS : Runnable {
override fun run() {
checkForSmsPermission()
val prefs = PreferenceManager.getDefaultSharedPreferences(requireContext())
val sms = SmsManager.getDefault()
val textPreferences = prefs.getString("text_preference", "1")
val primaryContact = prefs.getString("contact_text_1", "0701217070")
sms.sendTextMessage(
primaryContact,
null,
sosMassage,
null,
null
)
if (textPreferences == "2") {
for (i in 2..3) {
val contactNumber = prefs.getString("contact_text_$i", null)
if (contactNumber != null) {
sms.sendTextMessage(
contactNumber,
null,
sosMassage,
null,
null
)
}
}
}
}
}
I am trying to run an application that I rewrote in Kotlin from the following link:
https://www.androidhive.info/RxJava/android-rxjava-networking-with-retrofit-gson-notes-app/
The example was originally code base was coded in Java. I am getting the following error upon running the application.
2018-11-16 12:12:38.173 11843-11843/com.touchsides.rxjavanetworking E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.touchsides.rxjavanetworking, PID: 11843
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.touchsides.rxjavanetworking/com.touchsides.rxjavanetworking.view.MainActivity}: java.lang.InstantiationException: java.lang.Class<com.touchsides.rxjavanetworking.view.MainActivity> cannot be instantiated
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2843)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.InstantiationException: java.lang.Class<com.touchsides.rxjavanetworking.view.MainActivity> cannot be instantiated
at java.lang.Class.newInstance(Native Method)
at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:69)
at android.support.v4.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:43)
at android.app.Instrumentation.newActivity(Instrumentation.java:1215)
The MainActivity code is as follows:
abstract class MainActivity : AppCompatActivity()
{
lateinit var apiService: ApiService
var disposable = CompositeDisposable()
lateinit var mAdapter: NotesAdapter
var noteList = ArrayList<Note>()
companion object
{
val TAG = MainActivity::class.java.simpleName;
}
#BindView(R.id.coordinator_layout) var coordinatorLayout: CoordinatorLayout? = null
#BindView(R.id.recycler_view) var recyclerView: RecyclerView? = null
#BindView(R.id.txt_empty_notes_view) var noNotesView: TextView? = null
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
val toolbar = findViewById<Toolbar>(R.id.toolbar)
toolbar.setTitle(getString(R.string.activity_title_home))
setSupportActionBar(toolbar)
fab.setOnClickListener { view ->
showNoteDialog(false, null, -1);
}
}
override fun onCreateOptionsMenu(menu: Menu): Boolean
{
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.menu_main, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean
{
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
return when (item.itemId)
{
R.id.action_settings -> true
else -> super.onOptionsItemSelected(item)
}
}
/**
* Registering new user
* sending unique id as device identification
* https://developer.android.com/training/articles/user-data-ids.html
*/
private fun registerUser()
{
// unique id to identify the device
val uniqueId = UUID.randomUUID().toString()
disposable.add(apiService.register(uniqueId).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(
object : DisposableSingleObserver<User>()
{
override fun onSuccess(user: User)
{
// Storing user API Key in preferences
user.apiKey?.let { PrefUtils.storeApiKey(applicationContext, it) }
Toast.makeText(applicationContext,
"Device is registered successfully! ApiKey: " + PrefUtils.getApiKey(applicationContext),
Toast.LENGTH_LONG).show()
}
override fun onError(e: Throwable)
{
Log.e(TAG, "onError: " + e.message)
showError(e)
}
}))
}
/**
* Creating new note
*/
private fun createNote(note: String)
{
disposable.add(apiService.createNote(note).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(
object : DisposableSingleObserver<Note>()
{
override fun onSuccess(note: Note)
{
if (!TextUtils.isEmpty(note.error))
{
Toast.makeText(applicationContext, note.error, Toast.LENGTH_LONG).show()
return
}
Log.d(TAG, "new note created: " + note.id + ", " + note.note + ", " + note.timestamp)
// Add new item and notify adapter
noteList.add(0, note)
mAdapter.notifyItemInserted(0)
toggleEmptyNotes()
}
override fun onError(e: Throwable)
{
Log.e(TAG, "onError: " + e.message)
showError(e)
}
}))
}
/**
* Updating a note
*/
private fun updateNote(noteId: Int, note: String, position: Int)
{
disposable.add(apiService.updateNote(noteId,
note).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(object :
DisposableCompletableObserver()
{
override fun onComplete()
{
Log.d(TAG, "Note updated!")
val n = noteList.get(position)
n.note = (note)
// Update item and notify adapter
noteList.set(position, n)
mAdapter.notifyItemChanged(position)
}
override fun onError(e: Throwable)
{
Log.e(TAG, "onError: " + e.message)
showError(e)
}
}))
}
/**
* Deleting a note
*/
private fun deleteNote(noteId: Int, position: Int)
{
Log.e(TAG, "deleteNote: $noteId, $position")
disposable.add(apiService.deleteNote(noteId).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(
object : DisposableCompletableObserver()
{
override fun onComplete()
{
Log.d(TAG, "Note deleted! $noteId")
// Remove and notify adapter about item deletion
noteList.removeAt(position)
mAdapter.notifyItemRemoved(position)
Toast.makeText(this#MainActivity, "Note deleted!", Toast.LENGTH_SHORT).show()
toggleEmptyNotes()
}
override fun onError(e: Throwable)
{
Log.e(TAG, "onError: " + e.message)
showError(e)
}
}))
}
/**
* Shows alert dialog with EditText options to enter / edit
* a note.
* when shouldUpdate=true, it automatically displays old note and changes the
* button text to UPDATE
*/
private fun showNoteDialog(shouldUpdate: Boolean, note: Note?, position: Int)
{
val layoutInflaterAndroid = LayoutInflater.from(applicationContext)
val view = layoutInflaterAndroid.inflate(R.layout.note_dialog, null)
val alertDialogBuilderUserInput = AlertDialog.Builder(this#MainActivity)
alertDialogBuilderUserInput.setView(view)
val inputNote = view.findViewById<EditText>(R.id.note)
val dialogTitle = view.findViewById<TextView>(R.id.dialog_title)
dialogTitle.setText(if (!shouldUpdate) getString(R.string.lbl_new_note_title) else getString(R.string.lbl_edit_note_title))
if (shouldUpdate && note != null)
{
inputNote.setText(note.note)
}
alertDialogBuilderUserInput.setCancelable(false).setPositiveButton(if (shouldUpdate) "update" else "save",
DialogInterface.OnClickListener { dialogBox, id -> })
.setNegativeButton("cancel", DialogInterface.OnClickListener { dialogBox, id -> dialogBox.cancel() })
val alertDialog = alertDialogBuilderUserInput.create()
alertDialog.show()
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(View.OnClickListener {
// Show toast message when no text is entered
if (TextUtils.isEmpty(inputNote.text.toString()))
{
Toast.makeText(this#MainActivity, "Enter note!", Toast.LENGTH_SHORT).show()
return#OnClickListener
} else
{
alertDialog.dismiss()
}
// check if user updating note
if (shouldUpdate && note != null)
{
// update note by it's id
updateNote(note.id, inputNote.text.toString(), position)
} else
{
// create new note
createNote(inputNote.text.toString())
}
})
}
/**
* Opens dialog with Edit - Delete options
* Edit - 0
* Delete - 0
*/
private fun showActionsDialog(position: Int)
{
val colors = arrayOf<CharSequence>("Edit", "Delete")
val builder = AlertDialog.Builder(this)
builder.setTitle("Choose option")
builder.setItems(colors) { dialog, which ->
if (which == 0)
{
showNoteDialog(true, noteList.get(position), position)
} else
{
deleteNote(noteList.get(position).id, position)
}
}
builder.show()
}
private fun toggleEmptyNotes()
{
if (noteList.size > 0)
{
noNotesView?.setVisibility(View.GONE)
} else
{
noNotesView?.setVisibility(View.VISIBLE)
}
}
/**
* Showing a Snackbar with error message
* The error body will be in json format
* {"error": "Error message!"}
*/
fun showError(e: Throwable)
{
var message = ""
try
{
if (e is IOException)
{
message = "No internet connection!"
}
else (e is HttpException)
{
var error = e as HttpException
var errorBody = error.response().errorBody().toString()
val jObj = JSONObject(errorBody)
message = jObj.getString("error")
}
}
catch (e1: IOException)
{
e1.printStackTrace()
}
catch (e1: JSONException)
{
e1.printStackTrace()
}
catch (e1: Exception)
{
e1.printStackTrace()
}
if (TextUtils.isEmpty(message))
{
message = "Unknown error occurred! Check LogCat.";
}
val snackbar = coordinatorLayout?.let { Snackbar.make(it, message, Snackbar.LENGTH_LONG) }
val sbView = snackbar?.getView()
val textView = sbView?.findViewById<TextView>(android.support.design.R.id.snackbar_text)
textView?.setTextColor(Color.YELLOW)
snackbar?.show()
}
fun whiteNotificationBar(view: View)
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
var flags = view.getSystemUiVisibility();
flags = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
view.setSystemUiVisibility(flags);
getWindow().setStatusBarColor(Color.WHITE);
}
}
override fun onDestroy()
{
super.onDestroy()
disposable.dispose()
}
}
I found out what the issue was. It seems by using the code complete option it ended up making the MainActivity class abstract
abstract class MainActivity : AppCompatActivity()
{
/// code base
}
instead of
class MainActivity : AppCompatActivity()
{
// code base
}