MianActivity.java
package com.example.sms;
import java.util.ArrayList;
import java.util.List;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.telephony.SmsManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
private String s=null;
private EditText phonenum;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tt=(TextView) findViewById(R.id.textView1);
phonenum=(EditText)findViewById(R.id.editText1);
Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show();
try{
tt.setText(s);
}
catch(Exception e){
e.printStackTrace();
Toast.makeText(MainActivity.this, "null", Toast.LENGTH_LONG).show();
}
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
public void smsbutton(View view){
String phone_number = phonenum.getText().toString();
String sms_content = "short sms is coming".trim();
if(phone_number.equals("")) {
Toast.makeText(MainActivity.this,"phone number is empty", Toast.LENGTH_LONG).show();
} else {
SmsManager smsManager = SmsManager.getDefault();
if(sms_content.length() > 70) {
List<String> contents = smsManager.divideMessage(sms_content);
for(String sms : contents) {
smsManager.sendTextMessage(phone_number, null, sms, null, null);
}
} else {
smsManager.sendTextMessage(phone_number, null, sms_content, null, null);
}
Toast.makeText(MainActivity.this, "phone_number", Toast.LENGTH_SHORT).show();
}
}
public void longsmsbutton(View view){
String phone_number = phonenum.getText().toString();
String sms_content = "long sms is coming.....................................................................".trim();
if(phone_number.equals("")) {
Toast.makeText(MainActivity.this,"phone number is empty", Toast.LENGTH_LONG).show();
} else {
SmsManager smsManager = SmsManager.getDefault();
ArrayList<String> contents = smsManager.divideMessage(sms_content);
smsManager.sendMultipartTextMessage(phone_number, null, contents, null, null);
Toast.makeText(MainActivity.this, "phone_number", Toast.LENGTH_SHORT).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.main, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
fragment_main.xml
<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.example.sms.MainActivity$PlaceholderFragment" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="@string/hello_world" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_alignParentRight="true"
android:onClick="longsmsbutton"
android:text="发送长信息" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="48dp"
android:onClick="smsbutton"
android:text="发送短信" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button2"
android:layout_centerHorizontal="true"
android:ems="10"
android:inputType="phone" >
<requestFocus />
</EditText>
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sms"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<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="com.example.sms.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>
</application>
</manifest>