我在服务中以编程方式创建的BroadcastReceiver没有调用onReceive方法。我创建了一个在Service类中扩展BroadcastReceiver的类。当我刚刚在manifest中实现它并运行代码它之前没有工作的时候。因为服务在后台运行我一直认为BroadcastReceiver也会随之而行。
编辑: 现在我以编程方式声明权限后得到一个异常.MincastReceiver被触发但我得到以下异常。请看下面的代码。
代码:
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.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)
答案 0 :(得分:1)
您需要拥有PROCESS_OUTGOING_CALLS权限。
由于此权限很危险,因此您需要以编程方式请求它。
修改强>
对于Broadcast Receiver,添加默认构造函数。当您在运行时注册时,不要将<receiver>
添加到清单。