Mengirimkan Sms dengan Android Source Code / Mengambil nomor contact dari android
Nah pada kesempatan kali ini ane ingin berbagi ilmu lagi nih pada temen - temen yang ingin mendalami mengenai android. Nah pada tutorial kali ini akan ane berikan sedikit tutorial mengenai mengirimkan pesan di android. Pada dasarnya kita dapat mengirimkan pesan di android dengan mudah dengan memanfaatkan kelas SmsManager yang ada di android. Oke langsung saja kita desain layoutnya ya..<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/back" android:orientation="vertical"> <ImageView android:src="@drawable/header" android:layout_width="wrap_content" android:layout_height="wrap_content"> </ImageView> <!-- layout untuk header --> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="20dip" android:layout_marginLeft="20dip"> <!-- layout gambar home --> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/home"></ImageView> <TextView android:text="Main Menu / Generate Chart" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="#000000" android:textStyle="bold" android:textSize="17dip" android:id="@+id/title_caption"></TextView> </LinearLayout> <!-- layout untuk garis line --> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/line_gradient" android:layout_marginTop="-15dip"></RelativeLayout> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/line" android:layout_marginTop="20dip"></RelativeLayout> <ScrollView android:layout_width="fill_parent" android:layout_height="wrap_content"> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:text="Nomor HP" android:textColor="#000000" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <EditText android:text="" android:id="@+id/TextNoHP" android:layout_width="210dip" android:layout_height="wrap_content"></EditText> <ImageButton android:layout_width="wrap_content" android:id="@+id/button_add_contact" android:layout_height="wrap_content" android:src="@drawable/add_contact"></ImageButton> <ImageButton android:layout_width="wrap_content" android:id="@+id/button_send" android:layout_height="wrap_content" android:src="@drawable/sms"></ImageButton> </LinearLayout> <TextView android:text="Pesan" android:textColor="#000000" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> <EditText android:text="" android:id="@+id/TextPesan" android:layout_width="fill_parent" android:minLines="4" android:layout_height="wrap_content"></EditText> </LinearLayout> </ScrollView> </LinearLayout>
package org.data.provider; import java.util.List; import android.app.Activity; import android.app.PendingIntent; import android.content.ContentResolver; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.provider.ContactsContract; import android.telephony.SmsManager; import android.view.View; import android.view.View.OnClickListener; import android.widget.EditText; import android.widget.ImageButton; import android.widget.Toast; public class SmsActivity extends MyActivity { public static final String ACTION_SMS_SENT = "com.example.android.apis.os.SMS_SENT_ACTION"; public static final int PICK_CONTACT = 1; private EditText EditTextPesan, EditTextNomorHp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_sms); String sentText = getIntent().getExtras().getString("SEND_TEXT"); EditTextPesan = (EditText) findViewById(R.id.TextPesan); EditTextNomorHp = (EditText) findViewById(R.id.TextNoHP); EditTextPesan.setText(sentText); final SmsManager sms = SmsManager.getDefault(); ImageButton buttonSend = (ImageButton) findViewById(R.id.button_send); buttonSend.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if(EditTextNomorHp.getText().toString().equals("")){ Dialog.pesanOk("Maaf anda belum memilih nomor telepon", SmsActivity.this); return; } Listmessages = sms.divideMessage(EditTextPesan .getText().toString()); String recipient = EditTextNomorHp.getText().toString(); for (String message : messages) { sms.sendTextMessage(recipient, null, message, PendingIntent .getBroadcast(SmsActivity.this, 0, new Intent( ACTION_SMS_SENT), 0), null); } Toast.makeText(SmsActivity.this, "Pesan anda berhasil dikirim ", Toast.LENGTH_LONG).show(); } }); ImageButton img = (ImageButton) findViewById(R.id.button_add_contact); img.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); startActivityForResult(intent, PICK_CONTACT); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == PICK_CONTACT && resultCode==Activity.RESULT_OK) { Uri contactData = data.getData(); ContentResolver cr = getContentResolver(); Cursor cur = managedQuery(contactData, null, null, null, null); if (cur.getCount() > 0) { while (cur.moveToNext()) { String id = cur.getString(cur .getColumnIndex(ContactsContract.Contacts._ID)); if (Integer .parseInt(cur.getString(cur .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) { Cursor pCur = cr .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null); while (pCur.moveToNext()) { String nomorHp = pCur .getString(pCur .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA)); EditTextNomorHp.setText(nomorHp); } pCur.close(); } } } } } }
Nah cukup simple kan. Memang semua fungsi yang ada di android dapat dilakukan dengan mudah..
Semoga bermanfaat.
Update
Karena banyak yang menanyakan project dari source ini langsung saja download full projectnya disini yah..
https://dl.dropbox.com/u/20916268/Data%20Service%20Provider%209%20oktober%202011.zip