milosev.com
  • Home
    • List all categories
    • Sitemap
  • Downloads
    • WebSphere
    • Hitachi902
    • Hospital
    • Kryptonite
    • OCR
    • APK
  • About me
    • Gallery
      • Italy2022
      • Côte d'Azur 2024
    • Curriculum vitae
      • Resume
      • Lebenslauf
    • Social networks
      • Facebook
      • Twitter
      • LinkedIn
      • Xing
      • GitHub
      • Google Maps
      • Sports tracker
    • Adventures planning
  1. You are here:  
  2. Home
  3. Android

android:process=":MyService"

Details
Written by: Stanko Milosev
Category: Android
Published: 12 May 2022
Last Updated: 12 May 2022
Hits: 1317
  • kotlin
In order to create never ending foreground service in manifest I have added android:process=":MyService" so it looks like:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.broadcasts">

    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Broadcasts"
        tools:targetApi="31">
        <service
            android:name=".MyService"
            android:enabled="true"
            android:exported="true"
            android:process=":MyService"/>

        <receiver
            android:name=".MyReceiver"
            android:enabled="true"
            android:exported="true" />

        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <action android:name="android.intent.action.AIRPLANE_MODE" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Few more examples of broadcast receiver declared in manifest

Details
Written by: Stanko Milosev
Category: Android
Published: 12 May 2022
Last Updated: 12 May 2022
Hits: 1239
  • kotlin
In File -> New -> Other -> BroadCastReceiver I have added BroadCastReceiver receiver class, and in manifest I have added for example:
<action android:name="android.intent.action.AIRPLANE_MODE"/>
Now my AndroidManifest.xml looks like:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.broadcasts">

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Broadcasts"
        tools:targetApi="31">
        <receiver
            android:name=".MyReceiver"
            android:enabled="true"
            android:exported="true" />

        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <action android:name="android.intent.action.AIRPLANE_MODE"/>
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
Where MyReceiver.kt looks like:
package com.example.broadcasts

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent

class MyReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) {
        // This method is called when the BroadcastReceiver is receiving an Intent broadcast.
        TODO("MyReceiver.onReceive() is not implemented")
    }
}
MainActivity.kt looks like:

package com.example.broadcasts

import android.content.BroadcastReceiver
import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

    val br: BroadcastReceiver = MyReceiver()

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

        val filter = IntentFilter("android.intent.action.AIRPLANE_MODE").apply {
            addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED)
        }
        registerReceiver(br, filter)
    }

    fun onClick(view: View) {
        sendBroadcast(Intent(this, MyReceiver::class.java).setAction("MyAction"))
    }
}
With code:
    fun onClick(view: View) {
        sendBroadcast(Intent(this, MyReceiver::class.java).setAction("MyAction"))
    }
I am sending my own "MyAction"

SharedPreferences

Details
Written by: Stanko Milosev
Category: Android
Published: 09 May 2022
Last Updated: 11 May 2022
Hits: 1215
  • kotlin
Here is my example of SharedPreferences:
package com.milosev.myapplication

import android.content.SharedPreferences
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

    private lateinit var sharedPreferences: SharedPreferences

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        sharedPreferences = getSharedPreferences("userdetails", MODE_PRIVATE)
    }

    override fun onStart() {
        super.onStart()
        val username = sharedPreferences.getString("username", "")
        System.out.println(username)
    }

    override fun onStop() {
        super.onStop()
        val edit: SharedPreferences.Editor = sharedPreferences.edit()
        edit.putString("username", "test")
        edit.commit()
    }
}

stopForeground

Details
Written by: Stanko Milosev
Category: Android
Published: 02 May 2022
Last Updated: 02 May 2022
Hits: 1498
  • kotlin
In this article I gave an example on how to start foreground service, but not how to stop it.

In MyService.kt add the code:

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
	if (intent?.action.equals("stopForeground")) {
		stopForeground(true)
		stopSelfResult(startId)
	}
	return super.onStartCommand(intent, flags, startId)
}
and in MainActivity.kt I have added new button and onClick:
@RequiresApi(Build.VERSION_CODES.O)
fun stopForegroundServiceAndBroadcastReceiverClick(view: View) {
	val intent = Intent(this, MyService::class.java)
	intent.action = "stopForeground"
	startForegroundService(intent)
}
From here.
  1. startForegroundService and BroadcastReceiver
  2. Request location updates
  3. Write file on internal storage
  4. Small example on how to get gps Location

Page 5 of 11

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10