Android 7.1 如何在源码做一个按键点击就拨打120
时间: 2024-03-10 19:46:51 浏览: 15
要在Android 7.1源码中实现按键点击就拨打120需要进行以下步骤:
1. 打开源码中的Telephony应用程序,找到DialpadFragment.java文件;
2. 在DialpadFragment.java文件中,找到onViewCreated()方法,这个方法是在视图创建后被调用的;
3. 在onViewCreated()方法中,添加一个OnClickListener监听器,当用户点击按键时,会触发这个监听器;
4. 在OnClickListener监听器中,使用Intent.ACTION_CALL动作和URI的形式来拨打电话号码。
下面是实现的代码示例:
```
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// 找到拨号盘的按键
Button button = (Button) view.findViewById(R.id.button);
// 添加 OnClickListener 监听器
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 拨打电话号码 120
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:120"));
startActivity(intent);
}
});
}
```
注意:为了能够拨打电话,需要在AndroidManifest.xml文件中添加CALL_PHONE权限。