本文主要是介绍Android实现短信息发送,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
对于短信发送器的实现,也是较简单的,总体来说只需要设置好布局文件和Activity类就可以了,但在此基础上,还需要进行对配置文件的设置,下面就让我们来进行演示:
在创建好项目之后,配置布局文件代码如下所示:
其中使用了组件AutoCompleteTextView组件,能够有提示的出现语句,在这儿我们用于提示电话号码,比如输入188时,再输入1时就会有提示188这个电话号码,在内容输入区,我们使用一个文本编辑框进行信息内容的传送,最后来一个发送按钮就可以完成布局了
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context="com.wusu.android_002_Message.MainActivity" ><AutoCompleteTextViewandroid:id="@+id/editText_receive"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_alignParentTop="true"android:layout_marginTop="5dp"android:inputType="number"android:hint="@string/Input_number_of_receive"android:ems="10" /><EditTextandroid:id="@+id/editText_content"android:layout_width="fill_parent"android:layout_height="match_parent"android:layout_above="@+id/button_send"android:layout_alignRight="@+id/editText_receive"android:layout_below="@id/editText_receive"android:ems="10"android:layout_marginTop="10dp"android:hint="@string/Input_content"android:gravity="top"android:inputType="textMultiLine" ><requestFocus /></EditText><Buttonandroid:id="@+id/button_send"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_alignRight="@+id/editText_content"android:layout_marginBottom="36dp"android:layout_marginRight="14dp"android:text="@string/send" /></RelativeLayout>
在写完布局之后,我们进行Activity类的编写,主要代码如下所示:
public class MainActivity extends Activity implements OnClickListener {private Button button_send;private TextView textView_content,textView_receive;private AutoCompleteTextView autoCompleteTextView_number;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button_send=(Button) findViewById(R.id.button_send);button_send.setOnClickListener(this);textView_content=(TextView) findViewById(R.id.editText_content);textView_receive=(TextView) findViewById(R.id.editText_receive);autoCompleteTextView_number=(AutoCompleteTextView) findViewById(R.id.editText_receive);ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,numbers);autoCompleteTextView_number.setAdapter(adapter);}
//显示要提示的电话号码private String[] numbers={"1880","150","1008611","10086","151","1234567890","9876543210","725610987678","226610"}; @Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}@Overridepublic 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();if (id == R.id.action_settings) {finish();return true;}return super.onOptionsItemSelected(item);}@Overridepublic void onClick(View view) {switch (view.getId()) {case R.id.button_send:String content=textView_content.getText().toString();String number=textView_receive.getText().toString();if(TextUtils.isEmpty(number)){Toast.makeText(MainActivity.this,"请输入电话号码...", Toast.LENGTH_SHORT).show();return;}else{SmsManager smsManager=SmsManager.getDefault();ArrayList<String> contents=smsManager.divideMessage(content);for(String str:contents){smsManager.sendTextMessage(number,null, str, null, null);}Toast.makeText(this, "已发送", Toast.LENGTH_SHORT).show();}break;default:break;}}
}
</pre><p></p><pre>
现在,在进行配置文件,只需在配置文件中添加即可
<uses-permission android:name="android.permission.SEND_SMS"/>
以上就是Android中短信息发送功能的实现
这篇关于Android实现短信息发送的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!