为什么打电话在android中不起作用?

时间:2015-02-12 16:13:06

标签: android android-intent android-activity sharedpreferences

在我的应用程序中,单击按钮后,它应该能够调用相应的号码(由用户提供)。但问题是当我拨号时,它说unfortunately the project has stopped.现在我在模拟器中运行应用程序,我知道没有网络它不应该工作。相反,它应该显示一条错误消息。但为什么它会停止申请?

活动1:

    public class Activity1 extends ActionBarActivity {


    public static final String PREFS_TITLE = "SaveFile";

    TextView phnNum;
    public static final String Mob = "CallButton";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activity1);

        //finting the properties by given ID
        final EditText title = (EditText) findViewById(R.id.title);
        final EditText name = (EditText) findViewById(R.id.name);
        final EditText text = (EditText) findViewById(R.id.text);

        phnNum = (TextView) findViewById(R.id.phone);
        final EditText price = (EditText) findViewById(R.id.price);


        //adding the button clickable functionality
        Button nextViewButton = (Button) findViewById(R.id.Next_View);
        nextViewButton.setOnClickListener(new View.OnClickListener() {


                                              public void onClick(View view) {


                                                  SharedPreferences saveValue = getSharedPreferences(PREFS_TITLE, 0);


                                                  SharedPreferences.Editor editor = saveValue.edit();

                                                  editor.putString("Title", title.getText().toString());
                                                  editor.putString("Name", name.getText().toString());
                                                  editor.putString("Text", title.getText().toString());
                                                  editor.putString("Phone", phnNum.getText().toString());
                                                  editor.putString("Price", price.getText().toString());

                                                  editor.commit();

                                                  Intent intent = new Intent((getApplicationContext()), Activity2.class);
                                                  startActivity(intent);

                                              }
                                          }


        );


    }

}

活动2:

public class Activity2 extends ActionBarActivity {

    public static String PREFS_TITLE = "SaveFile";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activity2);


        TextView title_Avtivity2=(TextView)findViewById(R.id.title);
        TextView name_Avtivity2=(TextView)findViewById(R.id.name);
        TextView text_Avtivity2=(TextView)findViewById(R.id.text);
        TextView phon_Avtivity2=(TextView)findViewById(R.id.phone);
        TextView price_Avtivity2=(TextView)findViewById(R.id.price);

        Button callingButton=(Button)findViewById(R.id.call);
        SharedPreferences savedValue_Activity2=getSharedPreferences(PREFS_TITLE,0);
        title_Avtivity2.setText(savedValue_Activity2.getString("Title","No input"));
        name_Avtivity2.setText(savedValue_Activity2.getString("Name","No input"));
        text_Avtivity2.setText(savedValue_Activity2.getString("Text","No input"));
        phon_Avtivity2.setText(savedValue_Activity2.getString("Phone","No input"));
       price_Avtivity2.setText(savedValue_Activity2.getString("Price","No input"));
    }

public void call(View view){
    SharedPreferences getphonNum=getSharedPreferences(PREFS_TITLE,0);
    String warningMsg=getphonNum.getString("Phone","No phone number input");
    System.out.println(warningMsg);
    Intent intentForPhon=new Intent(Intent.ACTION_CALL,Uri.parse("Phon Num: "+warningMsg));
startActivity(intentForPhon);
}


}

1 个答案:

答案 0 :(得分:1)

确保解析右Uri

public void call(View view){
    SharedPreferences getphonNum=getSharedPreferences(PREFS_TITLE,0);
    String phoneNumber = getphonNum.getString("Phone","No phone number input");
    PhoneNumberUtils phoneNumberUtils = new PhoneNumberUtils();
    if(phoneNumberUtils.isGlobalPhoneNumber(phoneNumber)){
        Intent intentForPhon=new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+phoneNumber));
        startActivity(intentForPhon);
    } else {
        // do something else (throw exception or something)
    }
}

并确保您在清单

中拥有此权限
 <uses-permission android:name="android.permission.CALL_PHONE" />

并确保在按钮上设置OnClickListener

Button callingButton = (Button) findViewById(R.id.call);
callingButton.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        call();
    }
});