0

i create app that recive and recive sms and i want to keep working in background it just recive and read sms to sending to server and i need to keep working in background even if it killed and i want it to launch automatically when the phone is turned on

manifest

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

<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.READ_PHONE_NUMBERS"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
<uses-feature
    android:name="android.software.leanback"
    android:required="true" />
<uses-feature
    android:name="android.hardware.touchscreen"
    android:required="false" />

<application

    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.WebView"
    tools:ignore="MissingTvBanner">
    <activity
        android:name=".MainActivity"
        android:exported="true">
        <intent-filter>
            <category android:name="android.intent.category.LEANBACK_LAUNCHER"/>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service android:name=".ExampleService" android:stopWithTask="false" />

</application>

ExampleService

Class ExampleService : Service() {


    @RequiresApi(Build.VERSION_CODES.O)
    override fun onCreate() {
        super.onCreate()

    }

    override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {

        var bs:BroadcastReceiver = MyBroadcastReceiver()
        val filter = IntentFilter(Telephony.Sms.Intents.SMS_RECEIVED_ACTION).apply {
            addAction(Telephony.Sms.Intents.SMS_DELIVER_ACTION)
        }
        registerReceiver(bs,filter)
        return START_STICKY
    }

    override fun onBind(intent: Intent): IBinder? {

        return null
    }

    override fun onDestroy() {
    }
}
  • Does this answer your question? [How to make an android app to always run in background?](https://stackoverflow.com/questions/34573109/how-to-make-an-android-app-to-always-run-in-background) – xerx593 Nov 11 '21 at 10:45

0 Answers0