我正在尝试实现自己的电话处理 UI。
我想做的是,如果有来电,显示来电号码和图片,如果我按下按钮,来电将被接听/接听。
相关代码为:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
answerButton = (Button) findViewById(R.id.pickup);
answerButton.setOnClickListener(new OnClickListener() {
public void onClick(final View v) {
Intent intent = new Intent("android.intent.action.ANSWER");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
可悲的是,代码不起作用。起初,如果我按下我的回答按钮,则会引发异常:
ActivityNotFoundException:未找到处理 Intent {
act=android.intent.action.ANSWER的活动
然后我在 AndroidManifest.xml 中添加了一个条目:
<uses-permission android:name="android.permission.CALL_PHONE" />
我再次运行该应用程序,不再有异常。但是,我怀疑来电并没有真正被接受。因为如果按下Android的屏幕接听按钮(绿色按钮),来电被接受,并且模拟器屏幕的左上角也会显示一个绿色的通话图标,而我的应用程序没有。
我还在 android 源代码中阅读了 Phone 应用程序的源代码。Phone类中有acceptCall()等方法。但是这些代码对我来说似乎很难使用,因为代码中有很多导入声明,例如:
import com.android.internal.telephony.Call;
import com.android.internal.telephony.CallStateException;
import com.android.internal.telephony.CallerInfo;
import com.android.internal.telephony.CallerInfoAsyncQuery;
import com.android.internal.telephony.Connection;
import com.android.internal.telephony.MmiCode;
import com.android.internal.telephony.Phone;
而且,如果我在代码中添加这些导入,将会出现太多错误,例如 :
The import com.android.internal.telephony cannot be resolved
。
我的问题的正确和简单的方法是什么?