android學習筆記3——–短信發送器

效果圖:

import java.util.List; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.telephony.SmsManager; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 
 
public class SendMessageActivity extends Activity 

    private EditText mobileEditText; 
    private EditText contenText; 
    private Button sendbButton; 
    private Button exitButton; 
     
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        mobileEditText=(EditText)findViewById(R.id.mobile); 
        contenText=(EditText)findViewById(R.id.content); 
        sendbButton=(Button)findViewById(R.id.send); 
        exitButton=(Button)findViewById(R.id.exit); 
         
        sendbButton.setOnClickListener(listener); 
        exitButton.setOnClickListener(listener); 
    } 
     
    private OnClickListener listener=new OnClickListener() 
    { 
         
        @Override 
        public void onClick(View v) 
        { 
            Button button=(Button)v; 
            switch (button.getId()) 
            { 
                case R.id.send: 
                    String mobile=mobileEditText.getText().toString(); 
                    String contentString=contenText.getText().toString(); 
                    SmsManager smsManager=SmsManager.getDefault(); 
                     
                    //Intent intent=new Intent();                 
                    //intent.setAction(Intent.ACTION_SENDTO);  
                    //intent.setData(Uri.parse("smsto:"+mobile));  
                    if(contentString.length()>70) 
                    { 
                        List<String> contents=smsManager.pideMessage(contentString); 
                        for(String sms:contents) 
                        { 
                            smsManager.sendTextMessage(mobile, null, sms, null, null); 
                            //intent.putExtra("sms_body", sms);  
                            //SendMessageActivity.this.startActivity(intent);  
                        } 
                    } 
                    else  
                    { 
                        smsManager.sendTextMessage(mobile, null, contentString, null, null); 
                        //intent.putExtra("sms_body", contentString);  
                        //SendMessageActivity.this.startActivity(intent);  
                    } 
                    Toast.makeText(SendMessageActivity.this, "短信已發送", Toast.LENGTH_SHORT).show(); 
                    break; 
                     
                case R.id.exit: 
                    android.os.Process.killProcess(android.os.Process.myPid()); 
                    break; 
     
                default: 
                    break; 
            } 
             
        } 
    }; 

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SendMessageActivity extends Activity
{
 private EditText mobileEditText;
 private EditText contenText;
 private Button sendbButton;
 private Button exitButton;
 
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  mobileEditText=(EditText)findViewById(R.id.mobile);
  contenText=(EditText)findViewById(R.id.content);
  sendbButton=(Button)findViewById(R.id.send);
  exitButton=(Button)findViewById(R.id.exit);
  
  sendbButton.setOnClickListener(listener);
  exitButton.setOnClickListener(listener);
 }
 
 private OnClickListener listener=new OnClickListener()
 {
  
  @Override
  public void onClick(View v)
  {
   Button button=(Button)v;
   switch (button.getId())
   {
    case R.id.send:
     String mobile=mobileEditText.getText().toString();
     String contentString=contenText.getText().toString();
     SmsManager smsManager=SmsManager.getDefault();
     
     //Intent intent=new Intent();    
     //intent.setAction(Intent.ACTION_SENDTO);
     //intent.setData(Uri.parse("smsto:"+mobile));
     if(contentString.length()>70)
     {
      List<String> contents=smsManager.pideMessage(contentString);
      for(String sms:contents)
      {
       smsManager.sendTextMessage(mobile, null, sms, null, null);
       //intent.putExtra("sms_body", sms);
       //SendMessageActivity.this.startActivity(intent);
      }
     }
     else
     {
      smsManager.sendTextMessage(mobile, null, contentString, null, null);
      //intent.putExtra("sms_body", contentString);
      //SendMessageActivity.this.startActivity(intent);
     }
     Toast.makeText(SendMessageActivity.this, "短信已發送", Toast.LENGTH_SHORT).show();
     break;
     
    case R.id.exit:
     android.os.Process.killProcess(android.os.Process.myPid());
     break;
 
    default:
     break;
   }
   
  }
 };
}

main.xml文件

 

[java] 

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     >
 <TextView  
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="請輸入手機號"
     />
     
 <EditText 
     android:layout_height="wrap_content" 
     android:id="@+id/mobile" 
     android:layout_width="match_parent" 
     ></EditText>
     
 <TextView    
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"
     android:text="請輸入短信內容" 
     ></TextView>
     
 <EditText 
     android:layout_height="wrap_content"    
     android:layout_width="match_parent" 
     android:id="@+id/content"
     android:lines="3" 
     ></EditText>
     
 <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"
     android:id="@+id/send" 
     android:text="發送短信" 
     ></Button>
     
 <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"
      android:id="@+id/exit"
      android:text="退出軟件" 
      ></Button>
 </LinearLayout>
 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     >
 <TextView
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:text="請輸入手機號"
     />
   
 <EditText
  android:layout_height="wrap_content"
  android:id="@+id/mobile"
  android:layout_width="match_parent"
  ></EditText>
 
 <TextView 
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="請輸入短信內容"
  ></TextView>
 
 <EditText
  android:layout_height="wrap_content" 
  android:layout_width="match_parent"
  android:id="@+id/content"
  android:lines="3"
  ></EditText>
 
 <Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@+id/send"
  android:text="發送短信"
  ></Button>
 
 <Button
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:id="@+id/exit"
   android:text="退出軟件www.aiwalls.com"
   ></Button>
 </LinearLayout>
 
 
 
 
加入權限:
  <uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
 
摘自 奔跑的蝸牛
 

發佈留言