2011-08-06 212 views
0

在下面的代码我试图有一个文本框和一个按钮,用户将输入文本和按钮上按我只是发送短信到硬编码值,但我没有得到任何异常和但是当我发送短信的应用程序关闭错误日志doest有任何东西,我试图在真实的设备上不是一个模拟器。有任何机构知道这里可能是什么问题Android发送短信

 <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> 
+0

我在做类似的事情! http://stackoverflow.com/questions/14452808/sending-and-receiving-mms-in-android – toobsco42

回答

1

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

快速网络搜索带来了这个教程:SMS Messaging in Android

编辑:我重新检查您粘贴代码,你也应该移动onClick方法中的etsmstext的定义。