【发布时间】:2014-10-02 15:24:37
【问题描述】:
我已经设法编写代码来阻止来电,但它在不同的类中,我想在用户在对话框上按“是”时执行它。如何调用 onReceive()?我作为参数传递的意图是什么?
这是 MainActivity 的代码 -
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
activateButton = (Button)findViewById(R.id.activate);
activateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DialogBox();
}
});
}
protected void DialogBox() {
box = new AlertDialog.Builder(this);
box.setTitle("Reject incoming calls?").
setMessage("On activation, your phone will reject all incoming calls").setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//BLOCK CALLS
}
}).setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
final AlertDialog alert = box.create();
alert.show();
}
这是扩展 BroadcastReceiver 的类。 onReceive 保存拒绝呼叫的代码。
public class RejectCall extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
ITelephony telephonyService;
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
try {
Class c = Class.forName(tm.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
telephonyService = (ITelephony) m.invoke(tm);
Bundle bundle = intent.getExtras();
String phoneNumber = bundle.getString("incoming_number");
Log.d("INCOMING", phoneNumber);
if ((phoneNumber != null)) {
telephonyService.endCall();
Log.d("HANG UP", phoneNumber);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
另外,要执行这样的操作,我必须在 AndroidManifest 中声明哪些权限?
注意 - 我遇到过类似的问题,但似乎没有一个是在调用 onReceive(),因此是这个问题。
谢谢!
【问题讨论】:
标签: java android broadcastreceiver android-manifest