Murat
Murat

Reputation: 11

Broadcast receiver and Service don't work when applicaton close (android)

Sory everyone. Firstly my receiver doesnt work when application is closed on my devices because of AUTO START MANAGER. I feel stupid... And I learned very important things when I was trying to solve it.

Firstly Android 6.0 Permission Request Broadcast Receivers not working in Android 6.0 Marshmallow

Secondly: Android - duplicated phone state broadcast on Lollipop

http://www.skoumal.net/en/android-duplicated-phone-state-broadcast-on-lollipop/

Thanks...

I'm new on Android and I want to learn BroadcastReceiver&Service. This codes BroadcastReceiver listen offhook and idle state my phone and send intent Service do something. While application running everything normal. After close application BroadcastReceiver and Service don't work.

Update: Also I notice while application run and everythng normal, sevice were started twice and stopped twice.

I see this messages while service is starting:

  1. Toast.makeText(context, "" +phoneState +" " +record, Toast.LENGTH_SHORT).show();

  2. Toast.makeText(getApplicationContext(), "service work", Toast.LENGTH_SHORT).show();

again 1. Toast.makeText(context, "" +phoneState +" " +record, Toast.LENGTH_SHORT).show();

again 2. Toast.makeText(getApplicationContext(), "service work", Toast.LENGTH_SHORT).show()

I see this messages while service is stoping:

  1. Toast.makeText(context, "" +phoneState +" " +record, Toast.LENGTH_SHORT).show();

  2. Toast.makeText(getApplicationContext(), "service will be stopped.", Toast.LENGTH_SHORT).show();

again 1. Toast.makeText(context, "" +phoneState +" " +record, Toast.LENGTH_SHORT).show();

again 2. Toast.makeText(getApplicationContext(), "service will be stopped.", Toast.LENGTH_SHORT).show();

Everything repeat.

Manifest:

<?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="devapp.deneme">

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:enabled="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <service
            android:name=".MyService"
            android:enabled="true"
            android:exported="true" />

        <receiver
            android:name="devapp.deneme.MyReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
                <action android:name="android.intent.action.PHONE_STATE"/>
            </intent-filter>
        </receiver>
    </application>

</manifest>

Broadcast Receiver

package devapp.deneme;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.widget.Toast;

public class MyReceiver extends BroadcastReceiver {

    public MyReceiver(){

    }

    Intent service = null;
    Bundle bundle = null;
    String phoneState = null;
    boolean record = false;

    @Override
    public void onReceive(Context context, Intent intent) {

        service = new Intent(context, MyService.class);
        bundle = intent.getExtras();
        phoneState = bundle.getString(TelephonyManager.EXTRA_STATE);

        if (phoneState!=null){
            if ((phoneState.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))||(phoneState.equals(TelephonyManager.EXTRA_STATE_IDLE))){
                service.putExtra("durum", phoneState);
                if (phoneState.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
                    record = true;
                }
                else {
                    record = false;
                }
            }
            service.putExtra("record",record);
            Toast.makeText(context, "" +phoneState +" " +record, Toast.LENGTH_SHORT).show();
            context.startService(service);
        }
    }
}

Service

package devapp.deneme;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

public class MyService extends Service {
    public MyService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

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

        boolean record = intent.getBooleanExtra("record", false);
        if (record) {
            Toast.makeText(getApplicationContext(), "service work", Toast.LENGTH_SHORT).show();
        }
        else {
            Toast.makeText(getApplicationContext(), "service will be stopped.", Toast.LENGTH_SHORT).show();
            stopSelf();
        }
        return super.onStartCommand(intent, flags, startId);
    }
}

Upvotes: 1

Views: 1614

Answers (2)

Mithun Sarker
Mithun Sarker

Reputation: 4023

Start the service as sticky. Change your onStartCommand method of service like following

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

    boolean record = intent.getBooleanExtra("record", false);
    if (record) {
        Toast.makeText(getApplicationContext(), "service work", Toast.LENGTH_SHORT).show();
    }
    else {
        Toast.makeText(getApplicationContext(), "service will be stopped.", Toast.LENGTH_SHORT).show();
        stopSelf();
    }
     return START_STICKY;  // or START_REDELIVER_INTENT or START_STICKY_COMPATIBILITY
}

Upvotes: 1

Parvinder Maan
Parvinder Maan

Reputation: 46

You have to add the following in service class

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

           // your business logic 

        return START_STICKY;
}

Upvotes: 1

Related Questions