没有错误,没有警告,但是当我点击发送按钮时,它将返回错误异常。请帮我解决这个问题,这是我第一次构建一个Android消息传递应用程序。
未处理的异常:
Java.Lang.SecurityException:超时超出获取异常详细信息
MainActivity.cs
using Android.App;
using Android.OS;
using Android.Support.V7.App;
using Android.Runtime;
using Android.Widget;
using Android.Content.PM;
using Android.Telephony;
using System;
using Android;
using Android.Content;
namespace MhylesOrderingApp
{
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
private SmsManager _smsManager;
private BroadcastReceiver _smsSentBroadcastReceiver, _smsDeliveredBroadcastReceiver;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
var smsBtn = FindViewById<Button>(Resource.Id.btnSend);
var phoneNum = FindViewById<EditText>(Resource.Id.phoneNum);
var sms = FindViewById<EditText>(Resource.Id.txtSMS);
_smsManager = SmsManager.Default;
smsBtn.Click += (s, e) =>
{
var phone = phoneNum.Text;
var message = sms.Text;
var piSent = PendingIntent.GetBroadcast(this, 0, new Intent("SMS_SENT"), 0);
var piDelivered = PendingIntent.GetBroadcast(this, 0, new Intent("SMS_DELIVERED"), 0);
_smsManager.SendTextMessage(phone, null, message, piSent, piDelivered);
};
}
protected override void OnResume()
{
base.OnResume();
_smsSentBroadcastReceiver = new SMSSentReceiver();
_smsDeliveredBroadcastReceiver = new SMSDeliveredReceiver();
RegisterReceiver(_smsSentBroadcastReceiver, new IntentFilter("SMS_SENT"));
RegisterReceiver(_smsDeliveredBroadcastReceiver, new IntentFilter("SMS_DELIVERED"));
}
protected override void OnPause()
{
base.OnPause();
UnregisterReceiver(_smsSentBroadcastReceiver);
UnregisterReceiver(_smsDeliveredBroadcastReceiver);
}
}
[BroadcastReceiver]
public class SMSSentReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
switch ((int)ResultCode)
{
case (int)Result.Ok:
Toast.MakeText(Application.Context, "SMS has been sent", ToastLength.Short).Show();
break;
case (int)SmsResultError.GenericFailure:
Toast.MakeText(Application.Context, "Generic Failure", ToastLength.Short).Show();
break;
case (int)SmsResultError.NoService:
Toast.MakeText(Application.Context, "No Service", ToastLength.Short).Show();
break;
case (int)SmsResultError.NullPdu:
Toast.MakeText(Application.Context, "Null PDU", ToastLength.Short).Show();
break;
case (int)SmsResultError.RadioOff:
Toast.MakeText(Application.Context, "Radio Off", ToastLength.Short).Show();
break;
}
}
}
[BroadcastReceiver]
public class SMSDeliveredReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
switch ((int)ResultCode)
{
case (int)Result.Ok:
Toast.MakeText(Application.Context, "SMS Delivered", ToastLength.Short).Show();
break;
case (int)Result.Canceled:
Toast.MakeText(Application.Context, "SMS not delivered", ToastLength.Short).Show();
break;
}
}
}
}
AndroidManifest.xml中
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="1"
android:versionName="1.0"
package="MhylesOrderingApp.MhylesOrderingApp"
android:installLocation="auto">
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="27" />
<uses-permission android:name="android.permission.SEND_SMS" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
</application>
</manifest>
activity_main.axml
<EditText
android:id="@+id/phoneNum"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<EditText
android:id="@+id/txtSMS"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/btnSend"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send" />
我使用你的代码重现了你的错误。并通过以下两个步骤解决了这个问题:
1将Xamarin.Essentials
NuGet包添加到每个项目,如下所示:
在Solution Explorer面板中,右键单击解决方案名称,然后选择Manage NuGet Packages。搜索Xamarin.Essentials并将软件包安装到您的项目中。
2在Android 6.0版本之后使用RuntimePermissions for SendSMS:
1)修改按钮点击事件:
smsBtn.Click += (s, e) =>
{
var phone = phoneNum.Text;
var message = sms.Text;
var piSent = PendingIntent.GetBroadcast(this, 0, new Intent("SMS_SENT"), 0);
var piDelivered = PendingIntent.GetBroadcast(this, 0, new Intent("SMS_DELIVERED"), 0);
if ((int)Build.VERSION.SdkInt < 23)
{
_smsManager.SendTextMessage(phone, null, message, piSent, piDelivered);
return;
}
else {
if (ActivityCompat.CheckSelfPermission(this, Manifest.Permission.SendSms) != (int)Permission.Granted)
{
// Permission is not granted. If necessary display rationale & request.
RequestSendSMSPermission();
}
else
{
// We have permission, go ahead and send SMS.
_smsManager.SendTextMessage(phone, null, message, piSent, piDelivered);
}
}
};
2)在MainActivity类中添加以下函数:
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
void RequestSendSMSPermission()
{
Log.Info("MainActivity", "CAMERA permission has NOT been granted. Requesting permission.");
if (ActivityCompat.ShouldShowRequestPermissionRationale(this, Manifest.Permission.SendSms))
{
Log.Info("MainActivity", "Displaying camera permission rationale to provide additional context.");
Snackbar.Make(layout, "Camera permission is needed to show the camera preview.",
Snackbar.LengthIndefinite).SetAction("OK", new Action<View>(delegate (View obj) {
ActivityCompat.RequestPermissions(this, new String[] { Manifest.Permission.SendSms }, REQUEST_SENDSMS);
})).Show();
}
else
{
ActivityCompat.RequestPermissions(this, new String[] { Manifest.Permission.SendSms }, REQUEST_SENDSMS);
}
}
3)设置主布局根的id属性:
android:id="@+id/sample_main_layout"
我已经向github发布了一个演示。