在應(yīng)用內(nèi)部如何使用撥打電話這個功能?
相信各位老哥老姐都用到過牍汹,或在即將用到的路上 --谷哥的炊事班班長。
著重關(guān)注的兩點(diǎn):
1 .在Activity中撥打電話呢焰坪?亦或者是在Fragment中撥打電話呢绒窑?
2 .在Android6.0版本以上撥打電話呢灌砖?亦或者是在Android6.0版本以下?lián)艽螂娫捘兀?/p>
------------------------------------------下面請看代碼:------------------------------------------
撥號代碼(在Activity中使用):
AndroidManifest.xml : <uses-permission android:name="android.permission.CALL_PHONE" />
/**
* 判斷版本號撥打電話
*/
private void canCallPhone() {
//TODO 撥打電話 獲取運(yùn)行時(shí)權(quán)限
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
//申請 CALL_PHONE 權(quán)限
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, "您沒有給我權(quán)限呀,如需撥打電話,請重新給我授權(quán)", Toast.LENGTH_SHORT).show();
}
}
}
如上望侈,就是使用撥號功能所需要的主要代碼印蔬。
在Activity中使用撥號功能:
如果您是在Activity中使用,直接調(diào)用即可脱衙。
MainActivity.this.findViewById(R.id.activityCallPhone).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
canCallPhone();
}
});
在Fragment中使用撥號功能:
1.需要在Fragment中發(fā)送一個廣播。
inflate.findViewById(R.id.fragmentCallPhone).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//TODO 向主頁面發(fā)送廣播拉起撥號頁面
Intent startActivity = new Intent("callPhone");
MyFragment.this.getActivity().sendBroadcast(startActivity);
}
});
2.在Fragment附屬的Activity中注冊廣播并重寫其onReceive函數(shù)例驹。
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();
}
/**
* 設(shè)置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 撥打電話 獲取運(yùn)行時(shí)權(quán)限
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
//申請 CALL_PHONE 權(quán)限
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);
}
}
/**
* 設(shè)置Fragment中撥打電話
*/
private void setFragmentButtonOnClick() {
setFragment();
//TODO 接受MyFragment發(fā)送的廣播
IntentFilter intentFilter = new IntentFilter("callPhone");
BroadcastReceiver phoneBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
canCallPhone();
}
};
MainActivity.this.registerReceiver(phoneBroadcastReceiver, intentFilter);
}
/**
* 把Fragment塞進(jìn)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, "您沒有給我權(quán)限呀,如需撥打電話,請重新給我授權(quán)", 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 向主頁面發(fā)送廣播拉起撥號頁面
Intent startActivity = new Intent("callPhone");
MyFragment.this.getActivity().sendBroadcast(startActivity);
}
});
return inflate;
}
}
如有問題請您批評指正捐韩,互相幫助使我們更好的前行。