Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 安卓点击按钮自动发送短信_Java_Android_Android Intent_Sms_Send - Fatal编程技术网

Java 安卓点击按钮自动发送短信

Java 安卓点击按钮自动发送短信,java,android,android-intent,sms,send,Java,Android,Android Intent,Sms,Send,当用户按下屏幕上的按钮时,我尝试自动向特定号码发送SMS消息 这是我的代码: Intent smsIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("sms:xxxxxxxxxxx")); smsIntent.putExtra("sms_body", "Hello"); startActivity(smsIntent); xxxxxxx=电话号码 我有以下权限: <uses-permission android:name="andr

当用户按下屏幕上的按钮时,我尝试自动向特定号码发送SMS消息

这是我的代码:

Intent smsIntent = new Intent(Intent.ACTION_SENDTO,
Uri.parse("sms:xxxxxxxxxxx")); 
smsIntent.putExtra("sms_body", "Hello");
startActivity(smsIntent);
xxxxxxx=电话号码

我有以下权限:

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

当我按下按钮时,它会将我带到另一个屏幕,在那里我可以编辑我的文本并按下Send。我只想让它自动完成这个过程,而不需要把我带到另一个屏幕。因为我已经定义了我的消息,所以我只想将它发送到一个特定的号码

而且我也不确定我是否在第二行代码中输入了正确的电话号码。我必须先把我的国家代码放在那里吗?或者我可以只把我的手机号码放在那里就行了吗

谢谢

请尝试以下代码:

 String messageToSend = "this is a message";
 String number = "2121234567";

 SmsManager.getDefault().sendTextMessage(number, null, messageToSend, null,null);
关于号码,您需要输入该号码,就好像您是通过手机拨打电话或以正常方式发送短信一样。

试试这个

private static final String SMS_SENT_INTENT_FILTER = "com.yourapp.sms_send";
private static final String SMS_DELIVERED_INTENT_FILTER = "com.yourapp.sms_delivered";

String message = "hey, this is my message";

String phnNo = " " //preferable use complete international number

PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(
                SMS_SENT_INTENT_FILTER), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(
                SMS_DELIVERED_INTENT_FILTER), 0);

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

您还可以使用内置意图:

 buttonSendSms_intent.setOnClickListener(new Button.OnClickListener(){

       @Override
       public void onClick(View arg0) {
        // TODO Auto-generated method stub

        String smsNumber = edittextSmsNumber.getText().toString();
        String smsText = edittextSmsText.getText().toString();

        Uri uri = Uri.parse("smsto:" + smsNumber);
        Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
        intent.putExtra("sms_body", smsText);  
        startActivity(intent);
       }});

简单的方法是使用SmsManager.Telephony。

首先设置以下权限:

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

成功地完成了。

我正在做类似的事情@chris我正在使用您的代码,但无法通过我的号码接收短信。无论我使用哪个号码,我们如何检测故障(由于SIM卡不可用或网络问题等情况)在此代码中发送SMS的权限?在新的Google策略更改后,请确保您的应用程序在发布到play store时可以使用SMS权限来使用此命令。问题在于,如果有多个活动能够处理
Intent.ACTION\u SENDTO
操作,则会显示一个选择器,允许用户选择一个应用程序发送sms(因此在这种情况下需要人工干预)。我猜想OP想在没有人为干预的情况下自动完成这项工作。
    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

        <EditText
            android:id="@+id/editText"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="41dp"
            android:layout_marginLeft="41dp"
            android:layout_marginRight="41dp"
            android:layout_marginStart="41dp"
            android:layout_marginTop="43dp"
            android:ems="10"
            android:hint="Enter Phone no"
            android:inputType="textPersonName"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <EditText
            android:id="@+id/editText2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="37dp"
            android:layout_marginLeft="37dp"
            android:layout_marginRight="37dp"
            android:layout_marginStart="37dp"
            android:layout_marginTop="28dp"
            android:ems="10"
            android:hint="Enter text"
            android:inputType="textPersonName"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/editText" />

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="183dp"
            android:text="SEND"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />
    </android.support.constraint.ConstraintLayout> 
public class MainActivity extends AppCompatActivity {

    EditText e1, e2;
    Button b1;
    private final static int SEND_SMS_PERMISSION_REQ=1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        e1=findViewById(R.id.editText);
        e2=findViewById(R.id.editText2);
        b1=findViewById(R.id.button);
        b1.setEnabled(false);


        if(checkPermission(Manifest.permission.SEND_SMS))
        {
            b1.setEnabled(true);
        }
        else
        {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS}, SEND_SMS_PERMISSION_REQ);
        }
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s1=e1.getText().toString();
                String s2=e2.getText().toString();
                if(!TextUtils.isEmpty(s1)&&!TextUtils.isEmpty(s2))
                {

                    if(checkPermission(Manifest.permission.SEND_SMS))
                    {
                        SmsManager smsManager=SmsManager.getDefault();
                         smsManager.sendTextMessage(s1,null,s2,null,null);
                    }
                    else {
                        Toast.makeText(MainActivity.this, "Permission Denied", Toast.LENGTH_SHORT).show();
                    }
                }
                else
                {
                    Toast.makeText(MainActivity.this, "Permission denied", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

    private boolean checkPermission(String sendSms) {

        int checkpermission= ContextCompat.checkSelfPermission(this,sendSms);
        return checkpermission== PackageManager.PERMISSION_GRANTED;
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode)
        {
            case SEND_SMS_PERMISSION_REQ:
                if(grantResults.length>0 &&(grantResults[0]==PackageManager.PERMISSION_GRANTED))
                {
                    b1.setEnabled(true);
                }
                break;
        }
    }
}