반응형
인 텐트를 통해 SMS 보내기
인 텐트를 통해 SMS를 보내고 싶지만이 코드를 사용하면 잘못된 연락처로 리디렉션됩니다.
Intent intentt = new Intent(Intent.ACTION_VIEW);
intentt.setData(Uri.parse("sms:"));
intentt.setType("vnd.android-dir/mms-sms");
intentt.putExtra(Intent.EXTRA_TEXT, "");
intentt.putExtra("address", phone number);
context.startActivity(intentt);
왜?
또한 SMS 전송을 따르는 방법을 알고 있지만 어떻게 코딩하는지 모르겠습니다.
Starting activity: Intent {
act=android.intent.action.SENDTO dat=smsto:%2B**XXXXXXXXXXXX** flg=0x14000000
cmp=com.android.mms/.ui.ComposeMessageActivity }
여기서 XXXXXXXXXXXX는 전화 번호입니다.
한 블로그에서이 기능을 개발했습니다. SMS를 보낼 수있는 방법에는 두 가지가 있습니다.
- 기본 SMS 작성기 열기
- 메시지를 작성하고 Android 애플리케이션에서 전송
첫 번째 방법의 코드입니다.
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="@+id/btnSendSMS"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Send SMS"
android:layout_centerInParent="true"
android:onClick="sendSMS">
</Button>
</RelativeLayout>
활동
public class SendSMSActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void sendSMS(View v)
{
String number = "12346556"; // The number on which you want to send SMS
startActivity(new Intent(Intent.ACTION_VIEW, Uri.fromParts("sms", number, null)));
}
/* or
public void sendSMS(View v)
{
Uri uri = Uri.parse("smsto:12346556");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
it.putExtra("sms_body", "Here you can set the SMS text to be sent");
startActivity(it);
} */
}
참고 :- 이 방법에서는 AndroidManifest.xml 파일 내에서 SEND_SMS 권한이 필요하지 않습니다.
두 번째 방법은이 BLOG를 참조하십시오 . 여기에서 좋은 설명을 찾을 수 있습니다.
이것이 도움이되기를 바랍니다 ...
Uri uri = Uri.parse("smsto:YOUR_SMS_NUMBER");
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
intent.putExtra("sms_body", "The SMS text");
startActivity(intent);
다음과 같이 인 텐트를 만듭니다.
Intent smsIntent = new Intent(android.content.Intent.ACTION_VIEW);
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.putExtra("address","your desired phoneNumber");
smsIntent.putExtra("sms_body","your desired message");
startActivity(smsIntent);
이 코드를 사용해보십시오. 작동합니다
Uri smsUri = Uri.parse("tel:123456");
Intent intent = new Intent(Intent.ACTION_VIEW, smsUri);
intent.putExtra("sms_body", "sms text");
intent.setType("vnd.android-dir/mms-sms");
startActivity(intent);
이것이 당신을 도울 것입니다.
이것이 작동하기를 바랍니다. 이것은 내 앱에서 작동합니다.
SmsManager.getDefault().sendTextMessage("Phone Number", null, "Message", null, null);
/**
* Intent to Send SMS
*
*
* Extras:
*
* "subject"
* A string for the message subject (usually for MMS only).
* "sms_body"
* A string for the text message.
* EXTRA_STREAM
* A Uri pointing to the image or video to attach.
*
* For More Info:
* https://developer.android.com/guide/components/intents-common#SendMessage
*
* @param phoneNumber on which SMS to send
* @param message text Message to send with SMS
*/
public void startSMSIntent(String phoneNumber, String message) {
Intent intent = new Intent(Intent.ACTION_SENDTO);
// This ensures only SMS apps respond
intent.setData(Uri.parse("smsto:"+phoneNumber));
intent.putExtra("sms_body", message);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
다음과 같이 인 텐트를 만듭니다.
Uri uriSms = Uri.parse("smsto:1234567899");
Intent intentSMS = new Intent(Intent.ACTION_SENDTO, uriSms);
intentSMS.putExtra("sms_body", "The SMS text");
startActivity(intentSMS);
This is another solution using SMSManager:
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("PhoneNumber-example:+989147375410", null, "SMS Message Body", null, null);
If you want a certain message, use this:
String phoneNo = "";//The phone number you want to text
String sms= "";//The message you want to text to the phone
Intent smsIntent = new Intent(Intent.ACTION_VIEW, Uri.fromParts("sms", phoneNo, null));
smsIntent.putExtra("sms_body",sms);
startActivity(smsIntent);
- Manifest permission (you can put it after or before "application" )
uses-permission android:name="android.permission.SEND_SMS"/>
- make a button for example and write the below code ( as written before by
Prem
at this thread ) and replace the below phone_Number by an actual number, it will work:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.fromParts("sms", "phone_Number", null)));
Add try-catch otherwise phones without sim will crash.
void sentMessage(String msg) {
try {
Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.putExtra("sms_body", msg);
startActivity(smsIntent);
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "No SIM Found", Toast.LENGTH_LONG).show();
}
}
참고URL : https://stackoverflow.com/questions/9798657/send-a-sms-via-intent
반응형
'Development Tip' 카테고리의 다른 글
Xcode 4에서 .xcconfig 파일을 어떻게 사용할 수 있습니까? (0) | 2020.11.16 |
---|---|
Javascript에서 배열에 중복 값이 있는지 어떻게 확인합니까? (0) | 2020.11.16 |
html 요소에 첨부 된 이벤트를 어떻게 볼 수 있습니까? (0) | 2020.11.16 |
iOS Safari / Chrome / Firefox에서 클릭 한 링크에서 회색 배경 제거 (0) | 2020.11.16 |
HttpClient 헤더를 추가하면 일부 값이 포함 된 FormatException이 생성됩니다. (0) | 2020.11.16 |