0

Please I need help! I really have no idea in Java programming. We're trying to make an SMS Reminder sender, more of like an alarm clock for message sending. I want to connect my app to the phone's phonebook instead of manually typing it but i have no idea how, where and what codes to put. I hope you can help me on this one. And also please note if you saw errors that i'm not aware of. Thank you.

MainActivity.java:

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.app.Dialog; 

public class MainActivity extends Activity

{
Button btnSendSMS;
EditText txtPhoneNo;
EditText txtMessage;
timeAndDatePicker timeClass = new timeAndDatePicker();
alertFunction alertClass = new alertFunction();

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);        

btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
txtPhoneNo = (EditText) findViewById(R.id.txtPhoneNo);
txtMessage = (EditText) findViewById(R.id.txtMessage);

btnSendSMS.setOnClickListener(new View.OnClickListener() 
{
   public void onClick(View v) 
   {                
       String phoneNo = txtPhoneNo.getText().toString();
       String message = txtMessage.getText().toString();                 
       if (phoneNo.length()>0 && message.length()>0)                
           sendSMS(phoneNo, message);                
       else
           Toast.makeText(getBaseContext(), 
               "Please enter both phone number and message.", 
               Toast.LENGTH_SHORT).show();

       timeClass.setCurrentDateOnView();
       timeClass.addListenerOnButton();
       alertClass.onCreate();  

   }


 private void sendSMS(String phoneNo, String message) {
   String SENT = "SMS_SENT";
   String DELIVERED = "SMS_DELIVERED";

   PendingIntent sentPI = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(SENT), 0); 

   PendingIntent deliveredPI = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(DELIVERED), 0); 

   // ---when the SMS has been sent---
   registerReceiver(new BroadcastReceiver() {
       @Override
       public void onReceive(Context arg0, Intent arg1) {
           switch (getResultCode()) {
               case Activity.RESULT_OK:
                   Toast.makeText(getBaseContext(), "SMS sent", Toast.LENGTH_SHORT).show();
                   break;
               case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                   Toast.makeText(getBaseContext(), "Generic failure", Toast.LENGTH_SHORT).show();
                   break;
               case SmsManager.RESULT_ERROR_NO_SERVICE:
                   Toast.makeText(getBaseContext(), "No service", Toast.LENGTH_SHORT).show();
                   break;
               case SmsManager.RESULT_ERROR_NULL_PDU:
                   Toast.makeText(getBaseContext(), "Null PDU", Toast.LENGTH_SHORT).show();
                   break;
               case SmsManager.RESULT_ERROR_RADIO_OFF:
                   Toast.makeText(getBaseContext(), "Radio off", Toast.LENGTH_SHORT).show();
                   break;
           }
       }
   }, new IntentFilter(SENT));

   // ---when the SMS has been delivered---
   registerReceiver(new BroadcastReceiver() {
       @Override
       public void onReceive(Context arg0, Intent arg1) {
           switch (getResultCode()) {
               case Activity.RESULT_OK:
                   Toast.makeText(getBaseContext(), "SMS delivered", Toast.LENGTH_SHORT).show();
                   break;
               case Activity.RESULT_CANCELED:
                   Toast.makeText(getBaseContext(), "SMS not delivered", Toast.LENGTH_SHORT).show();
                   break;
           }
       }
   }, new IntentFilter(DELIVERED));

   SmsManager sms = SmsManager.getDefault();
   sms.sendTextMessage(phoneNo, null, message, sentPI, deliveredPI);
}

});
}

}

I found some answers on how to call contacts, but i don't know where i'll insert those in my codes. It's really confusing for me.

Chix
  • 73
  • 1
  • 6

1 Answers1

0

use this code...

ContentResolver contactResolver = getContentResolver(); 
Cursor cursor = contactResolver.query(Phone.CONTENT_URI, null, Phone.DISPLAY_NAME + "=?",          new String[]{contactName}, null);

if(cursor.getCount() > 0){
cursor.moveToFirst();
do {
   String number = cursor.getString(mCursor.getColumnIndex(Phone.NUMBER));
}while (cursor.moveToNext() ); 
}

or refer this link

http://stackoverflow.com/questions/8785131/fetch-contacts-in-android-application
Shashank Agarwal
  • 512
  • 3
  • 12
  • Thank you so much for your answer, but i have another problem. i'm a beginner, i don't where on my MAinActivity i'm going to place your codes. – Chix Mar 13 '14 at 07:36
  • have you checkd above link. its has detailed explanation.. Read about activity from this link http://developer.android.com/reference/android/app/Activity.html – Shashank Agarwal Mar 13 '14 at 07:40