Android实现拨号功能
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android实现拨号功能相关的知识,希望对你有一定的参考价值。
public class TinyDialer extends Activity
/** Called when the activity is first created. */
private Button button;
private EditText phonenumber;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button)findViewById(R.id.button_id);
phonenumber = (EditText)findViewById(R.id.phonenumber_id);
button.setOnClickListener(new OnClickListener()
public void onClick(View v)
String call = phonenumber.getText().toString();
if(PhoneNumberUtils.isGlobalPhoneNumber(call))
Intent i = new Intent();
i.setAction(Intent.ACTION_DIAL);
i.setData(Uri.parse(call));
startActivity(i);
else
Toast.makeText(TinyDialer.this, R.string.correct_number, Toast.LENGTH_LONG).show();
);
这个程序是实现拨号的,程序本身没有错误,但是不能进行拨号功能,不知道还有什么地方有错误,帮忙帮我找出来下,谢谢~~~
已将i.setAction(Intent.ACTION_DIAL);
改为 i.setAction(Intent.ACTION_CALL);
还是不能实现这个功能
在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 action; forcing 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
// broadcast; technically 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();
参考技术A private OnClickListener myListener = new Button.OnClickListener()
@Override
public void onClick(View v)
//调用系统的拨号服务实现电话拨打功能
String phone_number = numberEt.getText().toString();
phone_number = phone_number.trim();//删除字符串首部和尾部的空格
if(phone_number != null && !phone_number.equals(""))
//调用系统的拨号服务实现电话拨打功能
//封装一个拨打电话的intent,并且将电话号码包装成一个Uri对象传入
Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+phone_number));
MyPhoneActivity.this.startActivity(intent);//内部类
;
最后在在AndroidManifest.xml里面添加
<uses-permission android:name="android.permission.CALL_PHONE"/> 参考技术B intent.setAction(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:" +call));
因为data的格式前缀必须有" tel:"
=========================================================================
在AndroidManifest.xml中不需要加权限,
当intent.setAction(Intent.ACTION_CALL); 时,
才需要CALL_PHONE权限 参考技术C i.setAction(Intent.ACTION_DIAL);
改为 i.setAction(Intent.ACTION_CALL); 参考技术D 在setData方法中的参数改为:Uri.parse("tel:" + call) ,并加入CALL_PHONE权限
Android 拨号器的简单实现
功能实现:一个EditView 一个拨打按钮,输入号码跳转到拨号界面
界面布局:activity_call.xml
//线性垂直布局:一个EditView文本、一个Button按钮
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context=".PhoneActivity" > 11 12 <TextView 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:text="请输入电话" /> 16 17 <EditText 18 android:id="@+id/editText1" 19 android:layout_width="fill_parent" 20 android:layout_height="wrap_content" 21 android:inputType="phone" /> 22 23 <Button 24 android:id="@+id/callsumbit" 25 android:layout_width="wrap_content" 26 android:layout_height="wrap_content" 27 android:layout_gravity="center" 28 android:text="拨打电话" /> 29 30 </LinearLayout>
CallActivity的Create方法
1 protected void onCreate(Bundle savedInstanceState) { 2 super.onCreate(savedInstanceState); 3 setContentView(R.layout.activity_call); 4 Button btn = (Button) findViewById(R.id.callsumbit); 5 btn.setOnClickListener(new OnClickListener() { 6 7 @Override 8 public void onClick(View v) { 9 EditText etNumber = (EditText) findViewById(R.id.editText1); 10 11 String number = etNumber.getText().toString(); 12 Intent intent = new Intent(); 13 intent.setAction(Intent.ACTION_CALL); 14 intent.setData(Uri.parse("tel:" + number)); 15 startActivity(intent); 16 } 17 }); 18 }
增加拨打电话的权限:AndroidManifest.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.ccec.callphone" 4 android:versionCode="1" 5 android:versionName="1.0" > 6 7 <uses-sdk 8 android:minSdkVersion="8" 9 android:targetSdkVersion="18" /> 10 <uses-permission android:name="android.permission.CALL_PHONE"/> 11 12 <application 13 android:allowBackup="true" 14 android:icon="@drawable/ic_launcher" 15 android:label="@string/app_name" 16 android:theme="@style/AppTheme" > 17 <activity 18 android:name="com.ccec.callphone.CallActivity" 19 android:label="@string/app_name" > 20 <intent-filter> 21 <action android:name="android.intent.action.MAIN" /> 22 23 <category android:name="android.intent.category.LAUNCHER" /> 24 </intent-filter> 25 </activity> 26 </application> 27 28 </manifest>
至此可以实现拨号功能。
以上是关于Android实现拨号功能的主要内容,如果未能解决你的问题,请参考以下文章