-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainActivity.cs
127 lines (116 loc) · 5.29 KB
/
MainActivity.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using System;
using System.Threading.Tasks;
using Android;
using Android.App;
using Android.App.Roles;
using Android.Content.PM;
using Android.OS;
using Android.Support.V7.App;
using Android.Support.V4.Content;
using Android.Widget;
using Android.Content;
using Android.Provider;
using Android.Runtime;
using Xamarin.Essentials;
namespace XamarinSMS
{
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Main);
var getPermissionBtn = FindViewById<Button>(Resource.Id.GetPermissionBtn);
var readSmsBtn = FindViewById<Button>(Resource.Id.ReadSmsBtn);
var setDefaultBtn = FindViewById<Button>(Resource.Id.SetDefaultBtn);
var readDeletedSmsBtn = FindViewById<Button>(Resource.Id.ReadDeletedSmsBtn);
var openUrlBtn = FindViewById<Button>(Resource.Id.OpenUrlBtn);
if (getPermissionBtn != null)
getPermissionBtn.Click += (sender, e) =>
{
if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.ReadSms) == (int)Permission.Granted &&
ContextCompat.CheckSelfPermission(this, Manifest.Permission.ReceiveSms) == (int)Permission.Granted &&
ContextCompat.CheckSelfPermission(this, Manifest.Permission.WriteSms) == (int)Permission.Granted)
{
Toast.MakeText(Application.Context, "Permission has been obtained", ToastLength.Short)?.Show();
}
else
{
Toast.MakeText(Application.Context, "Try to get permission.", ToastLength.Short)?.Show();
RequestPermissions(new[] { Manifest.Permission.ReadSms, Manifest.Permission.ReceiveSms, Manifest.Permission.WriteSms }, 0);
Toast.MakeText(Application.Context, "Permission obtained successfully", ToastLength.Short)?.Show();
}
};
if (readSmsBtn != null)
readSmsBtn.Click += (sender, e) =>
{
var intent = new Intent(this, typeof(MessagesListActivity));
intent.PutExtra("Type", "Inbox");
StartActivity(intent);
};
if (readDeletedSmsBtn != null)
readDeletedSmsBtn.Click += (sender, e) =>
{
var intent = new Intent(this, typeof(MessagesListActivity));
intent.PutExtra("Type", "Deleted");
StartActivity(intent);
};
if (setDefaultBtn != null)
setDefaultBtn.Click += (sender, e) =>
{
if (double.TryParse(Build.VERSION.Release, out var sdk) && sdk >= 10)
{
var roleManager = GetSystemService(RoleService) as RoleManager;
if (roleManager == null)
{
}
else if (roleManager.IsRoleAvailable(RoleManager.RoleSms))
{
int makeDefaultAppRequest = 1;
var intent = roleManager.CreateRequestRoleIntent(RoleManager.RoleSms);
StartActivityForResult(intent, makeDefaultAppRequest);
}
else
{
}
}
else
{
var setSmsAppIntent = new Intent(Telephony.Sms.Intents.ActionChangeDefault);
setSmsAppIntent.PutExtra(Telephony.Sms.Intents.ExtraPackageName,
Application.Context.PackageName);
StartActivity(setSmsAppIntent);
}
};
if (openUrlBtn != null)
openUrlBtn.Click += async (sender, args) =>
{
var uri = new Uri("https://www.baidu.com");
await Xamarin.Essentials.Browser.OpenAsync(uri, BrowserLaunchMode.External);
};
}
public async Task OpenBrowser(Uri uri)
{
await Xamarin.Essentials.Browser.OpenAsync(uri, BrowserLaunchMode.SystemPreferred);
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
{
if (requestCode == 0)
{
if (grantResults.Length == 1 && grantResults[0] == Permission.Granted)
Toast.MakeText(Application.Context, $"{permissions[0]} Permission has been granted.", ToastLength.Short)?.Show();
else
Toast.MakeText(Application.Context, $"Failed to obtain {permissions[0]} permission.", ToastLength.Short)?.Show();
}
else
{
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
protected override void OnResume()
{
base.OnResume();
}
}
}