- Bisa menjawab dan menerima SMS secara otomatis ketika sms diterima
- Menggunakan fungsi if sederhana dalam menjawab pesan
- Tanpa database
Gambar 1. Hasil running source code |
Source code:
1. Manifest:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="oddsaydev.smsgatewaysederhana"> <uses-permission android:name="android.permission.RECEIVE_SMS" /> <uses-permission android:name="android.permission.SEND_SMS" /> <uses-permission android:name="android.permission.READ_SMS" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" 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> <receiver android:name=".ReceiveSms" > <intent-filter> <action android:name="android.provider.Telephony.SMS_RECEIVED" /> </intent-filter> </receiver> </application> </manifest>
2. ReceiveSms.java
/** * Created by Windows on 06/03/2017. */ import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.media.AudioManager; import android.media.ToneGenerator; import android.os.Bundle; import android.telephony.SmsManager; import android.telephony.SmsMessage; public class ReceiveSms extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { Bundle myBundle = intent.getExtras(); SmsMessage [] messages = null; String strMessage = ""; String nohp=""; String pesan=""; if (myBundle != null) { Object [] pdus = (Object[]) myBundle.get("pdus"); messages = new SmsMessage[pdus.length]; for (int i = 0; i < messages.length; i++) { messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]); nohp =messages[i].getOriginatingAddress(); pesan=messages[i].getMessageBody(); } // Toast.makeText(context, strMessage, Toast.LENGTH_SHORT).show(); String balasan=""; if(pesan.equals("reg")){ balasan="Selamat datang, pilih 1 untuk bahasa Inggris, pilih 2 untuk bahasa Indonesia"; }else if(pesan.equals("1")){ balasan="Anda memilih 1 untuk bahasa Inggris"; }else if(pesan.equals("2")){ balasan="Anda memilih 1 untuk bahasa Indonesia"; } SmsManager smsManager = SmsManager.getDefault(); smsManager.sendTextMessage(nohp, null, balasan, null, null); } ToneGenerator toneG = new ToneGenerator(AudioManager.STREAM_ALARM, 100); toneG.startTone(ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD, 200); } }
Selamat bereksperimen
sekian dan terimakasih..
This comment has been removed by the author.
ReplyDeletethanks
DeleteAda apk nya gan soalnya laptopku mati
ReplyDeletengga ada gan
Deleteitu layout buat kita sms tanya jawabnya dimana? Sedangkan disitu ada 1 kelas lagi MainActivity dan tidak ada keterangan buat layoutnya
ReplyDelete