Android短信管理器获取错误

lztngnrs  于 12个月前  发布在  Android
关注(0)|答案(2)|浏览(159)

我试图发送一个简单的短信.事情是它是我从教程下载的样本工作.但当我试图复制相同的代码,我得到错误.下面是代码时,我试图调试.

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清单文件中定义。
我的主要活动如下

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/

eqqqjvef

eqqqjvef1#

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

ny6fqffe

ny6fqffe2#

您只需在电话号码中添加+91或任何其他国家代码即可

smsManager.sendTextMessage("+9191195525", null, "Test", null, null);

字符串

相关问题