Androidda SMS yuborish
Ba’zida mijozlar buyurtma qilgan android ilovalarda ilovadan turib biron-bir telefon raqamga SMS xabar yuborish kerak bo’lib qolishi mumkin. Ushbu maqola Qanday qilib Android operatsion tizimlarida ishlaydigan qurilmalardan Java dasturlash tilida SMS xabar yuborish haqida bo’ladi.
Birinchi navbatda biz yaratayotgan android ilova SMS xabar yuborishi uchun tizimdan ruxsat olish kerak bo’ladi. Buning uchun AndroidManifest.xml faylga quyidagi qarotni qo’shamiz:
Endi biz SMS yuboradigan funksiyani yozamiz.
Funksiya boshida
import android.telephony.SmsManager;
Kutubxonasini chaqirib olamiz. Bu kutubxona Android tizimlarida SMS yuborish uchun mo’ljallangan.
Endi kodni asosiy qismiga o’tamiz:
public boolean sendSMS(String phoneNumber, String smsTxt) { boolean result = false; try { SmsManager smsManager = SmsManager.getDefault(); smsManager.sendTextMessage(phoneNumber, null, smsTxt, null, null); Toast.makeText(getApplicationContext(), "SMS Sent!", Toast.LENGTH_LONG).show(); result = true; } catch (Exception e) { Toast.makeText(getApplicationContext(), "SMS faild, please try again later!", Toast.LENGTH_LONG).show(); // e.printStackTrace(); } return result; }
Ushbu funksiyani ishga tushirish quyidagicha:
boolean result = sendSMS("+9989********", "Hello, World!!!");
Tayyor!
Mobil Dasturlash
Androidda SMS yuborish