Membuat Aplikasi SMS di Android Studio

Oktober 12, 2017 0 Comments

Walaupun di android sudah disediakan aplikasi perpesanan bawaan, tetapi tidak ada salahnya kita membuat aplikasi SMS ini, setidaknya untuk mengetahui bagaimana cara kerja dari aplikasi SMS ini, ataupun jika anda ingin membuat aplikasi SMS yang custom, misalnya membuat aplikasi SMS dengan gaya iMessage. Pada kesempatan kali ini akan dibahas cara membuat aplikasi SMS sederhana.



Pertama-tama buat desain tampilannya dulu
<?xml version="1.0" encoding="utf-8"?>
<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"
   
android:id="@+id/activity_main"
   
android:padding="10dp"
   
tools:context="com.giviews.message.MainActivity">

    <
RelativeLayout
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content">
        <
EditText
           
android:layout_width="match_parent"
            
android:layout_height="wrap_content"
           
android:inputType="phone"
           
android:ems="15"
           
android:id="@+id/tvNumber"
           
android:hint="Enter contact number"
           
android:singleLine="true" />
    </
RelativeLayout>

    <
LinearLayout
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:orientation="vertical"
       
android:id="@+id/pushingUp"
       
android:layout_alignParentBottom="true"
       
android:layout_alignParentStart="true"
       
android:layout_alignParentLeft="true">
        <
LinearLayout
           
android:layout_width="match_parent"
           
android:layout_height="wrap_content"
           
android:orientation="vertical">
            <
ScrollView
               
android:layout_width="match_parent"
               
android:layout_height="match_parent"
               
android:layout_weight="1">
                <
LinearLayout
                   
android:layout_width="match_parent"
                    
android:layout_height="wrap_content"
                   
android:orientation="vertical">
                    <
TextView
                       
android:layout_width="match_parent"
                       
android:layout_height="match_parent"
                        
android:id="@+id/txtMessage"
                       
android:layout_weight="1"/>
                </
LinearLayout>
            </
ScrollView>
        </
LinearLayout>

        <
LinearLayout
           
android:layout_width="match_parent"
           
android:layout_height="wrap_content"
           
android:orientation="horizontal">
            <
EditText
               
android:layout_width="0dp"
               
android:layout_height="wrap_content"
               
android:ems="160"
                
android:layout_weight="5"
               
android:id="@+id/tvMessage"
               
android:hint="Enter Message"/>
            <
Button
               
android:layout_width="0dp"
               
android:layout_weight="1"
               
android:background="@android:drawable/ic_menu_send"
               
android:layout_height="wrap_content"
               
android:id="@+id/btnSend" />
        </
LinearLayout>
    </
LinearLayout>

</
RelativeLayout>

Kemudian untuk di javanya masukan kode ini
package com.giviews.message;

import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
   
private Button btnSend;
   
private EditText tvMessage;
   
private EditText tvNumber;
    IntentFilter
intentFilter;

   
private BroadcastReceiver intentReceiver = new BroadcastReceiver() {
       
@Override
       
public void onReceive(Context context, Intent intent) {
            
//display the message in the textview
           
TextView inTxt = (TextView) findViewById(R.id.txtMessage);
            inTxt.setText(intent.getExtras().getString(
"message"));
        }
    };

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);

       
//intent to filter for SMS message received
       
intentFilter = new IntentFilter();
       
intentFilter.addAction("SMS_RECEIVED_ACTION");

       
btnSend = (Button) findViewById(R.id.btnSend);
       
tvNumber = (EditText) findViewById(R.id.tvNumber);
       
tvMessage = (EditText) findViewById(R.id.tvMessage);

       
btnSend.setOnClickListener(new View.OnClickListener() {
            
@Override
           
public void onClick(View view) {
                String myMsg =
tvMessage.getText().toString();
                String txtNumber =
tvNumber.getText().toString();
                sendMsg(txtNumber, myMsg);
            }
        });
    }

   
private void sendMsg(String txtNumber, String myMsg) {
        String SENT =
"Message Send";
        String DELIVERED =
"Message Delivered";

        PendingIntent sentPi = PendingIntent.getBroadcast(
this, 0, new Intent(SENT), 0);
        PendingIntent deliveredPi = PendingIntent.getBroadcast(
this, 0, new Intent(DELIVERED), 0);

        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(txtNumber,
null, myMsg, sentPi, deliveredPi);
    }

   
@Override
   
protected void onResume() {
       
//register the receiver
       
registerReceiver(intentReceiver, intentFilter);
       
super.onResume();
    }

   
@Override
   
protected void onPause() {
       
//unregister the receiver
       
unregisterReceiver(intentReceiver);
        
super.onPause();
    }
}

lalu buatlah class baru di java dengan nama MessageReceiver dan masukan kode ini
package com.giviews.message;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsMessage;
import android.widget.Toast;

/** * Created by asus on 04/10/2017. */
public class MessageReceiver extends BroadcastReceiver {
    @Override    public void onReceive(Context context, Intent intent) {
        //get message passed in        Bundle bundle = intent.getExtras();
        SmsMessage[] messages;
        String str = "";

        if (bundle != null) {
            Object[] pdus = (Object[]) bundle.get("pdus");
            messages = new SmsMessage[pdus != null ? pdus.length : 0];
            for (int i=0; i<messages.length; i++) {
                messages[i] = SmsMessage.createFromPdu((byte[]) (pdus != null ? pdus[i] : null));
                str += messages[i].getOriginatingAddress();
                str += ": ";
                str += messages[i].getMessageBody();
                str += "\n";
            }

            //display the message            Toast.makeText(context, str, Toast.LENGTH_SHORT).show();

            //Send a broadcast intent to update the SMS received in a TextView            Intent broadcastIntent = new Intent();
            broadcastIntent.setAction("SMS_RECEIVED_ACTION");
            broadcastIntent.putExtra("messages", str);
            context.sendBroadcast(broadcastIntent);
        }
    }
}

Terakhir tambahkan permission di AndroidManifest untuk mengirim
dan menerima SMS

<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>

dan daftarkan class MessageReceiver
<receiver android:name=".MessageReceiver">
    <intent-filter>
        <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
    </intent-filter>
</receiver>

Sekarang coba jalankan di smartphone android anda / di emulator, jika berhasil tampil seperti pada gambar diatas anda sudah berhasil. Ok sekianlah tutorial kali ini Selamat mencoba Semoga bermanfaat kurang dan lebihnya mohon maaf jika ada yang kurang

jelas silakan ditanyakan di komentar

Juanas Smith Shared

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard.

0 komentar :