安卓案例(短信发送器)

农村四月闲人少,勤学苦攻把名扬。这篇文章主要讲述安卓案例:短信发送器相关的知识,希望能为你提供帮助。
效果:

安卓案例(短信发送器)

文章图片

 
几个简单的布局:
main
< LinearLayout 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:orientation="vertical" tools:context=".MainActivity" > < RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" > < EditText android:id="@+id/et_number" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入手机号码:" /> < Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@id/et_number" android:layout_alignParentRight="true" android:onClick="click" android:text="+" /> < /RelativeLayout> < EditText android:id="@+id/et_content" android:layout_width="match_parent" android:layout_height="200dp" android:gravity="top" android:hint="请输入短信内容:" /> < Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="insert" android:text="插入短信模板" /> < Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="send" android:text="发送" /> < /LinearLayout>

 
联系人布局contact:
< ?xml version="1.0" encoding="utf-8"?> < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > < ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent" > < /ListView> < /LinearLayout>

 
模板布局template:
< ?xml version="1.0" encoding="utf-8"?> < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > < ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent"> < /ListView> < /LinearLayout>

 
contact_item:
< ?xml version="1.0" encoding="utf-8"?> < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > < TextView android:id="@+id/tv_name" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:textSize="20sp" /> < TextView android:id="@+id/tv_phone" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:textSize="20sp" /> < /LinearLayout>

 
template_item:
< ?xml version="1.0" encoding="utf-8"?> < TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" android:textAppearance="?android:attr/textAppearanceLarge" android:textColor="#000000" />

 
 
MainActivity:
package org.dreamtech.smsmg; import java.util.ArrayList; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.telephony.SmsManager; import android.view.View; import android.widget.EditText; public class MainActivity extends Activity {private EditText et_content; private EditText et_number; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_content = (EditText) findViewById(R.id.et_content); et_number = (EditText) findViewById(R.id.et_number); }// 显示意图 public void click(View v) { Intent intent = new Intent(this, ContactActivity.class); startActivityForResult(intent, 1); }@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {if (resultCode == 10) { String phone = data.getStringExtra("phone"); et_number.setText(phone); } else if (resultCode == 20) { String smsContent = data.getStringExtra("smsContent"); et_content.setText(smsContent); }super.onActivityResult(requestCode, resultCode, data); }// 显示意图 public void insert(View v) { Intent intent = new Intent(this, SmsTemplateActivity.class); startActivityForResult(intent, 2); }// 隐式意图 public void send(View v) { String number = et_number.getText().toString().trim(); String content = et_content.getText().toString().trim(); SmsManager smsManager = SmsManager.getDefault(); ArrayList< String> divideMessages = smsManager.divideMessage(content); for (String div : divideMessages) { smsManager.sendTextMessage(number, null, div, null, null); } } }

 
ContactActivity:
package org.dreamtech.smsmg; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.TextView; public class ContactActivity extends Activity { private List< Person> lists; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_contact); ListView lv = (ListView) findViewById(R.id.lv); lists = new ArrayList< Person> (); for (int i = 0; i < 20; i++) { Person p = new Person(); p.setName("张" + i); p.setPhone("11" + i); lists.add(p); } lv.setAdapter(new MyAdapter()); lv.setOnItemClickListener(new OnItemClickListener() {@Override public void onItemClick(AdapterView< ?> parent, View view, int position, long id) { String phone = lists.get(position).getPhone(); Intent intent = new Intent(); intent.putExtra("phone", phone); setResult(10, intent); finish(); } }); }private class MyAdapter extends BaseAdapter {@Override public int getCount() { return lists.size(); }@Override public Object getItem(int position) { return null; }@Override public long getItemId(int position) { return 0; }@Override public View getView(int position, View convertView, ViewGroup parent) {View view; if (convertView == null) { view = View.inflate(getApplicationContext(), R.layout.contact_item, null); } else { view = convertView; } TextView tv_name = (TextView) view.findViewById(R.id.tv_name); TextView tv_phone = (TextView) view.findViewById(R.id.tv_phone); tv_name.setText(lists.get(position).getName()); tv_phone.setText(lists.get(position).getPhone()); return view; }} }

【安卓案例(短信发送器)】 
SmsTemplateActivity:
package org.dreamtech.smsmg; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.ListView; public class SmsTemplateActivity extends Activity {String objects[] = { "我在吃饭,请稍后联系", "我在开会,请稍后联系", "我在上课,请稍后联系", "我在打代码,请稍后联系", "我在约会,请稍后联系" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_template); ListView lv = (ListView) findViewById(R.id.lv); ArrayAdapter< String> adapter = new ArrayAdapter< String> ( getApplicationContext(), R.layout.smstemplate_item, objects); lv.setAdapter(adapter); lv.setOnItemClickListener(new OnItemClickListener() {@Override public void onItemClick(AdapterView< ?> parent, View view, int position, long id) { String smsContent = objects[position]; Intent intent = new Intent(); intent.putExtra("smsContent", smsContent); setResult(20, intent); finish(); } }); } }

 
 
Person类:
package org.dreamtech.smsmg; public class Person { private String name; private String phone; public String getName() { return name; }public void setName(String name) { this.name = name; }public String getPhone() { return phone; }public void setPhone(String phone) { this.phone = phone; }}

 
 
注册Activity和配置权限:
< ?xml version="1.0" encoding="utf-8"?> < manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.dreamtech.smsmg" android:versionCode="1" android:versionName="1.0" > < uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> < uses-permission android:name="android.permission.SEND_SMS"/> < application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > < activity android:name="org.dreamtech.smsmg.MainActivity" android:label="@string/app_name" > < intent-filter> < action android:name="android.intent.action.MAIN" /> < category android:name="android.intent.category.LAUNCHER" /> < /intent-filter> < /activity> < activity android:name="org.dreamtech.smsmg.ContactActivity" > < /activity> < activity android:name="org.dreamtech.smsmg.SmsTemplateActivity" > < /activity> < /application> < /manifest>

 

    推荐阅读