在应用内部如何使用拨打电话这个功能?
相信各位老哥老姐都用到过,或在即将用到的路上 --谷哥的炊事班班长。
着重关注的两点:
1 .在Activity中拨打电话呢?亦或者是在Fragment中拨打电话呢?
2 .在Android6.0版本以上拨打电话呢?亦或者是在Android6.0版本以下拨打电话呢?
------------------------------------------下面请看代码:------------------------------------------
拨号代码(在Activity中使用):
AndroidManifest.xml : <uses-permission android:name="android.permission.CALL_PHONE" />
/**
* 判断版本号拨打电话
*/
private void canCallPhone() {
//TODO 拨打电话 获取运行时权限
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
//申请 CALL_PHONE 权限
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CALL_PHONE},
0);
}
}
else {
Intent intentPhone = new Intent(Intent.ACTION_DIAL);
intentPhone.setData(Uri.parse("tel:19999999999"));
MainActivity.this.startActivity(intentPhone);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
doNext(requestCode, grantResults);
}
private void doNext(int requestCode, int[] grantResults) {
if (requestCode == 0) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Intent intentPhone = new Intent(Intent.ACTION_DIAL);
intentPhone.setData(Uri.parse("tel:19999999999"));
MainActivity.this.startActivity(intentPhone);
} else {
Toast.makeText(this, "您没有给我权限呀,如需拨打电话,请重新给我授权", Toast.LENGTH_SHORT).show();
}
}
}
如上,就是使用拨号功能所需要的主要代码。
在Activity中使用拨号功能:
如果您是在Activity中使用,直接调用即可。
MainActivity.this.findViewById(R.id.activityCallPhone).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
canCallPhone();
}
});
在Fragment中使用拨号功能:
1.需要在Fragment中发送一个广播。
inflate.findViewById(R.id.fragmentCallPhone).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//TODO 向主页面发送广播拉起拨号页面
Intent startActivity = new Intent("callPhone");
MyFragment.this.getActivity().sendBroadcast(startActivity);
}
});
2.在Fragment附属的Activity中注册广播并重写其onReceive函数。
IntentFilter intentFilter = new IntentFilter("callPhone");
BroadcastReceiver phoneBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
canCallPhone();
}
};
MainActivity.this.registerReceiver(phoneBroadcastReceiver, intentFilter);
下面贴一下整个Demo的完整代码:
- 布局文件(.xml):
==============我是activity_mian.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.iwant.callphone.MainActivity">
<Button
android:id="@+id/activityCallPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textAllCaps="false"
android:text="Activity中拨打电话" />
<FrameLayout
android:id="@+id/iAmAFragment"
android:layout_marginTop="68dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
==============我是fragment_mian.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.iwant.callphone.MainActivity">
<Button
android:id="@+id/fragmentCallPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textAllCaps="false"
android:text="Fragment中拨打电话" />
</LinearLayout>
- 源代码文件(.java):
==============我是MainActivity.java
package com.iwant.callphone;
import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;
/**
* Created by YangFan on 2018/05/27.
*/
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setActivityButtonOnClick();
setFragmentButtonOnClick();
}
/**
* 设置Activity中拨打电话
*/
private void setActivityButtonOnClick() {
MainActivity.this.findViewById(R.id.activityCallPhone).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
canCallPhone();
}
});
}
/**
* 判断版本号拨打电话
*/
private void canCallPhone() {
//TODO 拨打电话 获取运行时权限
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
//申请 CALL_PHONE 权限
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CALL_PHONE},
0);
}} else {
Intent intentPhone = new Intent(Intent.ACTION_DIAL);
intentPhone.setData(Uri.parse("tel:19999999999"));
MainActivity.this.startActivity(intentPhone);
}
}
/**
* 设置Fragment中拨打电话
*/
private void setFragmentButtonOnClick() {
setFragment();
//TODO 接受MyFragment发送的广播
IntentFilter intentFilter = new IntentFilter("callPhone");
BroadcastReceiver phoneBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
canCallPhone();
}
};
MainActivity.this.registerReceiver(phoneBroadcastReceiver, intentFilter);
}
/**
* 把Fragment塞进FrameLayout
*/
private void setFragment() {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.iAmAFragment, new MyFragment()).commit();
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
doNext(requestCode, grantResults);
}
private void doNext(int requestCode, int[] grantResults) {
if (requestCode == 0) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Intent intentPhone = new Intent(Intent.ACTION_DIAL);
intentPhone.setData(Uri.parse("tel:19999999999"));
MainActivity.this.startActivity(intentPhone);
} else {
Toast.makeText(this, "您没有给我权限呀,如需拨打电话,请重新给我授权", Toast.LENGTH_SHORT).show();
}
}
}
}
==============我是MyFragment.java
package com.iwant.callphone;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by YangFan on 2018/05/27.
*/
public class MyFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View inflate = inflater.inflate(R.layout.fragment_main, container, false);
inflate.findViewById(R.id.fragmentCallPhone).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//TODO 向主页面发送广播拉起拨号页面
Intent startActivity = new Intent("callPhone");
MyFragment.this.getActivity().sendBroadcast(startActivity);
}
});
return inflate;
}
}
如有问题请您批评指正,互相帮助使我们更好的前行。