Android短信管理器收到错误

时间:2016-12-27 15:03:12

标签: android smsmanager

我正在尝试发送一个简单的短信。 问题是它正在研究我从教程中下载的样本。 但是当我试图复制相同的代码时,我收到了错误。 下面是我尝试调试时的代码。

D/ViewRootImpl: ViewPostImeInputStage processPointer 0
D/ViewRootImpl: ViewPostImeInputStage processPointer 1
W/System.err: java.lang.SecurityException: Sending SMS message: uid 10333 does not have android.permission.SEND_SMS.
W/System.err:     at android.os.Parcel.readException(Parcel.java:1620)
W/System.err:     at android.os.Parcel.readException(Parcel.java:1573)
W/System.err:     at com.android.internal.telephony.ISms$Stub$Proxy.sendTextForSubscriber(ISms.java:1577)
W/System.err:     at android.telephony.SmsManager.sendTextMessageInternal(SmsManager.java:380)
W/System.err:     at android.telephony.SmsManager.sendTextMessage(SmsManager.java:333)
W/System.err:     at com.creations.oreo.valletcall.MainActivity$1.onClick(MainActivity.java:40)
W/System.err:     at android.view.View.performClick(View.java:5697)
W/System.err:     at android.widget.TextView.performClick(TextView.java:10814)
W/System.err:     at android.view.View$PerformClick.run(View.java:22526)
W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err:     at android.os.Looper.loop(Looper.java:158)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:7229)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Disconnected from the target VM, address: 'localhost:8616', transport: 'socket'

我正在尝试通过连接实际的手机和权限错误来调试,可能上面显而易见的一个已经在Android Manifest文件中定义了。

我的主要活动如下

public class MainActivity extends AppCompatActivity {

    Button buttonSend;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);




        buttonSend = (Button) findViewById(R.id.buttonSend);

        buttonSend.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {



                try {

                   /* Intent sendIntent = new Intent(Intent.ACTION_VIEW);
                    sendIntent.putExtra("sms_body", "default content");
                    sendIntent.setType("vnd.android-dir/mms-sms");
                    startActivity(sendIntent);*/

                    SmsManager smsManager = SmsManager.getDefault();
                    smsManager.sendTextMessage("91195525", null, "Test", null, null);
                    Toast.makeText(getApplicationContext(), "SMS Sent!",
                            Toast.LENGTH_LONG).show();


                } catch (Exception e) {
                    Toast.makeText(getApplicationContext(),
                            "SMS faild, please try again later SMS!",
                            Toast.LENGTH_LONG).show();
                    e.printStackTrace();
                }

            }
        });


    }
}

我的Android清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.creations.oreo.valletcall">

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />
    <uses-permission android:name="android.permission.SEND_SMS"/>

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



    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        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>

我正在关注的教程就是这个

http://www.mkyong.com/android/how-to-send-sms-message-in-android/

1 个答案:

答案 0 :(得分:0)

参考the official doc

// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
                Manifest.permission.SEND_SMS)
        != PackageManager.PERMISSION_GRANTED) {

    // Should we show an explanation?
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
            Manifest.permission.SEND_SMS)) {

        // Show an explanation to the user *asynchronously* -- don't block
        // this thread waiting for the user's response! After the user
        // sees the explanation, try again to request the permission.

    } else {

        // No explanation needed, we can request the permission.

        ActivityCompat.requestPermissions(thisActivity,
                new String[]{Manifest.permission.SEND_SMS},
                MY_PERMISSIONS_REQUEST_SEND_SMS);

        // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
        // app-defined int constant. The callback method gets the
        // result of the request.
    }
}

您可以使用

阅读结果
@Override
public void onRequestPermissionsResult(int requestCode,
        String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_SEND_SMS: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! Do the
                // contacts-related task you need to do.

            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}

这样你就要求权限运行时,现在你必须使用这些方法来使它们在你的应用程序中运行。

希望这有帮助。

注意:另一种发送短信的方法是intents