2014-01-15 3 views
1

그래서 사용자가 버튼을 누르면 앱이 주어진 번호로 자동으로 SMS를 보내도록하려고합니다.Android - 버튼 클릭시 SMS 보내기.

메신저를 열고 텍스트를 쓸 수는 있지만 자동으로 보낼 수는 없습니다.

내 코드는 다음과 같습니다 (내가 추측하는 부분).

@Override 
public void onClick(View a) { 


    if(a.equals(sms)){ 
     tekst = (TextView) findViewById(R.id.txt); 
     Uri tlf = Uri.parse("smsto:"+tekst.getText().toString()); 
     Intent c = new Intent(Intent.ACTION_VIEW, tlf); 
     c.setData(tlf); 
     c.putExtra("sms_body","Hjelp jeg er i fare!"); 
     startActivity(c); 



    }else{ 
     tekst = (TextView) findViewById(R.id.txt); 
     Intent c = new Intent(Intent.ACTION_CALL); 
     Uri tlf = Uri.parse("tel:"+tekst.getText().toString()); 
     c.setData(tlf); 
     startActivity(c); 


    } 

} 

그래서 어떻게 SMS를 보낼 수 있습니까? "android.permission.SEND_SMS"이와

+1

당신은 다른 SmsManager''과 인터페이스 방식 (http://developer.android.com/reference/android/telephony/SmsManager.html)이 아닌 기본 SMS 앱에서 소비있어 의도를 생성을해야 . – Estel

답변

0

보십시오 : 당신이 더 많은 것을 구현할 수있는 매우 간단한 싹둑 코드의

String phoneNumber = "<phone_number_here>"; 
String message = "Test Message"; 
SmsManager smsManager = SmsManager.getDefault(); 
smsManager.sendTextMessage(phoneNumber, null, message, null, null); 

공지 것을

BTW, 나는 권한을 추가했습니다.

ContentValues values = new ContentValues(); 
values.put("address", "<phone_number_here>"); 
values.put("body", "Test Message"); 
getContentResolver().insert(Uri.parse("content://sms/sent"), values); 

을 추가하십시오 : 당신은 SMS 다른 SMS 클라이언트의에 표시 보려면 은/앱은 yuo 사용해야 설치하려고

<uses-permission android:name="android.permission.WRITE_SMS" /> 
<uses-permission android:name="android.permission.READ_SMS" /> 
0

을이

try { 
     SmsManager smsManager = SmsManager.getDefault(); 
     smsManager.sendTextMessage([number], null, [sms], null, null); 
     Toast.makeText(getApplicationContext(), "SMS Sent!", 
      Toast.LENGTH_SHORT).show(); 
    } catch (Exception e) { 
     Toast.makeText(getApplicationContext(), 
      "SMS faild, please try again later!", 
        Toast.LENGTH_SHORT).show(); 
     e.printStackTrace(); 

    } 

곳 [번호]는 SMS를 보내려는 번호이고 [SMS]는 보내려는 텍스트입니다.

0

이것을 시도하십시오.

//Declare the button and the tetviews to input number and the message 
Button sendSMSBtnBtn = (Button) findViewById(R.id.btnSendSMS); 
txtphoneNo = (EditText) findViewById(R.id.editTextPhoneNo); 
txtMessage = (EditText) findViewById(R.id.editTextSMS); 

//Calling the method sendSMSMessage in the button click event 
sendSMSBtn.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View view) { 
      sendSMSMessage(); 
     } 
});} 

// Method to send SMS using SMS Manager 
protected void sendSMSMessage() { 
     Log.i("Send SMS", ""); 

     String phoneNo = txtphoneNo.getText().toString(); 
     String message = txtMessage.getText().toString(); 

     try { 
     SmsManager smsManager = SmsManager.getDefault(); 
     smsManager.sendTextMessage(phoneNo, null, message, null, null); 
     Toast.makeText(getApplicationContext(), "SMS sent.", 
     Toast.LENGTH_LONG).show(); 
     } catch (Exception e) { 
     Toast.makeText(getApplicationContext(), 
     "SMS faild, please try again.", 
     Toast.LENGTH_LONG).show(); 
     e.printStackTrace(); 
     } 
    } 

참고 : 매니페스트 파일에 다음 권한을 설정해야합니다.

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