Android提供了一種方便的方法來發送短信的幫助下SmsManager其內置的API。使用API,您需要定義使用下麵的代碼片段:
SmsManager SmsManager = SmsManager.getDefault ();smsManager。sendTextMessage (number, null,“這是你把這裏的sms消息。”零,零);
讓我們看看如何使用Android SmsManager對象發送一個短信。要實現這一點,您需要有一個SIM卡插入你的android智能手機或大量裝備在你的android模擬器。
你需要做的第一件事是創建一個Android應用程序的幫助Android Studio IDE的名字greatlearning將com.firstapp.greatlearning提供包的名稱。
在那之後,你需要修改MainActivity。java文件在您的項目源文件夾。請參閱下麵的代碼片段:
包com.firstapp.greatlearning;進口android.net.Uri;進口android.os.Bundle;進口android.app.Activity;進口android.content.Intent;進口android.util.Log;進口android.view.Menu;進口android.view.View;進口android.widget.Button;進口android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button onButton = (Button) findViewById(R.id.button); onButton.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { sendSMS(); } }); } protected void sendSMS() { Log.i("Send SMS in Android", ""); Intent sendSms = new Intent(Intent.ACTION_VIEW); sendSms.setData(Uri.parse("smsto:")); sendSms.setType("vnd.android-dir/mms-sms"); sendSms.putExtra("address", new String ("Address-Here")); sendSms.putExtra("sms_body", "Type your message here "); try { startActivity(sendSms); finish(); Log.i("SMS sent successfully!!", ""); } catch (android.content.ActivityNotFoundException ex) { Toast.makeText(MainActivity.this, "SMS is not sent, please try later.", Toast.LENGTH_SHORT).show(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } }
接下來,您將需要修改布局activity_main XML文件的名稱。xml,您還可以添加按鈕、textfields字段和其他組件。看到如下所示的代碼,供您參考:
< ?xml version = " 1.0 " encoding = " utf - 8 " ?> < RelativeLayout xmlns: android = " http://schemas.android.com/apk/res/android " xmlns:工具= " 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”工具:上下文= "。MainActivity " > < TextView android: layout_width = " wrap_content“android: layout_height =“wrap_content”android:文本=“拖拽”android: id =“@ + id / TextView”android: layout_alignParentTop = " true " android: layout_centerHorizontal = " true " android: textSize = " 25 dp " / > < TextView android: layout_width =“wrap_content”android: layout_height =“wrap_content”andrbeplay2018官网oid:文本=“大學習”android: id =“@ + id / textView2”android: layout_below =“@ + id / TextView”android: layout_centerHorizontal = " true " android: textSize = 25 dp“android:輸入textColor = " # ffffff " / > < ImageView android: layout_width =“wrap_content”android: layout_height =“wrap_content”android: id =“@ + id / ImageView”android: layout_marginTop =“52 dp”android: layout_below =“@ + id / textView2”android: layout_centerHorizontal = " true " / > <按鈕android: layout_width =“wrap_content”android: layout_height =“wrap_content”android:文本=“寫短信”android: id =“@ + id /按鈕”android: layout_below =“@ + id / ImageView”android: layout_alignRight =“@ + id / textView2”android: layout_alignEnd =“@ + id / textView2”android: layout_marginTop = " 60 dp " android: layout_alignLeft =“@ + id / ImageView”android: layout_alignStart =“@ + id / ImageView”/ > < / RelativeLayout >
我們還需要修改字符串。xml文件資源文件夾下以下常量:
< ?xml version = " 1.0 " encoding = " utf - 8 " ?> <資源> <字符串名稱= " app_name " > GreatLearningSMSExample < /字符串> < /資源>
後您需要修改AndroidManifest。xml文件並運行在Android模擬器中的應用。下麵的XML代碼顯示了AndroidManifest默認內容。xml文件:
< ?xml version = " 1.0 " encoding = " utf - 8 " ?> <清單xmlns: android = " http://schemas.android.com/apk/res/android "包= " com.firstapp。greatlearning " > <應用android: allowBackup = " true " android:圖標=“@drawable / ic_launcher”android: label = " @string / app_name " android:主題= " @style / AppTheme " > <活動android: name = " com.firstapp.greatlearning。MainActivity @string / app_name“android: label = > <意圖過濾器> <行動android: name = " android.intent.action.MAIN " / > <類別android: name = " android.intent.category.LAUNCHER " / > < /意圖過濾器> < /活動> < /應用程序> < /清單>
這樣你可以做所有這些配置後運行該應用程序,這將要求你寫一個消息發送按鈕。您還可以修改輸出的GUI如果你不滿意,但是首先,你應該檢查應用程序的工作。