質問編集履歴

4

誤字

2017/04/23 05:59

投稿

edoooooo
edoooooo

スコア476

test CHANGED
@@ -1 +1 @@
1
- 指定の時間になったら、メソッドを実行したいです。
1
+ Androidで、指定の時間になったら、メソッドを実行したいです。(AlarmManager)
test CHANGED
File without changes

3

コードの追加

2017/04/23 05:59

投稿

edoooooo
edoooooo

スコア476

test CHANGED
@@ -1 +1 @@
1
- 24:00になったら、あるメソッドを実行したいです。
1
+ 指定の時間になったら、メソッドを実行したいです。
test CHANGED
@@ -69,3 +69,223 @@
69
69
  }
70
70
 
71
71
  ```
72
+
73
+
74
+
75
+ 実際にAlarmManagerを使えたコードです。<完成>
76
+
77
+ //MainActivity
78
+
79
+ ```java
80
+
81
+ package com.example.android.sample.easy;
82
+
83
+
84
+
85
+ import android.app.AlarmManager;
86
+
87
+ import android.app.PendingIntent;
88
+
89
+ import android.content.Intent;
90
+
91
+ import android.support.v7.app.AppCompatActivity;
92
+
93
+ import android.os.Bundle;
94
+
95
+ import android.util.Log;
96
+
97
+ import android.widget.Toast;
98
+
99
+
100
+
101
+
102
+
103
+
104
+
105
+ public class MainActivity extends AppCompatActivity {
106
+
107
+
108
+
109
+ private static final String TAG = MainActivity.class.getSimpleName();
110
+
111
+
112
+
113
+ @Override
114
+
115
+ protected void onCreate(Bundle savedInstanceState) {
116
+
117
+ super.onCreate(savedInstanceState);
118
+
119
+ setContentView(R.layout.activity_main);
120
+
121
+
122
+
123
+ addReset(14,53);
124
+
125
+ getPendingIntent();
126
+
127
+ }
128
+
129
+
130
+
131
+
132
+
133
+ public void addReset(int resetHour, int resetMinute) {
134
+
135
+
136
+
137
+ PendingIntent mResetSender = this.getPendingIntent();
138
+
139
+
140
+
141
+ // メソッドを呼ぶ時間の設定
142
+
143
+ java.util.Calendar cal = java.util.Calendar.getInstance();
144
+
145
+ cal.setTimeInMillis(System.currentTimeMillis());
146
+
147
+ // 設定した時刻をカレンダーに設定
148
+
149
+ cal.set(cal.HOUR_OF_DAY, resetHour);
150
+
151
+ cal.set(cal.MINUTE, resetMinute);
152
+
153
+ cal.set(cal.SECOND, 0);
154
+
155
+ cal.set(cal.MILLISECOND, 0);
156
+
157
+
158
+
159
+ Toast.makeText(this, String.format("%d時%d分にtoast表示のメソッドを呼びます", resetHour, resetMinute), Toast.LENGTH_LONG).show();
160
+
161
+
162
+
163
+ // AlramManager取得
164
+
165
+ AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
166
+
167
+ // AlarmManagerにPendingIntentを登録
168
+
169
+ am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), mResetSender);
170
+
171
+
172
+
173
+
174
+
175
+ Log.v(TAG, cal.getTimeInMillis() + "ms");
176
+
177
+ Log.v(TAG, "2:30に表示するメソッドを呼ぶ、セット完了");
178
+
179
+ }
180
+
181
+
182
+
183
+ private PendingIntent getPendingIntent() {
184
+
185
+ Intent intent = new Intent(this, MyResetService.class);
186
+
187
+ //
188
+
189
+ PendingIntent pendingIntent = PendingIntent.getService(this, PendingIntent.FLAG_ONE_SHOT, intent, PendingIntent.FLAG_UPDATE_CURRENT);
190
+
191
+ return pendingIntent;
192
+
193
+
194
+
195
+ }
196
+
197
+
198
+
199
+
200
+
201
+ }
202
+
203
+ ```
204
+
205
+ //MyResetService
206
+
207
+ ```
208
+
209
+ package com.example.android.sample.easy;
210
+
211
+
212
+
213
+ import android.app.Service;
214
+
215
+ import android.content.Intent;
216
+
217
+ import android.os.IBinder;
218
+
219
+ import android.util.Log;
220
+
221
+
222
+
223
+ public class MyResetService extends Service {
224
+
225
+ private static final String TAG = MyResetService.class.getSimpleName();
226
+
227
+
228
+
229
+
230
+
231
+ public MyResetService() {}
232
+
233
+
234
+
235
+ @Override
236
+
237
+ public IBinder onBind(Intent intent) {
238
+
239
+ return null;
240
+
241
+ }
242
+
243
+
244
+
245
+
246
+
247
+ @Override
248
+
249
+ public void onCreate() {
250
+
251
+ Thread thr = new Thread(null, mTask, "MyResetServiceThread");
252
+
253
+ thr.start();
254
+
255
+ Log.v(TAG,"スレッド開始");
256
+
257
+ }
258
+
259
+
260
+
261
+
262
+
263
+ // アラーム用サービス
264
+
265
+ Runnable mTask = new Runnable() {
266
+
267
+ public void run() {
268
+
269
+
270
+
271
+ Log.v(TAG,"あああああああああああ:これがLogに表示されれば");
272
+
273
+
274
+
275
+ // 役目を終えたサービスを止める
276
+
277
+ MyResetService.this.stopSelf();
278
+
279
+ Log.v(TAG,"サービス停止");
280
+
281
+ }
282
+
283
+ };
284
+
285
+
286
+
287
+
288
+
289
+ }
290
+
291
+ ```

2

誤字

2017/04/23 05:58

投稿

edoooooo
edoooooo

スコア476

test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,4 @@
1
- //⭐️この下の行をどのように変えればいいのでしょうか? Alarmでなく、ただ12時になったら、メソッドを呼ぶようにしたいです。。⭐️
1
+ この下の行をどのように変えればいいのでしょうか? Alarmでなく、ただ12時になったら、メソッドを呼ぶようにしたいです。
2
2
 
3
3
  am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), mResetSender);
4
4
 

1

誤字

2017/04/23 02:49

投稿

edoooooo
edoooooo

スコア476

test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,4 @@
1
- //⭐️この下の行をどのように変えればいいのでしょうか? Alarmでなく、ただ12時にデータベースとやりとりするメソッドを呼たいです。⭐️
1
+ //⭐️この下の行をどのように変えればいいのでしょうか? Alarmでなく、ただ12時になったら、メソッドを呼ぶようにしたいです。⭐️
2
2
 
3
3
  am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), mResetSender);
4
4
 
@@ -16,7 +16,7 @@
16
16
 
17
17
 
18
18
 
19
- // リセットする時間の設定
19
+ // リセットするメソッドを呼ぶ時間の設定
20
20
 
21
21
  Calendar cal = Calendar.getInstance();
22
22
 
@@ -34,11 +34,11 @@
34
34
 
35
35
 
36
36
 
37
- Toast.makeText(c, String.format("%12d時%00d分にデータをリセットます", resetHour, resetMinute), Toast.LENGTH_LONG).show();
37
+ Toast.makeText(c, String.format("%12d時%00d分にデータをリセットするメソッドを呼びます", resetHour, resetMinute), Toast.LENGTH_LONG).show();
38
38
 
39
39
 
40
40
 
41
- //⭐️この下の行をどのように変えればいいのでしょうか? Alarmでなく、ただ12時にデータベースとやりとりするメソッドを呼びたいです。⭐️
41
+ //⭐️この下の行をどのように変えればいいのでしょうか? Alarmでなく、ただ12時にデータをリセットするメソッドを呼びたいです。⭐️
42
42
 
43
43
  am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), mResetSender);
44
44
 
@@ -46,7 +46,7 @@
46
46
 
47
47
  Log.v(TAG, cal.getTimeInMillis()+"ms");
48
48
 
49
- Log.v(TAG, "12時にリセット、セット完了");
49
+ Log.v(TAG, "12時にリセットするメソッドを呼ぶ、セット完了");
50
50
 
51
51
  }
52
52
 
@@ -56,7 +56,7 @@
56
56
 
57
57
  private PendingIntent getPendingIntent() {
58
58
 
59
- // 12時に起動するアプリケーションを登録
59
+ // 12時に起動するアプリケーションを登録(リセットするメソッド)
60
60
 
61
61
  Intent intent = new Intent(c, MyResetService.class);
62
62