无法在Android Studio中发送短信

时间:2017-02-12 17:36:46

标签: android android-studio sms

Android新手,我似乎无法在我的模拟器上向自己发送任何文字。我已经允许清单文件中的权限并导入必要的类,但仍然无法弄清楚错误是什么。任何帮助将不胜感激。

$ which -a python python=3.6
 /Users/MacBook/miniconda3/bin/python
 /Users/MacBook/miniconda2/bin/python
 /Users/MacBook/anaconda/bin/python
 /Library/Frameworks/Python.framework/Versions/2.7/bin/python
 /usr/local/bin/python
 /usr/bin/python

清单

public class Detail extends AppCompatActivity {

public Button button5;
Spinner spinner2;
ArrayAdapter<CharSequence> adapter;
EditText editText4;

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

    editText4 = (EditText) this.findViewById(R.id.editText4);

    button5 = (Button) findViewById(R.id.button5);
    button5.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String contact = editText4.getText().toString();
            String message = "Help is on the way.";
            sendMessage(contact,message);
            Intent b = new Intent(Detail.this, End.class);
            startActivity(b);
        }
    });

    spinner2 = (Spinner) findViewById(R.id.spinner2);
    adapter = ArrayAdapter.createFromResource(this,R.array.selectissue,android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner2.setAdapter(adapter);
    spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            Toast.makeText(getBaseContext(),parent.getItemAtPosition(position) + " selected" , Toast.LENGTH_LONG).show();
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });
}

private void sendMessage(String contact, String message)
{
    SmsManager smsManager = SmsManager.getDefault();

    try {
        smsManager.sendTextMessage(contact, null , message , null , null);
        Toast.makeText(getApplicationContext(), "SMS Sent.", Toast.LENGTH_LONG).show();

    } catch (Exception e)
    {

        Toast.makeText(getApplicationContext(), "SMS failed to send.", Toast.LENGTH_LONG).show();

    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_detail, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}


 }

<?xml version="1.0" encoding="utf-8"?>

1 个答案:

答案 0 :(得分:0)

如果您想测试短信功能,然后使用激活的SIM卡在真实的Android设备上进行测试,则无法通过模拟器发送短信。