0

The BroadcastReceiver I created programmatically inside the Service didnot call onReceive method.I created a class that extends BroadcastReceiver inside Service class.Earlier when I just implemented it in manifest and ran the code it didnot work back then.Since Service runs in background continuously I thought the BroadcastReceiver would also run along with it.

Edit: Now I get an exception after declaring permissions programmatically.The BroadcastReceiver is triggered but I get the following exception.Please look below the code.

Code:

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Environment;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

import java.io.File;
import java.io.IOException;

/**
 * Created by Jobin on 15-03-2018.
 */

public class PhoneListenerService extends Service{
    private PhoneStateListener phoneStateListener;
    private TeleListener teleListener;
    private TelephonyManager telephonyManager;
    private File file;
    private OutgoingReceiver outgoingReceiver;
    @Override
    public void onCreate()
    {
        super.onCreate();
        outgoingReceiver=new OutgoingReceiver();
        IntentFilter intentFilter=new IntentFilter();
        intentFilter.addAction("android.intent.action.NEW_OUTGOING_CALL");
        registerReceiver(outgoingReceiver,intentFilter);
        file=new File(Environment.getExternalStorageDirectory().getAbsolutePath());
        telephonyManager=(TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        file=new File(Environment.getExternalStorageDirectory(),"AutoCall");
        if (!file.exists())
        {
            Log.e("File","Created");
            file.mkdir();
        }
        else
        {
            Log.e("File",file.getPath());
        }
        telephonyManager.listen(new TeleListener(getApplicationContext(),file.getAbsolutePath()),PhoneStateListener.LISTEN_CALL_STATE);
        Log.e("Oncreate","Service");

    }

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

        Log.e("OnCommand","Service");
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(outgoingReceiver);
    }

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

    public class OutgoingReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.e("Out","Track");
            String phone_number=intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
            Toast.makeText(context,"Outgoing call identified",Toast.LENGTH_SHORT).show();
        }
    }
}

Exception:

FATAL EXCEPTION: main                                                                                          Process: com.js.globemaster.autocallrecorder, PID: 7086
java.lang.RuntimeException: Unable to instantiate receiver com.js.globemaster.autocallrecorder.PhoneListenerService$OutgoingReceiver: java.lang.InstantiationException: java.lang.Class<com.js.globemaster.autocallrecorder.PhoneListenerService$OutgoingReceiver> has no zero argument constructor
                                                                                               at android.app.ActivityThread.handleReceiver(ActivityThread.java:3681)
                                                                                               at android.app.ActivityThread.access$2000(ActivityThread.java:229)
                                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1903)
                                                                                               at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                               at android.os.Looper.loop(Looper.java:148)
                                                                                               at android.app.ActivityThread.main(ActivityThread.java:7325)
                                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
                                                                                            Caused by: java.lang.InstantiationException: java.lang.Class<com.js.globemaster.autocallrecorder.PhoneListenerService$OutgoingReceiver> has no zero argument constructor
                                                                                               at java.lang.Class.newInstance(Native Method)
                                                                                               at android.app.ActivityThread.handleReceiver(ActivityThread.java:3676)
                                                                                               at android.app.ActivityThread.access$2000(ActivityThread.java:229) 
                                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1903) 
                                                                                               at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                               at android.os.Looper.loop(Looper.java:148) 
                                                                                               at android.app.ActivityThread.main(ActivityThread.java:7325) 
                                                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
jobin
  • 1,489
  • 5
  • 27
  • 52

1 Answers1

1

You need to have PROCESS_OUTGOING_CALLS permission.

Because this permission is dangerous, you need to request it programmatically.

Edit:

For Broadcast Receiver add default constructor. And you don't add <receiver> to manifest when you are registering at run-time.

Afshin
  • 8,839
  • 1
  • 18
  • 53
  • I havent declared permission programmatically for listening to incoming calls.But it is working. – jobin Mar 21 '18 at 15:04
  • Ia that not dangerous – jobin Mar 21 '18 at 15:04
  • @jobin https://developer.android.com/reference/android/Manifest.permission.html#PROCESS_OUTGOING_CALLS protection level:dangerous – Afshin Mar 21 '18 at 15:05
  • I havent declared permission programmatically for READ_PHONE_STATE except in manifest,but I can check incoming calls. – jobin Mar 21 '18 at 15:11
  • @jobin Some permissions needs to be able to be requested at runtime (i.e. programmatically).... Imagine your phone cam firing without telling you it is starting... So just try what he said and see the results for yourself – Jason Krs Mar 21 '18 at 15:19
  • Will try it now – jobin Mar 21 '18 at 15:21
  • It works now,but I get an exception:It says the receiver has no zero argument constructor.So what should i do now – jobin Mar 21 '18 at 15:29
  • @Afshin Edited.Please look into it – jobin Mar 21 '18 at 15:37
  • @jobin check here: https://stackoverflow.com/questions/29947038/java-lang-instantiationexception-class-has-no-zero-argument-constructor – Afshin Mar 21 '18 at 15:43
  • The receiever is non static but it is reigistered inside the parent i.e..class that extends Service class – jobin Mar 21 '18 at 15:47
  • If I am programmatically doing it do I have to remove from manifest? – jobin Mar 21 '18 at 15:48
  • @jobin O.O yea.... you never do both at same time.... and add default constructor too for your Broadcast Receiver. – Afshin Mar 21 '18 at 15:49
  • It works bro...Thanks a lot.Could you edit your answer so that mark it as an answer – jobin Mar 21 '18 at 15:51