【问题标题】:Android send sms安卓发送短信
【发布时间】:2015-01-28 19:04:10
【问题描述】:

在下面的代码中,我试图有一个文本框和一个按钮,用户可以在其中输入文本,按下按钮时我只会将 SMS 发送到硬编码值,但我没有得到任何异常,我已经包含权限。但是当我发送短信时,应用程序关闭错误日志并没有任何内容,我正在真实设备上尝试这个,而不是模拟器。有没有人知道这里可能是什么问题

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

            try {
                EditText et =(EditText) findViewById(R.id.editText1);
        final String smstext =et.getText().toString().trim();
                sms.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    if (smstext != "" || smstext != null)
    {
                SmsManager smsmgr=SmsManager.getDefault();
        smsmgr.sendTextMessage("+xxxxxxxx", null, smstext , null , null);

    }
    else
    {
    Toast.makeText(getApplicationContext(),"Enter Message", Toast.LENGTH_SHORT).show();
    }
}
});

    }
            catch (Exception e) 
            {
                Toast.makeText(getApplicationContext(),"Send SMS error: "+ e, Toast.LENGTH_SHORT).show();
            }

main.xml

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:id="@+id/linearLayout2" android:layout_width="fill_parent" android:layout_height="fill_parent">
      <EditText android:id="@+id/editText1" android:layout_width="fill_parent" android:layout_height="wrap_content">
         <requestFocus></requestFocus>
      </EditText>
      <Button android:text="Send SMS" android:id="@+id/sms" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
      <Button android:text="Back" android:id="@+id/back1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>


   </LinearLayout>

【问题讨论】:

标签: android android-layout


【解决方案1】:

为了得到sendTextMessage()结果的一些反馈,你应该在函数的第5个参数上传递一个PendingIntent对象。

快速在线搜索带来了本教程:SMS Messaging in Android

编辑:我重新检查了您粘贴的代码,您还应该将etsmstext 的定义移到onClick 方法中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多