質問編集履歴
1
ソースを追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -5,3 +5,169 @@
|
|
5
5
|
画面を立ち上げる機能はkotlinにありますか?
|
6
6
|
|
7
7
|
javaでなければ実装できないでしょうか?
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
### ソース
|
12
|
+
|
13
|
+
```MainActivty
|
14
|
+
|
15
|
+
package com.example.myapplication
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
import android.app.AlarmManager
|
20
|
+
|
21
|
+
import android.app.PendingIntent
|
22
|
+
|
23
|
+
import android.content.BroadcastReceiver
|
24
|
+
|
25
|
+
import android.content.Context
|
26
|
+
|
27
|
+
import android.content.Intent
|
28
|
+
|
29
|
+
import android.os.Bundle
|
30
|
+
|
31
|
+
import android.util.Log
|
32
|
+
|
33
|
+
import android.widget.Button
|
34
|
+
|
35
|
+
import androidx.appcompat.app.AppCompatActivity
|
36
|
+
|
37
|
+
import java.util.*
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
class MainActivity : AppCompatActivity() {
|
44
|
+
|
45
|
+
override fun onCreate(savedInstanceState: Bundle?) {
|
46
|
+
|
47
|
+
super.onCreate(savedInstanceState)
|
48
|
+
|
49
|
+
setContentView(R.layout.activity_main)
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
val btn: Button = findViewById(R.id.btn)
|
54
|
+
|
55
|
+
btn.text = "before"
|
56
|
+
|
57
|
+
btn.setOnClickListener {
|
58
|
+
|
59
|
+
// 時間をセットする
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
val calendar: Calendar = Calendar.getInstance()
|
64
|
+
|
65
|
+
// Calendarを使って現在の時間をミリ秒で取得
|
66
|
+
|
67
|
+
calendar.setTimeInMillis(System.currentTimeMillis())
|
68
|
+
|
69
|
+
// 5秒後に設定
|
70
|
+
|
71
|
+
calendar.add(Calendar.SECOND, 5)
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
val intent = Intent(
|
76
|
+
|
77
|
+
applicationContext,
|
78
|
+
|
79
|
+
AlarmBroadcastReceiver::class.java
|
80
|
+
|
81
|
+
)
|
82
|
+
|
83
|
+
val pending = PendingIntent.getBroadcast(
|
84
|
+
|
85
|
+
applicationContext, 0, intent, 0
|
86
|
+
|
87
|
+
)
|
88
|
+
|
89
|
+
// アラームをセットする
|
90
|
+
|
91
|
+
val am = getSystemService(ALARM_SERVICE) as AlarmManager
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
am.setExact(AlarmManager.RTC_WAKEUP, calendar.timeInMillis, pending)
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
}
|
102
|
+
|
103
|
+
}
|
104
|
+
|
105
|
+
}
|
106
|
+
|
107
|
+
class AlarmBroadcastReceiver : BroadcastReceiver() {
|
108
|
+
|
109
|
+
override fun onReceive(context: Context?, intent: Intent?) {
|
110
|
+
|
111
|
+
val intent = Intent(context, MainActivity::class.java)
|
112
|
+
|
113
|
+
intent.action = "received"
|
114
|
+
|
115
|
+
val pIntent = PendingIntent.getActivity(context, 0, intent, 0)
|
116
|
+
|
117
|
+
pIntent.send()
|
118
|
+
|
119
|
+
}
|
120
|
+
|
121
|
+
}
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
```
|
126
|
+
|
127
|
+
```AndroidManifest
|
128
|
+
|
129
|
+
<?xml version="1.0" encoding="utf-8"?>
|
130
|
+
|
131
|
+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
132
|
+
|
133
|
+
package="com.example.myapplication">
|
134
|
+
|
135
|
+
<uses-permission android:name="android.permission.WAKE_LOCK"/>
|
136
|
+
|
137
|
+
<application
|
138
|
+
|
139
|
+
android:allowBackup="true"
|
140
|
+
|
141
|
+
android:icon="@mipmap/ic_launcher"
|
142
|
+
|
143
|
+
android:label="@string/app_name"
|
144
|
+
|
145
|
+
android:roundIcon="@mipmap/ic_launcher_round"
|
146
|
+
|
147
|
+
android:supportsRtl="true"
|
148
|
+
|
149
|
+
android:theme="@style/Theme.MyApplication">
|
150
|
+
|
151
|
+
<activity android:name=".MainActivity">
|
152
|
+
|
153
|
+
<intent-filter>
|
154
|
+
|
155
|
+
<action android:name="android.intent.action.MAIN" />
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
<category android:name="android.intent.category.LAUNCHER" />
|
160
|
+
|
161
|
+
</intent-filter>
|
162
|
+
|
163
|
+
</activity>
|
164
|
+
|
165
|
+
<receiver android:name=".AlarmBroadcastReceiver"/>
|
166
|
+
|
167
|
+
</application>
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
</manifest>
|
172
|
+
|
173
|
+
```
|