編輯:關於android開發
1、發送短信涉及到權限,我們需要把權限加上
2、當我們發送短信時,不管發送是否成功,接收方是否接收到,系統都會發送廣播
3、這時我們注冊廣播去接收一下就可以了
4、布局文件很簡單,裡面就兩個EditText和一個button
下面上代碼,裡面有注釋:
發送廣播接收器:
package com.example.fanlei.cutnotedemo; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.telephony.SmsManager; import android.widget.Toast; /** * 監聽短信是否已經發送成功 */ public class MySmsSendReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals("SMS_SEND")){ switch(getResultCode()) { case Activity.RESULT_OK: Toast.makeText(context,"發送成功",Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_GENERIC_FAILURE: Toast.makeText(context,"發送失敗",Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_NO_SERVICE: break; case SmsManager.RESULT_ERROR_NULL_PDU: break; case SmsManager.RESULT_ERROR_RADIO_OFF: break; } } } }
接收方的廣播
package com.example.fanlei.cutnotedemo; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; /** * 監聽對方是否發已經接收到短信 */ public class MySmsDeliverReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals("SMS_DELIVER")){ switch(getResultCode()) { case Activity.RESULT_OK: Toast.makeText(context,"對方已接收",Toast.LENGTH_SHORT).show(); break; case Activity.RESULT_CANCELED: break; } } } }
主函數
package com.example.fanlei.cutnotedemo; import android.app.PendingIntent; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.telephony.SmsManager; import android.view.View; import android.widget.EditText; import android.widget.Toast; import java.util.List; public class MainActivity extends ActionBarActivity { private EditText editText1,editText2; private MySmsSendReceiver mySmsSendReceiver; //發送短信的廣播 private MySmsDeliverReceiver mySmsDeliverReceiver;//對方是否已經接收的廣播 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mySmsSendReceiver = new MySmsSendReceiver(); mySmsDeliverReceiver = new MySmsDeliverReceiver(); editText1 = (EditText) findViewById(R.id.editText1);//手機號 editText2 = (EditText) findViewById(R.id.editText2);//內容 //初始化啟動意圖 Intent sendIntent = new Intent(); sendIntent.setAction("SMS_SEND"); Intent deliverIntent = new Intent(); deliverIntent.setAction("SMS_DELIVER"); //滿足條件後執行Intent final PendingIntent send = PendingIntent.getBroadcast(MainActivity.this,0 ,sendIntent,0); final PendingIntent deliver = PendingIntent.getBroadcast(MainActivity.this,0,deliverIntent,0); //發送短信按鈕 findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String phoneNumber = editText1.getText().toString(); String content = editText2.getText().toString(); if (phoneNumber.equals("")){ Toast.makeText(MainActivity.this,"手機號碼為空",Toast.LENGTH_SHORT).show(); }else { //獲得短信的管理者 SmsManager sm = SmsManager.getDefault(); //短信字數限制,如果>70,則需要拆分短信發送 if (content.length() > 70){ List<String> contents = sm.divideMessage(content); for (String sms : contents){ sm.sendTextMessage(phoneNumber,null,sms,send,deliver);//發送短信 } }else { sm.sendTextMessage(phoneNumber,null,content,send,deliver);//發送短信 } } } }); //注冊廣播 registerReceiver(mySmsSendReceiver,new IntentFilter("SMS_SEND")); registerReceiver(mySmsDeliverReceiver,new IntentFilter("SMS_DELIVER")); } }
布局文件
<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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/editText1" android:hint="手機號" android:inputType="phone"/> <EditText android:id="@+id/editText2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/editText1" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:hint="內容"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button" android:layout_centerVertical="true" android:layout_centerHorizontal="true" android:text="發送短信" /> </RelativeLayout>
小伙伴們可以用10086進行測試
關於eclipse android 在manifest改app應用包名注意事項,androidmanifest在我剛學android 時候,然後立即就做項目。那時連ecl
Android studio 插件安裝 plugin Android studio 插件安裝 plugin android studio 上有很多第三方
閱讀《Android 從入門到精通》(12)——自動完成文本框 自動完成文本框(AutoCompleteTextView) java.lang.Object; andro
Android 連接webservice(利用谷歌提供的jar包),androidwebserviceAndroid開發,需要連接webservice,之前就想用谷歌提供
Android種使用Notification實現通知管理以及自定義通知欄