我编写了一个程序来发送短信,该短信在执行时会崩溃
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnsend = (Button) findViewById(R.id.btnsend);
final EditText edtnum = (EditText) findViewById(R.id.edtnum);
final EditText edttext = (EditText) findViewById(R.id.edttext);
btnsend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String phoneNo=edtnum.getText().toString();
String message=edttext.getText().toString();
SmsManager manager=SmsManager.getDefault();
manager.sendTextMessage(phoneNo,null,message,null,null);
}
});
}
}
应用程序在按下按钮时崩溃并给出此错误
2019-09-09 10:30:33.384 2104-2104 / com.example.video60 E / AndroidRuntime:致命异常:main 流程:com.example.video60,PID:2104 java.lang.SecurityException:正在发送短信:uid 10086没有android.permission.SEND_SMS。 在android.os.Parcel.readException(Parcel.java:1942) 在android.os.Parcel.readException(Parcel.java:1888) 在com.android.internal.telephony.ISms $ Stub $ Proxy.sendTextForSubscriber(ISms.java:789) 在android.telephony.SmsManager.sendTextMessageInternal(SmsManager.java:329) 在android.telephony.SmsManager.sendTextMessage(SmsManager.java:312) 在com.example.video60.MainActivity $ 1.onClick(MainActivity.java:41) 在android.view.View.performClick(View.java:6256) 在android.view.View $ PerformClick.run(View.java:24697) 在android.os.Handler.handleCallback(Handler.java:789) 在android.os.Handler.dispatchMessage(Handler.java:98) 在android.os.Looper.loop(Looper.java:164) 在android.app.ActivityThread.main(ActivityThread.java:6541) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
答案 0 :(得分:1)
在您的android清单中,声明以下内容:
<uses-permission android:name="android.permission.SEND_SMS" />
因为您的异常告诉您:
发送SMS消息:uid 10086没有 android.permission.SEND_SMS。
您的应用无权发送短信
有关其他信息,请查看此:https://developer.android.com/guide/topics/permissions/overview
我的答案适用于api 22及以下版本,对于更高的权限,您必须请求权限,您可以在这里找到:https://developer.android.com/training/permissions/requesting
类似这样的东西:
if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED) {
// Permission is not granted
}
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED) {
// Permission is not 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; request the permission
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.SEND_SMS},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
} else {
// Permission has already been granted
}