This example works only in Android Studio emulator and with accepting all certificates, and it is made with Retrofit library.
First example of Web Api in .NET Core:
using Microsoft.AspNetCore.Mvc;
namespace WebApi.Controllers;
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
[HttpGet]
public string Get()
{
return "test";
}
[HttpGet("{id}")]
public string Get(int id)
{
return "value";
}
[HttpPost]
public string Post([FromBody] string value)
{
return $"Sent: {value}";
}
}
Now example in Kotlin.
In \app\src\main\AndroidManifest.xml I have added internet permissions:
Here I gave one example of never ending application, which, unfortunately, after latest Huwaei update on my phone is not working. I gonna leave that article as is, and here I will show another possible example using mix of my original article and this and this answer.
I have added Service and BroadcastReceiver, in Manifest I have added permissions:
While I was trying to create my tracking application in Android, I have noticed that application is getting killed, from time to time. After some research I have found this web site, which it seems that solved my problem, but please note that I am testing only on one device, since I am doing it for my self as a hobby.
First I have started empty activity.
Second I have added File -> New -> Service -> Service
Last one I have added File -> New -> Other -> Broadcast Receiver
In the manifest I added FOREGROUND_SERVICE permission:
package com.milosev.getgpslocation
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.Service
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Build
import android.os.CountDownTimer
import android.os.IBinder
import androidx.annotation.RequiresApi
import androidx.core.app.NotificationCompat
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import kotlinx.coroutines.*
class MyService : Service(),CoroutineScope by MainScope() {
private var job: Job? = null
override fun onBind(intent: Intent): IBinder {
TODO("Return the communication channel to the service.")
}
@RequiresApi(Build.VERSION_CODES.O)
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
when (intent?.action) {
"startForeground" -> {
val channelId = createNotificationChannel("my_service", "My Background Service")
val notificationBuilder = NotificationCompat.Builder(this, channelId)
val notification = notificationBuilder.setOngoing(true)
.setContentTitle("test")
.setContentText("test")
.setSmallIcon(R.mipmap.ic_launcher)
.setPriority(1)
.setCategory(Notification.CATEGORY_SERVICE)
.build()
startForeground(101, notification)
val context = this
job = launch {
while(true) {
val myIntent = Intent(context, MyBroadcastReceiver::class.java).setAction("MyAction")
sendBroadcast(myIntent);
delay(1_000)
}
}
}
}
if (intent?.action.equals("stopForeground")) {
job?.cancel()
stopForeground(true)
stopSelfResult(startId)
}
return START_STICKY;
}
@RequiresApi(Build.VERSION_CODES.O)
private fun createNotificationChannel(channelId: String, channelName: String): String {
val chan = NotificationChannel(
channelId,
channelName, NotificationManager.IMPORTANCE_LOW
)
chan.lightColor = Color.RED
chan.lockscreenVisibility = Notification.VISIBILITY_PRIVATE
val service = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
service.createNotificationChannel(chan)
return channelId
}
override fun onDestroy() {
super.onDestroy()
val myIntent = Intent(this, MyBroadcastReceiver::class.java).setAction("Restart")
sendBroadcast(myIntent);
}
override fun onTaskRemoved(rootIntent: Intent?) {
super.onTaskRemoved(rootIntent)
val myIntent = Intent(this, MyBroadcastReceiver::class.java).setAction("Restart")
sendBroadcast(myIntent);
}
}
POI:
override fun onDestroy() {
super.onDestroy()
val myIntent = Intent(this, MyBroadcastReceiver::class.java).setAction("Restart")
sendBroadcast(myIntent);
}
override fun onTaskRemoved(rootIntent: Intent?) {
super.onTaskRemoved(rootIntent)
val myIntent = Intent(this, MyBroadcastReceiver::class.java).setAction("Restart")
sendBroadcast(myIntent);
}
In main activity:
@RequiresApi(Build.VERSION_CODES.O)
override fun onResume() {
super.onResume()
LocalBroadcastManager.getInstance(this)
.registerReceiver(broadCastReceiver, IntentFilter("MyAction"))
val intent = Intent(this, MyService::class.java)
intent.action = "startForeground"
startForegroundService(intent)
}
Notice that there is a problem with onResume because it will start the service immediately.
Edit: 2022-06-05:
Unfortunately after latest update of Huawei my app is again getting killed. Here you can find informations about this issuse.