質問編集履歴
3
hennsilyu
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,404 +1,12 @@
|
|
1
1
|
### 前提・実現したいこと
|
2
2
|
|
3
|
-
セットしたアラームの時刻がきたら、画面遷移(レイアウトだとactivity_main.xmlからactivity_play_sound.xml、処理Activityは、MainActivityからPlaySoundActivityにしたいが、全く時刻通りになっても画面遷移する処理してくれません。原因がわかる方教えてください。
|
4
3
|
|
5
|
-
GitHubにもあるので見てくれると助かります。https://github.com/kimukou1205/ShakeAlarm
|
6
4
|
|
7
5
|
|
8
6
|
|
9
7
|
### 該当のソースコード
|
10
8
|
|
11
9
|
```ここに言語を入力
|
12
|
-
|
13
|
-
package com.example.axu1.richarddawkinsalarmclock;
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
import android.annotation.SuppressLint;
|
18
|
-
|
19
|
-
import android.annotation.TargetApi;
|
20
|
-
|
21
|
-
import android.app.AlarmManager;
|
22
|
-
|
23
|
-
import android.app.PendingIntent;
|
24
|
-
|
25
|
-
import android.content.Context;
|
26
|
-
|
27
|
-
import android.content.Intent;
|
28
|
-
|
29
|
-
import android.os.Build;
|
30
|
-
|
31
|
-
import android.os.Bundle;
|
32
|
-
|
33
|
-
import android.support.v7.app.AppCompatActivity;
|
34
|
-
|
35
|
-
import android.util.Log;
|
36
|
-
|
37
|
-
import android.view.View;
|
38
|
-
|
39
|
-
import android.widget.Button;
|
40
|
-
|
41
|
-
import android.widget.TextView;
|
42
|
-
|
43
|
-
import android.widget.TimePicker;
|
44
|
-
|
45
|
-
import android.widget.Toast;
|
46
|
-
|
47
|
-
import java.util.Calendar;
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
import com.example.axu1.richarddawkinsalarmclock.util.PreferenceUtil;
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
import java.util.Random;
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
public class MainActivity extends AppCompatActivity implements AccelerometerListener {
|
60
|
-
|
61
|
-
public static final String ALARM_TIME = "alarm_time";
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
AlarmManager alarmManager;
|
66
|
-
|
67
|
-
private PendingIntent pending_intent;
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
private TimePicker alarmTimePicker;
|
72
|
-
|
73
|
-
private TextView alarmTextView;
|
74
|
-
|
75
|
-
private TextView countTextView;
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
private AlarmReceiver alarm;
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
private Integer shakeCount;
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
MainActivity inst;
|
90
|
-
|
91
|
-
Context context;
|
92
|
-
|
93
|
-
PreferenceUtil pref;
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
@Override
|
98
|
-
|
99
|
-
protected void onResume() {
|
100
|
-
|
101
|
-
super.onResume();
|
102
|
-
|
103
|
-
if (AccelerometerManager.isSupported(this)) {
|
104
|
-
|
105
|
-
AccelerometerManager.startListening(this);
|
106
|
-
|
107
|
-
}
|
108
|
-
|
109
|
-
}
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
@Override
|
114
|
-
|
115
|
-
public void onAccelerationChanged(float x, float y, float z) {
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
}
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
@Override
|
124
|
-
|
125
|
-
public void onShake(float force) {
|
126
|
-
|
127
|
-
//stopAlarm();
|
128
|
-
|
129
|
-
shakeCount++;
|
130
|
-
|
131
|
-
//countTextView = (TextView) findViewById(R.id.txtShakeCount);
|
132
|
-
|
133
|
-
Toast.makeText(this, shakeCount.toString() + "回", Toast.LENGTH_SHORT).show();
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
if (shakeCount >= 10) {
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
//------------------
|
144
|
-
|
145
|
-
this.context = this;
|
146
|
-
|
147
|
-
final Intent myIntent = new Intent(this.context, AlarmReceiver.class);
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
int min = 1;
|
152
|
-
|
153
|
-
int max = 9;
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
Random r = new Random();
|
158
|
-
|
159
|
-
int random_number = r.nextInt(max - min + 1) + min;
|
160
|
-
|
161
|
-
Log.e("random number is ", String.valueOf(random_number));
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
myIntent.putExtra("extra", "no");
|
166
|
-
|
167
|
-
sendBroadcast(myIntent);
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
alarmManager.cancel(pending_intent);
|
172
|
-
|
173
|
-
setAlarmText("アラーム終了しました");
|
174
|
-
|
175
|
-
//setAlarmText("You clicked a " + " canceled");
|
176
|
-
|
177
|
-
shakeCount = 0;
|
178
|
-
|
179
|
-
}
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
//countTextView.setText(shakeCount.toString());
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
//------------------
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
}
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
@Override
|
200
|
-
|
201
|
-
public void onStop() {
|
202
|
-
|
203
|
-
super.onStop();
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
//Check device supported Accelerometer senssor or not
|
208
|
-
|
209
|
-
if (AccelerometerManager.isListening()) {
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
//Start Accelerometer Listening
|
214
|
-
|
215
|
-
AccelerometerManager.stopListening();
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
//Toast.makeText(this, "onStop Accelerometer Stopped", Toast.LENGTH_SHORT).show();
|
220
|
-
|
221
|
-
}
|
222
|
-
|
223
|
-
}
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
@Override
|
230
|
-
|
231
|
-
protected void onCreate(Bundle savedInstanceState) {
|
232
|
-
|
233
|
-
super.onCreate(savedInstanceState);
|
234
|
-
|
235
|
-
setContentView(R.layout.activity_main);
|
236
|
-
|
237
|
-
Button sendButton = (Button) findViewById(R.id.nextButton);
|
238
|
-
|
239
|
-
sendButton.setOnClickListener(new View.OnClickListener() {
|
240
|
-
|
241
|
-
@Override
|
242
|
-
|
243
|
-
public void onClick(View v) {
|
244
|
-
|
245
|
-
Intent intent = new Intent(getApplication(), SubActivity.class);
|
246
|
-
|
247
|
-
startActivity(intent);
|
248
|
-
|
249
|
-
}
|
250
|
-
|
251
|
-
});
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
this.context = this;
|
256
|
-
|
257
|
-
final Intent myIntent = new Intent(this.context, AlarmReceiver.class);
|
258
|
-
|
259
|
-
shakeCount = 0;
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
//alarm = new AlarmReceiver();
|
266
|
-
|
267
|
-
alarmTextView = (TextView) findViewById(R.id.alarmText);
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
// Get the alarm manager service
|
274
|
-
|
275
|
-
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
// set the alarm to the time that you picked
|
280
|
-
|
281
|
-
final Calendar calendar = Calendar.getInstance();
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
alarmTimePicker = (TimePicker) findViewById(R.id.alarmTimePicker);
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
Button start_alarm = (Button) findViewById(R.id.start_alarm);
|
292
|
-
|
293
|
-
start_alarm.setOnClickListener(new View.OnClickListener() {
|
294
|
-
|
295
|
-
@TargetApi(Build.VERSION_CODES.M)
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
@Override
|
300
|
-
|
301
|
-
public void onClick(View v) {
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
calendar.add(Calendar.SECOND, 3);
|
306
|
-
|
307
|
-
//setAlarmText("You clicked a button");
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
final int hour = alarmTimePicker.getCurrentHour();
|
312
|
-
|
313
|
-
final int minute = alarmTimePicker.getCurrentMinute();
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
Log.e("MyActivity", "In the receiver with " + hour + " and " + minute);
|
318
|
-
|
319
|
-
setAlarmText("You clicked a " + hour + " and " + minute);
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getCurrentHour());
|
326
|
-
|
327
|
-
calendar.set(Calendar.MINUTE, alarmTimePicker.getCurrentMinute());
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
myIntent.putExtra("extra", "yes");
|
332
|
-
|
333
|
-
pending_intent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pending_intent);
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
// now you should change the set Alarm text so it says something nice
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
setAlarmText(hour + ":" + minute + "のアラームをセットしました");
|
350
|
-
|
351
|
-
//Toast.makeText(getApplicationContext(), "You set the alarm", Toast.LENGTH_SHORT).show();
|
352
|
-
|
353
|
-
}
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
});
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
Button stop_alarm = (Button) findViewById(R.id.stop_alarm);
|
364
|
-
|
365
|
-
stop_alarm.setOnClickListener(new View.OnClickListener() {
|
366
|
-
|
367
|
-
@Override
|
368
|
-
|
369
|
-
public void onClick(View v) {
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
int min = 1;
|
374
|
-
|
375
|
-
int max = 9;
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
Random r = new Random();
|
380
|
-
|
381
|
-
int random_number = r.nextInt(max - min + 1) + min;
|
382
|
-
|
383
|
-
Log.e("random number is ", String.valueOf(random_number));
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
myIntent.putExtra("extra", "no");
|
388
|
-
|
389
|
-
sendBroadcast(myIntent);
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
alarmManager.cancel(pending_intent);
|
394
|
-
|
395
|
-
setAlarmText("アラーム終了");
|
396
|
-
|
397
|
-
//setAlarmText("You clicked a " + " canceled");
|
398
|
-
|
399
|
-
}
|
400
|
-
|
401
|
-
});
|
402
10
|
|
403
11
|
|
404
12
|
|
@@ -476,51 +84,7 @@
|
|
476
84
|
|
477
85
|
alarmManager.setAlarmClock(new AlarmManager.AlarmClockInfo(alarmTimeMillis, null), pendingIntent);
|
478
86
|
|
479
|
-
|
87
|
+
|
480
|
-
|
481
|
-
alarmManager.setExact(AlarmManager.RTC_WAKEUP, alarmTimeMillis, pendingIntent);
|
482
|
-
|
483
|
-
} else {
|
484
|
-
|
485
|
-
alarmManager.set(AlarmManager.RTC_WAKEUP, alarmTimeMillis, pendingIntent);
|
486
|
-
|
487
|
-
}
|
488
|
-
|
489
|
-
// 保存
|
490
|
-
|
491
|
-
pref.setLong(ALARM_TIME, alarmTimeMillis);
|
492
|
-
|
493
|
-
}
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
// 解除
|
498
|
-
|
499
|
-
private void unregister() {
|
500
|
-
|
501
|
-
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
|
502
|
-
|
503
|
-
alarmManager.cancel(getPendingIntent());
|
504
|
-
|
505
|
-
pref.delete(ALARM_TIME);
|
506
|
-
|
507
|
-
}
|
508
|
-
|
509
|
-
private PendingIntent getPendingIntent() {
|
510
|
-
|
511
|
-
Intent intent = new Intent(this, com.example.axu1.richarddawkinsalarmclock.AlarmCheckActivity.class);
|
512
|
-
|
513
|
-
intent.setClass(this, com.example.axu1.richarddawkinsalarmclock.AlarmCheckActivity.class);
|
514
|
-
|
515
|
-
return PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
|
516
|
-
|
517
|
-
}
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
}
|
522
|
-
|
523
|
-
|
524
88
|
|
525
89
|
```
|
526
90
|
|
2
修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
### 前提・実現したいこと
|
2
2
|
|
3
|
-
セットしたアラームの時刻がきたら、画面遷移(レイアウトだとactivity_min.xmlからactivity_play_sound.xml、処理Activityは、MainActivityからPlaySoundActivity
|
3
|
+
セットしたアラームの時刻がきたら、画面遷移(レイアウトだとactivity_main.xmlからactivity_play_sound.xml、処理Activityは、MainActivityからPlaySoundActivityにしたいが、全く時刻通りになっても画面遷移する処理してくれません。原因がわかる方教えてください。
|
4
4
|
|
5
5
|
GitHubにもあるので見てくれると助かります。https://github.com/kimukou1205/ShakeAlarm
|
6
6
|
|
1
修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
### 前提・実現したいこと
|
2
2
|
|
3
|
-
セットしたアラームの時刻がきたら、画面遷移(レイアウトだとactivity_play_sound、処理ActivityはPlaySoundActivity)にしたいが、全く時刻通りになっても画面遷移する処理してくれません。原因がわかる方教えてください。
|
3
|
+
セットしたアラームの時刻がきたら、画面遷移(レイアウトだとactivity_min.xmlからactivity_play_sound.xml、処理Activityは、MainActivityからPlaySoundActivity)にしたいが、全く時刻通りになっても画面遷移する処理してくれません。原因がわかる方教えてください。
|
4
4
|
|
5
5
|
GitHubにもあるので見てくれると助かります。https://github.com/kimukou1205/ShakeAlarm
|
6
6
|
|