Ich möchte eine SMS per Absicht senden, aber wenn ich diesen Code verwende, wird er an einen falschen Kontakt weitergeleitet:
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);
Warum? Ich kenne auch einen Weg, dem Senden von SMS zu folgen, aber ich weiß nicht, wie dieser Code codiert wird:
Starting activity: Intent {
act=Android.intent.action.SENDTO dat=smsto:%2B**XXXXXXXXXXXX** flg=0x14000000
cmp=com.Android.mms/.ui.ComposeMessageActivity }
wobei XXXXXXXXXXXX die Telefonnummer ist.
Ich habe diese Funktionalität aus einem Blog heraus entwickelt. Es gibt zwei Möglichkeiten, SMS zu senden.
Dies ist der Code der 1. Methode.
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>
Aktivität
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);
} */
}
HINWEIS: - Bei dieser Methode benötigen Sie keine SEND_SMS-Berechtigung in der Datei AndroidManifest.xml.
Für die zweite Methode verweisen wir auf diesen BLOG . Hier finden Sie eine gute Erklärung.
Hoffe das wird dir helfen ...
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);
Hoffe das jemandem helfen
Versuchen Sie diesen Code. Es wird klappen
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);
Hoffe, das wird dir helfen.
Hoffe das ist Arbeit, das funktioniert in meiner App
SmsManager.getDefault().sendTextMessage("Phone Number", null, "Message", null, null);
Dies ist eine andere Lösung, die SMSManager verwendet:
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("PhoneNumber-example:+989147375410", null, "SMS Message Body", null, null);
Uri uriSms = Uri.parse("smsto:1234567899");
Intent intentSMS = new Intent(Intent.ACTION_SENDTO, uriSms);
intentSMS.putExtra("sms_body", "The SMS text");
startActivity(intentSMS);
/**
* 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);
}
}
Wenn Sie eine bestimmte Nachricht wünschen, verwenden Sie diese:
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);
uses-permission Android:name="Android.permission.SEND_SMS"/>
Prem
in diesen Thread geschrieben) und ersetze die folgende phone_Number durch eine tatsächliche Nummer, es wird funktionieren:startActivity(new Intent(Intent.ACTION_VIEW, Uri.fromParts("sms", "phone_Number", null)));