如何修复短信权限错误 android studio?
how to fix sms permission error android studio?
在这个项目中,我想向目标号码发送位置消息。
我在清单中添加了 SEND_SMS,我希望这个应用程序在我按下按钮时发送一条消息。但是代码有问题
这是 logcat
中的错误
020-05-06 16:38:42.694 20708-20905/com.example.sos2 E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
Process: com.example.sos2, PID: 20708
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask.done(AsyncTask.java:353)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
at java.util.concurrent.FutureTask.run(FutureTask.java:271)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
Caused by: java.lang.SecurityException: Sending SMS message: uid 10218 does not have android.permission.SEND_SMS.
at android.os.Parcel.readException(Parcel.java:1960)
at android.os.Parcel.readException(Parcel.java:1899)
at com.android.internal.telephony.ISms$Stub$Proxy.sendTextForSubscriber(ISms.java:867)
at android.telephony.SmsManager.sendTextMessageInternal(SmsManager.java:367)
at android.telephony.SmsManager.sendTextMessage(SmsManager.java:350)
at com.example.sos2.SendMessage1$SendMessageTask.doInBackground(SendMessage1.java:792)
at com.example.sos2.SendMessage1$SendMessageTask.doInBackground(SendMessage1.java:727)
at android.os.AsyncTask.call(AsyncTask.java:333)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
这是我的javaclass
lass SendMessageTask extends AsyncTask<Void, Integer, Void> { //I have changed the input from View to Void
View view;
String address,city,state,country,postal_code,known_name;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... view) {
dTap = 0;
for (int i = 1; i <= 40; i++) {
try {
Thread.sleep(500);
presentTime=i*500;
if(i%2 == 0)
{
publishProgress(500*i/1000+10);
}
// Log.d("hehe","thread is running");
} catch (InterruptedException e) {
e.printStackTrace();
}
if (dTap == 1) {
dTap=0;
// Log.d("hehe","dtap is 1");
publishProgress(2);
return null;
}
}
publishProgress(1);
// Log.d("hehe","starting recording");
// Log.d("hehe","Send Message loc: "+finalAddress);
startRecording();
SharedPreferences sp = getSharedPreferences("Contacts", Context.MODE_PRIVATE);
String s1 = sp.getString("1", "chooseContact1");
String s2 = sp.getString("2", "chooseContact2");
String s3 = sp.getString("3", "chooseContact3");
String s4 = sp.getString("4", "chooseContact4");
String s5 = sp.getString("5", "chooseContact5");
SmsManager d = SmsManager.getDefault();
SmsManager smsManager = SmsManager.getDefault();
s1 = s1.replaceAll("\s+","");
s2 = s2.replaceAll("\s+","");
s3 = s3.replaceAll("\s+","");
s4 = s4.replaceAll("\s+","");
s5 = s5.replaceAll("\s+","");
if(finalAddress.equals("Hey.. I am in Danger. Please, help me ASAP!!")){
publishProgress(500);
}
// Log.d("hehe",s1);
if(!s1.equals("chooseContact1")) {
// Toast.makeText(SendMessage1.this,"this is contact one "+ finalAddress,Toast.LENGTH_SHORT).show();
// Log.d("hehe",finalAddress+"this is sms 1");
smsManager.sendTextMessage(s1,null,finalAddress,null,null);
// Log.d("hehe","sending sms"+finalAddress);
}
if (!s2.equals("chooseContact2")) {
smsManager.sendTextMessage(s2, null, finalAddress, null, null); //must be uncommented
}
if (!s3.equals("chooseContact3")) {
smsManager.sendTextMessage(s3, null, finalAddress, null, null);
}
if (!s4.equals("chooseContact4")) {
smsManager.sendTextMessage(s4, null, finalAddress, null, null);
}
if (!s5.equals("chooseContact5")) {
smsManager.sendTextMessage(s5, null, finalAddress, null, null);
}
//Geocoder geoCoder ;
// Toast.makeText(this,ans +"this is the address",Toast.LENGTH_SHORT).show();
return null;
}
我应该更改什么或将此代码添加到 Fixx?帮帮我伙计们
我在这个项目中使用 SDK 23
自从 API 23 以来,您还必须在运行时检查权限。用户可以随时撤销您的应用程序的权限,因此您必须确保在执行异步任务之前您仍然可以发送短信.
尝试在异步任务之前添加类似这样的内容:
if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED) {
// Permission is not granted
// You have to ask for permission again
} else {
// Granted, you can start you async task here
}
文档:
https://developer.android.com/training/permissions/requesting.html
在这个项目中,我想向目标号码发送位置消息。 我在清单中添加了 SEND_SMS,我希望这个应用程序在我按下按钮时发送一条消息。但是代码有问题
这是 logcat
中的错误
020-05-06 16:38:42.694 20708-20905/com.example.sos2 E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
Process: com.example.sos2, PID: 20708
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask.done(AsyncTask.java:353)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
at java.util.concurrent.FutureTask.run(FutureTask.java:271)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
Caused by: java.lang.SecurityException: Sending SMS message: uid 10218 does not have android.permission.SEND_SMS.
at android.os.Parcel.readException(Parcel.java:1960)
at android.os.Parcel.readException(Parcel.java:1899)
at com.android.internal.telephony.ISms$Stub$Proxy.sendTextForSubscriber(ISms.java:867)
at android.telephony.SmsManager.sendTextMessageInternal(SmsManager.java:367)
at android.telephony.SmsManager.sendTextMessage(SmsManager.java:350)
at com.example.sos2.SendMessage1$SendMessageTask.doInBackground(SendMessage1.java:792)
at com.example.sos2.SendMessage1$SendMessageTask.doInBackground(SendMessage1.java:727)
at android.os.AsyncTask.call(AsyncTask.java:333)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
这是我的javaclass
lass SendMessageTask extends AsyncTask<Void, Integer, Void> { //I have changed the input from View to Void
View view;
String address,city,state,country,postal_code,known_name;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... view) {
dTap = 0;
for (int i = 1; i <= 40; i++) {
try {
Thread.sleep(500);
presentTime=i*500;
if(i%2 == 0)
{
publishProgress(500*i/1000+10);
}
// Log.d("hehe","thread is running");
} catch (InterruptedException e) {
e.printStackTrace();
}
if (dTap == 1) {
dTap=0;
// Log.d("hehe","dtap is 1");
publishProgress(2);
return null;
}
}
publishProgress(1);
// Log.d("hehe","starting recording");
// Log.d("hehe","Send Message loc: "+finalAddress);
startRecording();
SharedPreferences sp = getSharedPreferences("Contacts", Context.MODE_PRIVATE);
String s1 = sp.getString("1", "chooseContact1");
String s2 = sp.getString("2", "chooseContact2");
String s3 = sp.getString("3", "chooseContact3");
String s4 = sp.getString("4", "chooseContact4");
String s5 = sp.getString("5", "chooseContact5");
SmsManager d = SmsManager.getDefault();
SmsManager smsManager = SmsManager.getDefault();
s1 = s1.replaceAll("\s+","");
s2 = s2.replaceAll("\s+","");
s3 = s3.replaceAll("\s+","");
s4 = s4.replaceAll("\s+","");
s5 = s5.replaceAll("\s+","");
if(finalAddress.equals("Hey.. I am in Danger. Please, help me ASAP!!")){
publishProgress(500);
}
// Log.d("hehe",s1);
if(!s1.equals("chooseContact1")) {
// Toast.makeText(SendMessage1.this,"this is contact one "+ finalAddress,Toast.LENGTH_SHORT).show();
// Log.d("hehe",finalAddress+"this is sms 1");
smsManager.sendTextMessage(s1,null,finalAddress,null,null);
// Log.d("hehe","sending sms"+finalAddress);
}
if (!s2.equals("chooseContact2")) {
smsManager.sendTextMessage(s2, null, finalAddress, null, null); //must be uncommented
}
if (!s3.equals("chooseContact3")) {
smsManager.sendTextMessage(s3, null, finalAddress, null, null);
}
if (!s4.equals("chooseContact4")) {
smsManager.sendTextMessage(s4, null, finalAddress, null, null);
}
if (!s5.equals("chooseContact5")) {
smsManager.sendTextMessage(s5, null, finalAddress, null, null);
}
//Geocoder geoCoder ;
// Toast.makeText(this,ans +"this is the address",Toast.LENGTH_SHORT).show();
return null;
}
我应该更改什么或将此代码添加到 Fixx?帮帮我伙计们 我在这个项目中使用 SDK 23
自从 API 23 以来,您还必须在运行时检查权限。用户可以随时撤销您的应用程序的权限,因此您必须确保在执行异步任务之前您仍然可以发送短信.
尝试在异步任务之前添加类似这样的内容:
if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED) {
// Permission is not granted
// You have to ask for permission again
} else {
// Granted, you can start you async task here
}
文档: https://developer.android.com/training/permissions/requesting.html