我已经挖掘了每一种形式,每一个答案 - 都无济于事。 我只是试图通过SmsManager发送短信,但它不断给出错误
java.lang.SecurityException: Sending SMS message: uid 10059 does not have android.permission.SEND_SMS.
但是我的清单设置正确,我已根据需要设置了SMS权限:
清单类是: -
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.jake.test4">
<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:exported="true"
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
android:exported="true"
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
但它仍然无法工作!
我实际上是在试图让这个工作起作用,所以有人可以告诉我到底发生了什么事吗?
我的完整Java就在这里:
package com.example.jake.test4;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.File;
public class MainActivity extends Activity {
String phoneNumber = new String("5556");
String smsBody = new String("You just received an SMS!");
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(findViewById(android.R.id.content), "Sending SMS!", Snackbar.LENGTH_LONG)
.show();
sendSmsByManager();
}
});
}
public void sendSmsByManager() {
try {
// Get the default instance of the SmsManager
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber,
null,
smsBody,
null,
null);
Toast.makeText(getApplicationContext(), "Your sms has successfully sent!",
Toast.LENGTH_LONG).show();
} catch (Exception ex) {
Toast.makeText(getApplicationContext(),"Your sms has failed...",
Toast.LENGTH_LONG).show();
ex.printStackTrace();
}
}
}
应该像发送短信一样简单....有人可以帮助我吗?
答案 0 :(得分:0)
在您的活动中添加此字段
final private int REQUEST_CODE_ASK_PERMISSIONS = 123;
添加你的fab onClickListener
int hasWriteContactsPermission = 0;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
hasWriteContactsPermission = checkSelfPermission(Manifest.permission.SEND_SMS);
}
if (hasWriteContactsPermission != PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!shouldShowRequestPermissionRationale(Manifest.permission.SEND_SMS)) {
showMessageOKCancel("You need to allow access to send sms",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.SEND_SMS},
REQUEST_CODE_ASK_PERMISSIONS);
}
}
});
return;
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.SEND_SMS},
REQUEST_CODE_ASK_PERMISSIONS);
}
return;
}
// your code to send sms
// sendSmsByManager();
在您的活动中添加这两种方法
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE_ASK_PERMISSIONS:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission Granted
} else {
// Permission Denied
Toast.makeText(MainActivity.this, "Send SMS Denied", Toast.LENGTH_SHORT)
.show();
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
new AlertDialog.Builder(MainActivity.this)
.setMessage(message)
.setPositiveButton("OK", okListener)
.setNegativeButton("Cancel", null)
.create()
.show();
}