-
Notifications
You must be signed in to change notification settings - Fork 252
/
RuntimePermissions.cs
111 lines (92 loc) · 3.54 KB
/
RuntimePermissions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// add send sms in maifest
namespace AndroidPermissions
{
[Activity(Label = "AndroidPermissions", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
// Get our button from the layout resource,
// and attach an event to it
Button button = FindViewById<Button>(Resource.Id.MyButton);
button.Click += async delegate {
await TryGetSendSMSAsync() ;
};
}
async Task TryGetSendSMSAsync()
{
if ((int)Build.VERSION.SdkInt < 23)
{
await SMSSend();
return;
}
await GetSendSMSPermissionAsync();
}
async Task GetSendSMSPermissionAsync()
{
//Check to see if any permission in our group is available, if one, then all are
const string permission = Manifest.Permission.SendSms;
if (CheckSelfPermission(Manifest.Permission.SendSms) == (int)Permission.Granted)
{
await SMSSend();
return;
}
//need to request permission
if (ShouldShowRequestPermissionRationale(permission))
{
var callDialog = new AlertDialog.Builder(this);
callDialog.SetTitle("explain");
callDialog.SetMessage("This app needt to send sms so it need sms send permission");
callDialog.SetNeutralButton("yes", delegate {
RequestPermissions(PermissionsLocation, RequestLocationId);
});
callDialog.SetNegativeButton("no", delegate { });
callDialog.Show();
return;
}
//Finally request permissions with the list of permissions and Id
RequestPermissions(PermissionsLocation, RequestLocationId);
}
readonly string[] PermissionsLocation =
{
Manifest.Permission.SendSms //, more
};
const int RequestLocationId = 0;
public override async void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
{
switch (requestCode)
{
case RequestLocationId:
{
if (grantResults[0] == Permission.Granted)
{
//Permission granted
Toast.MakeText(this, "sms send permission is available", ToastLength.Long).Show();
await SMSSend();
}
else
{
//Permission Denied :(
//Disabling location functionality
Toast.MakeText(this, "sms send permission is denailed", ToastLength.Long).Show();
;
}
}
break;
}
}
async Task SMSSend()
{
try
{
SmsManager.Default.SendTextMessage("1234567890", null, "Hello from Xamarin.Android", null, null);
}
catch (Exception ex)
{
}
}
}
}