In this one video I’ll provide an Android Text Messaging example program. I’ll cover how to send and receive text messages. I’ll also talk about Android Handlers and threads.
I did my best to make everything very simple so you’ll be able to use the code in your programs. All of the code, like always, follows the video below. Do anything you’d like with it.
If you find videos like this useful, it helps if you tell Google Plus with a click here [googleplusone]
Code From the Video
AndroidManifest.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.newthinktank.derekbanas.texting" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="15" android:targetSdkVersion="21" /> <uses-permission android:name="android.permission.SEND_SMS" /> <uses-permission android:name="android.permission.RECEIVE_SMS" /> <uses-permission android:name="android.permission.READ_SMS" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.newthinktank.derekbanas.texting.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> <action android:name="android.intent.action.SEND" /> <action android:name="android.intent.action.SENDTO" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="sms" /> <data android:scheme="smsto" /> <data android:scheme="mms" /> <data android:scheme="mmsto" /> </intent-filter> </activity> <receiver android:name="com.newthinktank.derekbanas.texting.MainActivity$SmsReceiver" android:permission="android.permission.BROADCAST_SMS" > <intent-filter> <action android:name="android.provider.Telephony.SMS_DELIVER" /> </intent-filter> </receiver> <receiver android:name="com.newthinktank.derekbanas.texting.MainActivity$MMSReceiver" android:permission="android.permission.BROADCAST_WAP_PUSH" > <intent-filter> <action android:name="android.provider.Telephony.WAP_PUSH_DELIVER" /> <data android:mimeType="application/vnd.wap.mms-message" /> </intent-filter> </receiver> <service android:name="com.newthinktank.derekbanas.texting.MainActivity$HeadlessSmsSendService" android:exported="true" android:permission="android.permission.SEND_RESPOND_VIA_MESSAGE" > <intent-filter> <action android:name="android.intent.action.RESPOND_VIA_MESSAGE" /> <category android:name="android.intent.category.DEFAULT" /> <data android:scheme="sms" /> <data android:scheme="smsto" /> <data android:scheme="mms" /> <data android:scheme="mmsto" /> </intent-filter> </service> </application> </manifest> |
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
<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="wrap_content" android:layout_height="wrap_content" android:inputType="phone" android:ems="10" android:id="@+id/pNumEditText" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginBottom="164dp" android:hint="Phone Number" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Send" android:id="@+id/sendButton" android:layout_alignBottom="@+id/pNumEditText" android:layout_toRightOf="@+id/pNumEditText" android:layout_toEndOf="@+id/pNumEditText" android:layout_marginLeft="10dp" android:onClick="sendMessage"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textMultiLine" android:ems="10" android:id="@+id/txtMsgEditText" android:layout_below="@+id/sendButton" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:lines="5" android:hint="Message" android:longClickable="false" android:layout_marginTop="10dp"> <requestFocus /> </EditText> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textMultiLine" android:ems="10" android:id="@+id/messagesEditText" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_above="@+id/sendButton" /> </RelativeLayout> |
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
package com.newthinktank.derekbanas.texting; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.support.v7.app.ActionBarActivity; import android.telephony.SmsManager; import android.telephony.SmsMessage; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends ActionBarActivity { EditText txtMsgEditText, pNumEditText, messagesEditText; Button sendButton; static String messages = ""; // Allows use to update the UI with new messages by telling the Activity // to update the UI every 5 seconds // A handler can schedule for code to execute at a set time in this Activities // thread Handler mHandler = new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); txtMsgEditText = (EditText) findViewById(R.id.txtMsgEditText); pNumEditText = (EditText) findViewById(R.id.pNumEditText); messagesEditText = (EditText) findViewById(R.id.messagesEditText); sendButton = (Button) findViewById(R.id.sendButton); // Thread updates the messages EditText every 10 seconds new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub while (true) { try { // Wait 5 seconds and then execute the code in run() Thread.sleep(5000); mHandler.post(new Runnable() { @Override public void run() { // Update the messagesEditText messagesEditText.setText(messages); } }); } catch (Exception ex) { ex.printStackTrace(); } } } }).start(); } public void sendMessage(View view) { // Get the phone number and message to send String phoneNum = pNumEditText.getText().toString(); String message = txtMsgEditText.getText().toString(); try{ // Handles sending and receiving data and text SmsManager smsManager = SmsManager.getDefault(); // Sends the text message // 2nd is for the service center address or null // 4th if not null broadcasts with a successful send // 5th if not null broadcasts with a successful delivery smsManager.sendTextMessage(phoneNum, null, message, null, null); Toast.makeText(this, "Message Sent", Toast.LENGTH_SHORT).show(); } catch (IllegalArgumentException ex){ Log.e("TEXTING", "Destination Address or Data Empty"); Toast.makeText(this, "Enter a Phone Number and Message", Toast.LENGTH_LONG).show(); ex.printStackTrace(); } catch (Exception ex) { Toast.makeText(this, "Message Not Sent", Toast.LENGTH_LONG).show(); ex.printStackTrace(); } // Update the message EditText messages = messages + "You : " + message + "\n"; } // Receives texts public static class SmsReceiver extends BroadcastReceiver{ // Handles sending and receiving data and text final SmsManager smsManager = SmsManager.getDefault(); public SmsReceiver() { } @Override public void onReceive(Context context, Intent intent) { final Bundle bundle = intent.getExtras(); try{ // Check if we received data if (bundle != null){ // Store data sent as a PDU (Protocal Data Unit) which includes the // number and text final Object[] pdusObj = (Object[]) bundle.get("pdus"); // Cycle through the data received for (int i = 0; i < pdusObj.length; i++) { // Create a SmsMessage from the raw PDU data SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]); // Get sending phone number String phoneNumber = smsMessage.getDisplayOriginatingAddress(); // Get the message sent String message = smsMessage.getDisplayMessageBody(); // Update the messages EditText // messages = messages + phoneNumber + " : " + message + "\n"; // I use this to block the receiving number messages = messages + "Sender : " + message + "\n"; } // end for loop } // bundle is null } catch (Exception ex) { Log.e("SmsReceiver", "Exception smsReceiver" +ex); } } } // Handles receiving MMS public class MMSReceiver extends BroadcastReceiver { public MMSReceiver() { } @Override public void onReceive(Context context, Intent intent) { throw new UnsupportedOperationException("Not Implemented Yet"); } } // Handles when you want to send a pre-written message when a call is rejected public class HeadlessSmsSendService extends BroadcastReceiver { public HeadlessSmsSendService() { } @Override public void onReceive(Context context, Intent intent) { throw new UnsupportedOperationException("Not Implemented Yet"); } } @Override protected void onDestroy() { mHandler.removeCallbacksAndMessages(null); super.onDestroy(); } } |
Dude im pretty confused i android studio doesnt work in my pc can i use eclipse and copy what u do in android studio? What happens then? Everyone teaches and uses android studio and i use eclipse will it be the same?
Yes you can use Eclipse instead. Everything will work the same.