未触发传出呼叫的广播接收器

broadcastreceiver for outgoingcall not fired

我在服务中以编程方式创建的 BroadcastReceiver 没有调用 onReceive method.I 创建了一个 class 扩展了服务 class.Earlier 中的 BroadcastReceiver 当我刚刚在清单中实现它时 运行 它没有返回的代码 then.Since 服务 运行 持续在后台运行我认为 BroadcastReceiver 也会 运行 随之而来。

编辑: 现在我在声明权限后得到一个异常 programmatically.The BroadcastReceiver 被触发但是我得到以下 exception.Please 看下面的代码。

代码:

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();
        }
    }
}

异常:

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.access00(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.access00(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) 

您需要有 PROCESS_OUTGOING_CALLS 权限。

因为这个权限是危险的,你需要以编程方式请求它。

编辑:

为广播接收器添加默认构造函数。当您在 run-time.

注册时,您不会添加 <receiver> 来显示