Android实现拨号功能

Android实现拨号功能,第1张

请检查你这个activity的permission,有没有加上以下的设置。

在AndroidManifest.xml里面添加。

<uses-permission android:name="android.permission.BROADCAST_STICKY" />

<uses-permission android:name="android.permission.CALL_PHONE" />

<uses-permission android:name="android.permission.CALL_PRIVILEGED" />

<uses-permission android:name="android.permission.INTERNET" />

<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />

另外帮你贴下android自带拨号程序的核心部分,请自己参照下吧~~

public class OutgoingCallBroadcaster extends Activity {

private static final String PERMISSION = android.Manifest.permission.PROCESS_OUTGOING_CALLS

private static final String TAG = "OutgoingCallBroadcaster"

private static final boolean LOGV = true//Config.LOGV

public static final String EXTRA_ALREADY_CALLED = "android.phone.extra.ALREADY_CALLED"

public static final String EXTRA_ORIGINAL_URI = "android.phone.extra.ORIGINAL_URI"

private Phone mPhone

@Override

protected void onCreate(Bundle icicle) {

super.onCreate(icicle)

mPhone = PhoneApp.getInstance().phone

Intent intent = getIntent()

if (LOGV) Log.v(TAG, "onCreate: Got intent " + intent + ".")

String action = intent.getAction()

String number = PhoneNumberUtils.getNumberFromIntent(intent, this)

if (number != null) {

number = PhoneNumberUtils.stripSeparators(number)

}

final boolean emergencyNumber =

(number != null) &&PhoneNumberUtils.isEmergencyNumber(number)

boolean callNow

if (getClass().getName().equals(intent.getComponent().getClassName())) {

// If we were launched directly from the OutgoingCallBroadcaster,

// not one of its more privileged aliases, then make sure that

// only the non-privileged actions are allowed.

if (!Intent.ACTION_CALL.equals(intent.getAction())) {

Log.w(TAG, "Attempt to deliver non-CALL actionforcing to CALL")

intent.setAction(Intent.ACTION_CALL)

}

}

/* Change CALL_PRIVILEGED into CALL or CALL_EMERGENCY as needed. */

if (Intent.ACTION_CALL_PRIVILEGED.equals(action)) {

action = emergencyNumber

? Intent.ACTION_CALL_EMERGENCY

: Intent.ACTION_CALL

intent.setAction(action)

}

if (Intent.ACTION_CALL.equals(action)) {

if (emergencyNumber) {

finish()

return

}

callNow = false

} else if (Intent.ACTION_CALL_EMERGENCY.equals(action)) {

if (!emergencyNumber) {

finish()

return

}

callNow = true

} else {

finish()

return

}

// Make sure the screen is turned on. This is probably the right

// thing to do, and more importantly it works around an issue in the

// activity manager where we will not launch activities consistently

// when the screen is off (since it is trying to keep them paused

// and has... issues).

//

// Also, this ensures the device stays awake while doing the following

// broadcasttechnically we should be holding a wake lock here

// as well.

PhoneApp.getInstance().wakeUpScreen()

/* If number is null, we're probably trying to call a non-existent voicemail number or

* something else fishy. Whatever the problem, there's no number, so there's no point

* in allowing apps to modify the number. */

if (number == null || TextUtils.isEmpty(number)) callNow = true

if (callNow) {

intent.setClass(this, InCallScreen.class)

startActivity(intent)

}

Intent broadcastIntent = new Intent(Intent.ACTION_NEW_OUTGOING_CALL)

if (number != null) broadcastIntent.putExtra(Intent.EXTRA_PHONE_NUMBER, number)

broadcastIntent.putExtra(EXTRA_ALREADY_CALLED, callNow)

broadcastIntent.putExtra(EXTRA_ORIGINAL_URI, intent.getData().toString())

broadcastIntent.putExtra(EXTRA_INTENT_FROM_BT_HANDSFREE,

intent.getBooleanExtra(OutgoingCallBroadcaster.EXTRA_INTENT_FROM_BT_HANDSFREE, false))

if (LOGV) Log.v(TAG, "Broadcasting intent " + broadcastIntent + ".")

sendOrderedBroadcast(broadcastIntent, PERMISSION, null, null,

Activity.RESULT_OK, number, null)

finish()

}

}

无权限版(d出拨号界面并自动输入电话号码,用户选择是否拨号):

import android.content.Context

import android.content.Intent

import android.net.Uri

public void Call(String Num,Context c){

if(Num !=null && Num.length() >0){

Intent itt=new Intent(Intent.ACTION_DIAL,Uri.parse("tel:"+Num))

itt.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)

c.startActivity(itt)

}

}

权限版(d出拨号界面,自动输入电话号码并立刻拨号,在部分系统中会触发安全警告):

<!--- 权限 --->

    <uses-permission android:name="android.permission.CALL_PHONE" /> import android.content.Context

import android.content.Intent

import android.net.Uri

    public void Call(String Num,Context c){

        if(Num !=null && Num.length() >0){

            Intent itt=new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+Num))

            itt.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)

            c.startActivity(itt)

        }

    }

创建一个Intent。

要实现拨号程序可能很简单,一个简单的Intent就能实现,从发出意图到真正实现拨号的代码中间还执行了很多其他方法,这都是android底层源码自动实现的,其中就包含了启动拨号的Activity。要想实现不d出拨号界面就实现拨号,就必须绕过中间这些方法,直接调用framework层中的拨号方法。详情可以去看下别人写的android拨号流程,就可知道真正实现拨号的方法。

一开始是想通过反射调用底层方法,但是当初试过,好像不能实现,所以最后直接调用底层方法,由于android本身并未提供相关的类或方法,所以必须引用额外的jar包(将android源码编译打包成jar)。这里用的是某位大神将android2.2的部分源码编译成的jar包。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/11098867.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-13
下一篇 2023-05-13

发表评论

登录后才能评论

评论列表(0条)

保存