2013-04-18 3 views
0

나는 단어를 정의한 다음 SMS를 통해 정의를 보내는 응용 프로그램을 가지고 있습니다. 문제는 사용자가 전화 번호를 입력해야한다는 것입니다. 아마 응용 프로그램이 연락처 목록을 가져와야한다고 생각했기 때문에 사용자는 연락처를 선택하여 해당 연락처로 메시지를 보낼 수 있습니다. 여기에 SMSActivity 내 코드입니다 :연락처를 가져 오는 방법 텍스트를 보내려면

public class SMSActivity extends Activity 
{ 

String APPTAG = "SMSTransmit"; 

//Private static strings: 
private final static String SENT = "SMS_SENT"; 
private final static String DELIVERED = "SMS_DELIVERED"; 

private Button btnSend; 
private Button btnExit; 
private Button btnClear; 
private EditText etPhoneNumber; 
private EditText etUserMessage; 

//Private BroadcastReceiver member variables: 
private SMSDispatchReceiver sendReceiver = null; 
private SMSReceiptReceiver receiptReceiver = null; 


@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_sms); 

    Bundle extras = getIntent().getExtras(); 
    String definiedWord = null; 
    if (extras != null) 
    { 
     definiedWord = extras.getString("DEFINITION"); 
    } 
    else 
    { 
     definiedWord = "None"; 
    } 

    Log.d("recievedAGAIN", definiedWord); 
    Log.v(APPTAG, "MainActivity: onCreate() called"); 

    //Create a new broadcast receiver for sending the SMS 
    sendReceiver = new SMSDispatchReceiver(); 

    //Create a new broadcast receiver for receipt of the send SMS 
    receiptReceiver = new SMSReceiptReceiver(); 

    //Register the new receivers (as new IntentFiler() objects): 
    registerReceiver(sendReceiver, new IntentFilter(SENT)); 
    registerReceiver(receiptReceiver, new IntentFilter(DELIVERED)); 

    //Get the view objects: 
    btnSend = (Button) findViewById(R.id.btnSend); 
    btnClear = (Button) findViewById(R.id.btnClear); 
    btnExit = (Button) findViewById(R.id.btnExit); 
    etPhoneNumber = (EditText) findViewById(R.id.etNumber); 
    etUserMessage = (EditText) findViewById(R.id.etMessage); 

    //Disable the soft keyboard (this is only in general): 
    InputMethodManager inMethodMgr =                (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
    inMethodMgr.hideSoftInputFromInputMethod(etPhoneNumber.getWindowToken(), 0); 
    inMethodMgr.hideSoftInputFromInputMethod(etUserMessage.getWindowToken(), 0); 

    //Set the hint for the EditText boxes: 
    etPhoneNumber.setHint("Enter phone number (Default = 5556)"); 
    etUserMessage.setHint(definiedWord); 

    //Create an click event listener for the Send button (OnClickListener): 
    btnSend.setOnClickListener(new View.OnClickListener() 
    { 

     @Override 
     public void onClick(View v) 
     { 

      String strMessage = null; 
      String strNumber = null; 

      //Get the phone number from the EditText box: 
      strNumber = etPhoneNumber.getText().toString(); 

      //Check the phone number: 
      if (strNumber.length() <= 0) 
      { 

       //No phone number, then get the default (5556): 
       strNumber = SMSProperties.getPhoneNumber(); 
      } 


      //Get the message from the EditText box: 
      strMessage = etUserMessage.getText().toString(); 

      //Check the message contains some content: 
      if (strMessage.length() > 0) 
      { 

       //Sent the SMS to the phone number 
       sendSMS(strNumber, strMessage); 

      } else 
      { 

       //Warn the user and reset the focus/hint: 
       Toast.makeText(getBaseContext(), "No message text! Please  enter some text for the SMS!!", Toast.LENGTH_SHORT).show(); 
       etUserMessage.setHint("Enter message text here . . . "); 
       etUserMessage.requestFocus(); 

      } 

     } 
    }); 



    //Create an click event listener for the Clear button (OnClickListener): 
    btnClear.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      //Clear the appropriate EditText box: 
      if (etUserMessage.isFocused()) { 

       //Clear the text in the user message EditText box: 
       etUserMessage.setText(""); 

       //Set the hint in the user message EditText box: 
       etUserMessage.setHint("Enter message text here . . . "); 

      } else if (etPhoneNumber.isFocused()) { 

       //Clear the text in the phone number EditText box: 
       etPhoneNumber.setText(""); 

       //Set the hint in the phone number EditText box: 
       etPhoneNumber.setHint("Enter phone number (Default = 5556)"); 
      } 
     } 
    }); 



    //Create an click event listener for the Exit button (OnClickListener): 
    btnExit.setOnClickListener(new View.OnClickListener() 
    { 

     @Override 
     public void onClick(View v) 
     { 

      //Finish the activity: 
      //NOTE: As this is the main activity it will cause onDestroy() to be called! 
      finish(); 
     } 
    }); 

} 



//Method to send an SMS message to another device: 
private void sendSMS(String strNum, String strMsg) 
{     
    //TODO: Create a pending intent for the SMSDistatchReceiver (SENT): 
    PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent("SENT"), 0); 

    //TODO: Create a pending intent for the SMSReceiptReceiver (DELIVERED): 
    PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent("DELIVERED"), 0); 
    //Get a default SmsManager object: 
    SmsManager smsMgr = SmsManager.getDefault(); 

    //TODO: Send the SMS using the SmsManager: 
    smsMgr.sendTextMessage(strNum, null, strMsg, sentPI, deliveredPI); 
} 

답변

1

접촉 연락처 목록

contact.setOnClickListener(new View.OnClickListener() {    
      @Override 
      public void onClick(View g) { 
       Intent q = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI); 
       startActivityForResult(q, 1001); 

      } 
     }); 

과 (1001 참조) 결과를 처리하기를 가져올 수있는 버튼입니다 생각이를 사용

public void onActivityResult(int reqCode, int resultCode, Intent data) { 
     super.onActivityResult(reqCode, resultCode, data); 

     if (resultCode == Activity.RESULT_OK) { 
      // getting the URI from result for further working 
      Uri contactData = data.getData(); 
      Cursor c = managedQuery(contactData, null, null, null, null); 

      if (c.moveToFirst()) { 


       String id =c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID)); 

       String hasPhone =c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); 



       if (hasPhone.equalsIgnoreCase("1")) { 
       Cursor phones = getContentResolver().query( 
           ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, 
           ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id, 
           null, null); 
        phones.moveToFirst(); 
         //this string will hold the contact number 
         String cNumber = phones.getString(phones.getColumnIndex("data1")); 
         //this string will hold the contact name 
         String cName = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)); 

        } 

      }} 
     } 
관련 문제