0

Why my Service is not running in background when if remove my app from background or remove from recent apps.

I very much confuse between IntentService and Service which one I should prefer.

I am making app where I will read all call log detail and will send automatic SMS therefore I need to run service background.

But the problem is when I'm running this app is working fine also when I'm minimizing it then also working fine but the moment when i closed or remove the apps from background it no longer working.

This is my Manifest file

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="mytraining.com.callrecord">

    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.READ_PHONE_NUMBERS" />
    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
    <uses-permission android:name="android.permission.REQUEST_COMPANION_RUN_IN_BACKGROUND" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ANSWER_PHONE_CALLS" />


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <service
            android:name=".PhoneReceiver"
            android:enabled="true"
            android:exported="true"></service>
        <receiver android:name=".PhoneCallReceiver">
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
            </intent-filter>
        </receiver>
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
   </application>
</manifest>

Is there any change need to do or not.

And also it's also not working in Above oreo

PhoneReceiver.java

public class PhoneReceiver extends Service {
    private static int lastState = TelephonyManager.CALL_STATE_IDLE;
    private static Date callStartTime;
    private static boolean isIncoming;
    private static String savedNumber;

    @Override
    public void onCreate() {
        super.onCreate();
        Toast.makeText(this, "Service is Running", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDestroy() {
    super.onDestroy();
        Intent restartIntent = new Intent("Restart");
        sendBroadcast(restartIntent);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        Toast.makeText(getApplicationContext(), "Service is Oncommand", Toast.LENGTH_SHORT).show();
        return super.onStartCommand(intent, flags, startId);
    }
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
  • IntentSerivce run in background thread. For Service you should create another thread by yourself. For your goal (load cursor) there are better way do it async - https://developer.android.com/reference/android/support/v4/content/CursorLoader – Artem Botnev Jun 27 '19 at 09:08

1 Answers1

1

Try using return start sticky but there is a catch to this some manufacturer like Vivo,Oppo,Mi will still kill your service and starting from Oreo you need to show notification to keep your service running else android will kill it .

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(getApplicationContext(), "Service is Oncommand", 
Toast.LENGTH_SHORT).show();
return START_STICKY;

For more info refer this link. https://developer.android.com/reference/android/app/Service#START_NOT_STICKY

Kishan Thakkar
  • 429
  • 4
  • 11