没有错误,没有警告,但是当我单击发送按钮时,它将返回一个错误异常。请帮助我解决这个问题,这是我第一次构建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" />
答案 0 :(得分:0)
我通过使用您的代码重现了您的错误。并通过以下两个步骤解决了该问题:
1如下所示将SELECT SUM(CASE WHEN column1 IS NULL THEN 1 ELSE 0),
SUM(CASE WHEN column2 IS NULL THEN 1 ELSE 0),
SUM(CASE WHEN column2 IS NULL THEN 1 ELSE 0),
....
FROM Table
NuGet包添加到每个项目:
在“解决方案资源管理器”面板中,右键单击解决方案名称,然后选择“管理NuGet软件包”。搜索Xamarin.Essentials,然后将该软件包安装到您的项目中。
2在Android 6.0版本之后对SendSMS使用RuntimePermissions:
1)修改按钮单击的事件:
Xamarin.Essentials
2)在MainActivity类中添加以下函数:
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);
}
}
};
3)设置主布局的根的id属性:
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);
}
}
我已向github发布了一个演示。