java - Sending text message using android application -


i trying build sms application following this tutorial , seems fine except when hit send button,the message not received other user number have entered in edittext. in application have 2 edittext , 1 button.one edittext message , other specifying phone number of receiver. code given below :

mainactivity.java

public class mainactivity extends appcompatactivity {      private static final int my_permissions_request_send_sms =0 ;     button send;     edittext message;     edittext phoneno;     string number;     string txt;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          send=(button)findviewbyid(r.id.sendbutton);         message=(edittext)findviewbyid(r.id.textmessage);         phoneno=(edittext)findviewbyid(r.id.phone);          send.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view view) {                 sendsmsmessage();             }         });     }      public void sendsmsmessage(){         number=phoneno.gettext().tostring();         txt=message.gettext().tostring();          if (contextcompat.checkselfpermission(this,                 manifest.permission.send_sms)                 != packagemanager.permission_granted) {             if (activitycompat.shouldshowrequestpermissionrationale(this,                     manifest.permission.send_sms)) {             } else {                 activitycompat.requestpermissions(this,                         new string[]{manifest.permission.send_sms},                         my_permissions_request_send_sms);             }         }      }      @override     public void onrequestpermissionsresult(int requestcode, @nonnull string[] permissions, @nonnull int[] grantresults) {         //super.onrequestpermissionsresult(requestcode, permissions, grantresults);         switch (requestcode) {             case my_permissions_request_send_sms: {                 if (grantresults.length > 0                         && grantresults[0] == packagemanager.permission_granted) {                     smsmanager smsmanager = smsmanager.getdefault();                     smsmanager.sendtextmessage(number, null, txt, null, null);                     toast.maketext(getapplicationcontext(), "sms sent.",                             toast.length_long).show();                 } else {                     toast.maketext(getapplicationcontext(),                             "sms faild, please try again.", toast.length_long).show();                     return;                 }             }         }      } } 

activity_main.xml

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.shaloin.sample852.mainactivity">  <edittext     android:id="@+id/textmessage"     android:textsize="20sp"     android:layout_margintop="20dp"     android:hint="text message"     android:layout_width="match_parent"     android:layout_height="wrap_content" /> <edittext     android:id="@+id/phone"     android:layout_below="@+id/textmessage"     android:textsize="20sp"      android:layout_margintop="20dp"     android:hint="phone number"     android:layout_width="match_parent"     android:layout_height="wrap_content" /> <button     android:id="@+id/sendbutton"     android:layout_below="@+id/phone"     android:layout_centerhorizontal="true"     android:text="send"     android:layout_width="wrap_content"     android:layout_height="wrap_content" /> 

androidmanifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.shaloin.sample852">  <uses-permission android:name="android.permission.send_sms"/> <uses-permission android:name="android.permission.receive_sms"/>  <application     android:allowbackup="true"     android:icon="@mipmap/ic_launcher"     android:label="@string/app_name"     android:roundicon="@mipmap/ic_launcher_round"     android:supportsrtl="true"     android:theme="@style/apptheme">     <activity android:name=".mainactivity">         <intent-filter>             <action android:name="android.intent.action.main" />              <category android:name="android.intent.category.launcher" />         </intent-filter>     </activity> </application> 

i have tried sending message using in-built sms application , works application made doesn't.can ? thank :)

actually, didn't write code send message in sendsmsmessage function, may first time message might send message.

public void sendsmsmessage() {     number = phoneno.gettext().tostring();     txt = message.gettext().tostring();      if (contextcompat.checkselfpermission(this,             manifest.permission.send_sms)             != packagemanager.permission_granted) {         if (activitycompat.shouldshowrequestpermissionrationale(this,                 manifest.permission.send_sms)) {         } else {             activitycompat.requestpermissions(this,                     new string[]{manifest.permission.send_sms},                     my_permissions_request_send_sms);         }         return;     }      sendmessage();  }  public void sendmessage() {    smsmanager smsmanager = smsmanager.getdefault();     smsmanager.sendtextmessage(number, null, txt, null, null);     toast.maketext(getapplicationcontext(), "sms sent.",             toast.length_long).show(); } 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -