呼叫意图的Andr​​oid [英] Call intent in Android

查看:195
本文介绍了呼叫意图的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能让通话由pressing按钮?我得到我的电话号码从的EditText 的字符串。下面是我的示例code:

How can I make call by pressing button? I get my number as a string from EditText. Here is my sample code:

String phone = editPhone.getText().toString();
btnPhone.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                call();
            }
        });
public void call() {
    try {
        Intent callIntent = new Intent(Intent.ACTION_CALL);
        callIntent.setData(Uri.parse(phone));
        startActivity(callIntent);
    } catch (ActivityNotFoundException activityException) {
         Log.e("myphone dialer", "Call failed", e);
    }
}

我添加了所有权限以清单文件。

但我正在逐渐 NullPointerException异常

推荐答案

这个简单的方法应该为你工作。

This simple approach should work for you.

例。

public class CallActivity extends Activity{
   String phone = "";

   onCreate()
   {
        btnPhone.setOnClickListener(new View.OnClickListener() { 
            @Override 
            public void onClick(View arg0) { 
                phone = editPhone.getText().toString(); 
                call(); 
            } 
        });    
   }

   public void call() {   
            Intent callIntent = new Intent(Intent.ACTION_CALL);          
            callIntent.setData(Uri.parse("tel:"+phone));          
            startActivity(callIntent);  
   }
}

您可能会使用字符串变量电话超出范围。

You might be using String variable phone out of scope.

这篇关于呼叫意图的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆