java - How to make a phone number clickable to use in sms service in android? -
i have phone number in xml layout , want make clickable such automatically entered in sms service , can send sms number.
alinas.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginright="10dp" android:layout_margintop="5dp" android:layout_marginleft="10dp" android:background="#ffffff"> <imageview android:layout_width="160dp" android:layout_height="140dp" android:layout_marginleft="10dp" android:layout_margintop="10dp" android:background="@drawable/alina_sbakerycafe" android:id="@+id/imageview" /> <textview android:layout_width="2500dp" android:layout_height="50dp" android:text="alina's bakery cafe" android:textstyle="bold" android:textsize="25dp" android:gravity="end" android:textalignment="textend" android:layout_aligntop="@+id/imageview" android:layout_alignparentleft="true" android:layout_alignparentstart="true" android:layout_margintop="42dp" /> <textview android:layout_width="300dp" android:layout_height="50dp" android:text="address: new baneswor, kathmandu phone no.:\t 9841123456" android:textalignment="center" android:textsize="15dp" android:layout_centervertical="true" android:layout_centerhorizontal="true" /> <button android:layout_width="@android:dimen/thumbnail_width" android:layout_height="@android:dimen/app_icon_size" android:onclick="movetoactivityviewmenu" android:background="@drawable/button" android:text="view menu" android:textcolor="#ffffff" android:layout_marginbottom="83dp" android:id="@+id/button" android:layout_alignparentbottom="true" android:layout_centerhorizontal="true" /> </relativelayout>
after click in phone number should automatically entered here:
sendsmsactivity.java
package com.golo.acer.mrestro4; import android.app.activity; import android.content.intent; import android.os.bundle; import android.telephony.smsmanager; import android.view.view; import android.widget.button; import android.widget.edittext; import android.widget.toast; public class sendsmsactivity extends activity { button sendsmsbtn; edittext tophonenumber; edittext smsmessageet; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_send_sms); sendsmsbtn = (button) findviewbyid(r.id.btnsendsms); tophonenumber = (edittext) findviewbyid(r.id.edittextphoneno); smsmessageet = (edittext) findviewbyid(r.id.edittextsms); sendsmsbtn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { sendsms(); } }); } private void sendsms() { string tophone = tophonenumber.gettext().tostring(); string smsmessage = smsmessageet.gettext().tostring(); try { smsmanager smsmanager = smsmanager.getdefault(); smsmanager.sendtextmessage(tophone, null, smsmessage, null, null); toast.maketext(this, "sms sent", toast.length_long).show(); } catch (exception e) { e.printstacktrace(); } } public void gotoinbox(view v) { intent intent = new intent(sendsmsactivity.this, receivesmsactivity.class); startactivity(intent); } public void movetosmsactivity(view view) { intent intent = new intent(this, smsactivity.class); startactivity(intent); } }
you can use
@override public void onreceive(context context, intent intent) { final bundle bundle = intent.getextras(); try { if (bundle != null) { object[] pdusobj = (object[]) bundle.get("pdus"); (object apdusobj : pdusobj) { smsmessage currentmessage = smsmessage.createfrompdu((byte[]) apdusobj); string senderaddress = currentmessage.getdisplayoriginatingaddress(); string message = currentmessage.getdisplaymessagebody(); log.e(tag, "received sms: " + message + ", sender: " + senderaddress); string verificationcode = getverificationcode(message); log.e(tag, "otp received: " + verificationcode); setdata(verificationcode); intent hhtpintent = new intent(context, otpverificationactivity.class); hhtpintent.putextra("otp", verificationcode); context.startservice(hhtpintent); } } } catch (exception e) { log.e(tag, "exception: " + e.getmessage()); } } /** * getting otp sms message body * ':' separator of otp message * * @param message * @return */ private string getverificationcode(string message) { string code = null; code = message.substring(0, 6); return code; }}
Comments
Post a Comment