先上预览图
按照一般的方法,安卓6.0以下的设备获取打电话权限很简单,但是6.0以上就稍微麻烦一点。
下面的方法是适合6.0以上的。
ativity上:
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.superseppol.telephonedemo.MainActivity">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/funtion_diol"
android:layout_marginBottom="46dp"
android:layout_above="@+id/editText2"
android:layout_alignRight="@+id/callbutton"
android:layout_alignEnd="@+id/callbutton"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="@+id/editText2"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
android:text="@string/funtion_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/callbutton"
android:onClick="callButtonClickAction"
android:layout_marginTop="20dp"
android:layout_below="@+id/editText2"
android:layout_centerHorizontal="true"/>
申请权限:
<user-permissionandroid:name="android.permission.CALL_PHONE"/>
在java包里添加DialogUtil.java
packagecom.example.superseppol.telephonedemo;
/**
* Created by Super Seppol on 2017/3/2.
*/
importandroid.app.Activity;
importandroid.app.AlertDialog;
importandroid.content.Context;
importandroid.content.DialogInterface;
importandroid.view.WindowManager;
/**
*对话框工具箱
*
*@authorxiaopan
*/
public classDialogUtil
{
/**
*显示一个对话框
*
*@paramcontext上下文对象,最好给Activity,否则需要android.permission.SYSTEM_ALERT_WINDOW
*@paramtitle标题
*@parammessage消息
*@paramconfirmButton确认按钮
*@paramconfirmButtonClickListener确认按钮点击监听器
*@paramcenterButton中间按钮
*@paramcenterButtonClickListener中间按钮点击监听器
*@paramcancelButton取消按钮
*@paramcancelButtonClickListener取消按钮点击监听器
*@paramonShowListener显示监听器
*@paramcancelable是否允许通过点击返回按钮或者点击对话框之外的位置关闭对话框
*@paramonCancelListener取消监听器
*@paramonDismissListener销毁监听器
*@return对话框
*/
public staticAlertDialogshowAlert(Context context,String title,String message,
String confirmButton,DialogInterface.OnClickListener confirmButtonClickListener,
String centerButton,DialogInterface.OnClickListener centerButtonClickListener,
String cancelButton,DialogInterface.OnClickListener cancelButtonClickListener,
DialogInterface.OnShowListener onShowListener, booleancancelable,
DialogInterface.OnCancelListener onCancelListener,
DialogInterface.OnDismissListener onDismissListener)
{
AlertDialog.Builder promptBuilder =newAlertDialog.Builder(context);
if(title !=null)
{
promptBuilder.setTitle(title);
}
if(message !=null)
{
promptBuilder.setMessage(message);
}
if(confirmButton !=null)
{
promptBuilder.setPositiveButton(confirmButton,confirmButtonClickListener);
}
if(centerButton !=null)
{
promptBuilder.setNeutralButton(centerButton,centerButtonClickListener);
}
if(cancelButton !=null)
{
promptBuilder.setNegativeButton(cancelButton,cancelButtonClickListener);
}
promptBuilder.setCancelable(true);
if(cancelable)
{
promptBuilder.setOnCancelListener(onCancelListener);
}
AlertDialog alertDialog = promptBuilder.create();
if(!(contextinstanceofActivity))
{
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
}
alertDialog.setOnDismissListener(onDismissListener);
alertDialog.setOnShowListener(onShowListener);
alertDialog.show();
returnalertDialog;
}
/**
*显示一个对话框
*
*@paramcontext上下文对象,最好给Activity,否则需要android.permission.SYSTEM_ALERT_WINDOW
*@paramtitle标题
*@parammessage消息
*@paramconfirmButtonClickListener确认按钮点击监听器
*@paramcancelButtonClickListener取消按钮点击监听器
*@return对话框
*/
public staticAlertDialogshowConfirm(Context context,String title,String message,
DialogInterface.OnClickListener confirmButtonClickListener,
DialogInterface.OnClickListener cancelButtonClickListener)
{
returnshowAlert(context,title,message,"Yes",
confirmButtonClickListener, null, null,"No",cancelButtonClickListener, null, true, null, null);
}
/**
*显示一个提示框
*
*@paramcontext上下文对象,最好给Activity,否则需要android.permission.SYSTEM_ALERT_WINDOW
*@parammessage提示的消息
*@paramconfirmButton确定按钮的名字
*/
public staticAlertDialogshowPrompt(Context context,String message,String confirmButton)
{
returnshowAlert(context, null,message,confirmButton, null, null, null, null, null, null, true, null, null);
}
/**
*显示一个提示框
*
*@paramcontext上下文对象,最好给Activity,否则需要android.permission.SYSTEM_ALERT_WINDOW
*@parammessage提示的消息
*/
public staticAlertDialogshowPrompt(Context context,String message)
{
returnshowAlert(context, null,message,"OK", null, null, null, null, null, null, true, null, null);
}
}
再添加RequestPermissionType.java
packagecom.example.superseppol.telephonedemo;
/**
* Created by Super Seppol on 2017/3/2.
*/
public interfaceRequestPermissionType
{
/**
*请求打电话的权限码
*/
intREQUEST_CODE_ASK_CALL_PHONE=100;
}
最后的MainActivity上的代码:
packagecom.example.superseppol.telephonedemo;
importandroid.Manifest;
importandroid.app.Dialog;
importandroid.content.Context;
importandroid.content.DialogInterface;
importandroid.content.Intent;
importandroid.content.pm.PackageManager;
importandroid.net.Uri;
importandroid.os.Build;
importandroid.support.v4.app.ActivityCompat;
importandroid.support.v4.content.ContextCompat;
importandroid.support.v7.app.AppCompatActivity;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.widget.EditText;
importandroid.widget.Toast;
public classMainActivityextendsAppCompatActivity
{
/**
* context
*/
privateContextmContext;
privateEditTexteditText;
@Override
protected voidonCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.mContext=this;
editText=(EditText)findViewById(R.id.editText2);
}
/**
*点击打电话的按钮响应事件
*
*@paramviewview
*/
public voidcallButtonClickAction(View view)
{
//先new出一个监听器,设置好监听
DialogInterface.OnClickListener dialogOnclicListener =newDialogInterface.OnClickListener()
{
@Override
public voidonClick(DialogInterface dialog, intwhich)
{
switch(which)
{
caseDialog.BUTTON_POSITIVE:
Toast.makeText(MainActivity.this,"Yes"+ which,Toast.LENGTH_SHORT).show();
requestPermission();
break;
caseDialog.BUTTON_NEGATIVE:
Toast.makeText(MainActivity.this,"No"+ which,Toast.LENGTH_SHORT).show();
break;
caseDialog.BUTTON_NEUTRAL:
Toast.makeText(MainActivity.this,"Cancel"+ which,Toast.LENGTH_SHORT).show();
break;
}
}
};
//弹窗让用户选择,是否允许申请权限
DialogUtil.showConfirm(mContext,"申请权限","是否允许获取打电话权限?",dialogOnclicListener,dialogOnclicListener);
}
/**
*申请权限
*/
private voidrequestPermission()
{
//判断Android版本是否大于23
if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.M)
{
intcheckCallPhonePermission = ContextCompat.checkSelfPermission(mContext,Manifest.permission.CALL_PHONE);
if(checkCallPhonePermission != PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(this, newString[]{Manifest.permission.CALL_PHONE},
RequestPermissionType.REQUEST_CODE_ASK_CALL_PHONE);
return;
}
else
{
callPhone();
}
}
else
{
callPhone();
}
}
/**
*注册权限申请回调
*@paramrequestCode申请码
*@parampermissions申请的权限
*@paramgrantResults结果
*/
@Override
public voidonRequestPermissionsResult(intrequestCode,String[] permissions, int[] grantResults)
{
switch(requestCode)
{
caseRequestPermissionType.REQUEST_CODE_ASK_CALL_PHONE:
if(grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
callPhone();
}
else
{
// Permission Denied
Toast.makeText(MainActivity.this,"CALL_PHONE Denied",Toast.LENGTH_SHORT).show();
}
break;
default:
super.onRequestPermissionsResult(requestCode,permissions,grantResults);
}
}
/**
*拨号方法
*/
private voidcallPhone()
{
String phone=editText.getText().toString();
Intent intent =newIntent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:"+phone));
startActivity(intent);
}
}
网友评论