Search code examples
androidservicebroadcastreceivertelephony

Android Broadcast Receiver in Service


I'm trying to make service that will do something each time phone call is ended. My problem is that service doesn't receive any broadcasts. I've implemented it like that:

    package com.kubasienki.spedycja;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;

/**
 * Created by kuba on 11/4/2015.
 */

    public class FirstService extends Service {

        private static String TAG = "kurwa";
    final BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if(action.equals("android.provider.Telephony.CALL_STATE_IDLE")){
                Log.v("kurwa", "skonczono");
            }
            else if(action.equals(android.telephony.TelephonyManager.ACTION_PHONE_STATE_CHANGED)){
                Log.v("kurwa", "???");
            }
            else {
                Log.v("kurwa", "??!!!?");

            }
        }

    };

        @Override
        public IBinder onBind(Intent arg0) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public void onStart(Intent intent, int startId) {
            // TODO Auto-generated method stub

            super.onStart(intent, startId);
            Log.d(TAG, "FirstService started");


            IntentFilter filter = new IntentFilter();
            filter.addAction("android.provider.TelephonyManager.CALL_STATE_IDLE");
            filter.addAction(android.telephony.TelephonyManager.ACTION_PHONE_STATE_CHANGED);


            registerReceiver(receiver, filter);
            //this.stopSelf();
        }

        @Override
        public void onDestroy() {
            // TODO Auto-generated method stub
            super.onDestroy();
            Log.d(TAG, "FirstService destroyed");
        }

    }

Manifest:

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

    <uses-permission android:name="android.permission.CALL_PHONE" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar.FullScreen" >
        <uses-permission android:name="android.permission.CALL_PHONE" />
        <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
        <activity android:name="com.kubasienki.spedycja.MainActivity"
            android:screenOrientation="portrait"
            android:configChanges="orientation|keyboardHidden">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

And starting service :

startService(new Intent(this, FirstService.class));

Solution

  • Perrmissions were in the wrong place.