Skip to content

Commit 75423c9

Browse files
committed
update android functions example
1 parent ef5aaa6 commit 75423c9

1 file changed

Lines changed: 66 additions & 8 deletions

File tree

examples/android.md

Lines changed: 66 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,37 +20,95 @@ Android platform features could be used with [PyJNIus](https://github.com/kivy/p
2020
%connect-there
2121
```
2222

23-
## Send SMS
23+
```python
24+
%%there
25+
from kivy.logger import Logger
26+
from android.permissions import Permission, check_permission, request_permission
27+
import plyer
28+
```
29+
30+
## Show notification
31+
32+
```python
33+
%%there
34+
plyer.notification.notify(title='Python', message='Here')
35+
```
36+
37+
## Read accelerometer value
38+
39+
```python
40+
%%there
41+
print(plyer.accelerometer.acceleration)
42+
plyer.accelerometer.enable()
43+
```
44+
45+
```python
46+
%%there --delay 1
47+
print("x: {}, y: {}, z: {}".format(*plyer.accelerometer.acceleration))
48+
```
49+
50+
```python
51+
%%there
52+
plyer.accelerometer.disable()
53+
print(plyer.accelerometer.acceleration)
54+
```
55+
56+
## Write to external storage
57+
Writing to eternal storage is a restricted action.
58+
If *WRITE_EXTERNAL_STORAGE* permission was not granted to application, access to "/sdcard" will raise *PermissionError*:
59+
60+
```python
61+
%%there
62+
def write_to_sdcard():
63+
with open("/sdcard/test_python_here.txt", "w") as f:
64+
f.write("Python everywhere!")
65+
66+
write_to_sdcard()
67+
```
2468

69+
```python
70+
%%there shell
71+
touch /sdcard/test_python_here
72+
```
2573

26-
### Request SEND_SMS runtime permission
74+
### Request runtime permission
2775

2876
```python
2977
%there -bl 1 log
3078
```
3179

3280
```python
3381
%%there
34-
from kivy.logger import Logger
35-
from android.permissions import Permission, check_permission, request_permission
3682

3783
def permissions_callback(permissions, grant_results):
3884
if permissions and all(grant_results):
3985
Logger.info("Runtime permissions: granted")
4086
else:
4187
Logger.error("Runtime permissions: not granted")
4288

43-
permission = Permission.SEND_SMS
89+
permission = Permission.WRITE_EXTERNAL_STORAGE
4490
if check_permission(permission):
4591
print(f"{permission} is already granted")
4692
else:
4793
request_permission(permission, callback=permissions_callback)
4894
```
4995

50-
### Send message with Plyer
96+
The system permission prompt should appear at this point.
97+
98+
99+
### Use external storage
100+
101+
102+
After WRITE_EXTERNAL_STORAGE permission was granted:
51103

52104
```python
53105
%%there
54-
import plyer
55-
plyer.sms.send(recipient=" ", message="Hey!")
106+
write_to_sdcard()
107+
```
108+
109+
```python
110+
%%there shell
111+
touch /sdcard/test_python_here.txt
112+
cat /sdcard/test_python_here.txt
113+
rm /sdcard/test_python_here.txt
56114
```

0 commit comments

Comments
 (0)