Android Studio中两个模拟器互发短信的解决方案

折腾了半天,查资料后发现是由于当前版本较高,危险权限即使申明了也不能用造成的。

网上许多资料都有误导性,其实只需要多加几行代码就解决了,先把成功的代码贴上来:

(1)主活动代码:


import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.widget.EditText;
import android.widget.Toast;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;

import java.util.List;

public class MainActivity extends Activity implements ActivityCompat.OnRequestPermissionsResultCallback{
    /*数据成员*/
    private Button snd;//发送键
    private EditText tel;//电话号码
    private EditText txt;//要发送的文本
    /*活动被创建时调用的方法:onCreate()*/
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        snd = findViewById(R.id.Button01);
        tel = findViewById(R.id.EditText01);
        txt = findViewById(R.id.EditText02);
        snd.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                sendShortMessage();//点击之后调用sendShortMessage()方法
            }
        });
    }
    /*发送信息的函数*/
    protected void sendShortMessage() {
        String phoneNo = tel.getText().toString();
        String message = txt.getText().toString();
        int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE);

        if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE}, 0);
        }
        try {
            SmsManager smsManager = SmsManager.getDefault();
            List<String> texts = smsManager.divideMessage(message);
            for (String text : texts) {
                smsManager.sendTextMessage(phoneNo, null, text, null, null);
            }
            Toast.makeText(getApplicationContext(), "发送成功",
                    Toast.LENGTH_LONG).show();
        }
        catch (Exception e) {
            Toast.makeText(getApplicationContext(),
                    "发送失败",
                    Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }
    }
}

(2)布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >
    <EditText
        android:id="@+id/EditText01"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    <EditText
        android:id="@+id/EditText02"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    <Button
        android:id="@+id/Button01"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="发送"
        />
</LinearLayout>
(3)Manifest文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.wuyusong.exp05">
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.SEND_SMS"/>
    <uses-permission android:name="android.permission.READ_SMS"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    </activity>
    </application>
    </manifest>
运行效果:

在运行之前需要检查权限,然后授权以后才能拿到READ_PHONE_STATE。关键代码:

public class MainActivity extends Activity implements ActivityCompat.OnRequestPermissionsResultCallback{...}
以及:

        int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE);

        if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE}, 0);
        }




阅读更多 登录后自动展开
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页