質問編集履歴
4
誤字
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
指定の時間になったら、メソッドを実行したいです。
|
1
|
+
Androidで、指定の時間になったら、メソッドを実行したいです。(AlarmManager)
|
body
CHANGED
File without changes
|
3
コードの追加
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
|
1
|
+
指定の時間になったら、メソッドを実行したいです。
|
body
CHANGED
@@ -33,4 +33,114 @@
|
|
33
33
|
return pendingIntent;
|
34
34
|
}
|
35
35
|
}
|
36
|
+
```
|
37
|
+
|
38
|
+
実際にAlarmManagerを使えたコードです。<完成>
|
39
|
+
//MainActivity
|
40
|
+
```java
|
41
|
+
package com.example.android.sample.easy;
|
42
|
+
|
43
|
+
import android.app.AlarmManager;
|
44
|
+
import android.app.PendingIntent;
|
45
|
+
import android.content.Intent;
|
46
|
+
import android.support.v7.app.AppCompatActivity;
|
47
|
+
import android.os.Bundle;
|
48
|
+
import android.util.Log;
|
49
|
+
import android.widget.Toast;
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
public class MainActivity extends AppCompatActivity {
|
54
|
+
|
55
|
+
private static final String TAG = MainActivity.class.getSimpleName();
|
56
|
+
|
57
|
+
@Override
|
58
|
+
protected void onCreate(Bundle savedInstanceState) {
|
59
|
+
super.onCreate(savedInstanceState);
|
60
|
+
setContentView(R.layout.activity_main);
|
61
|
+
|
62
|
+
addReset(14,53);
|
63
|
+
getPendingIntent();
|
64
|
+
}
|
65
|
+
|
66
|
+
|
67
|
+
public void addReset(int resetHour, int resetMinute) {
|
68
|
+
|
69
|
+
PendingIntent mResetSender = this.getPendingIntent();
|
70
|
+
|
71
|
+
// メソッドを呼ぶ時間の設定
|
72
|
+
java.util.Calendar cal = java.util.Calendar.getInstance();
|
73
|
+
cal.setTimeInMillis(System.currentTimeMillis());
|
74
|
+
// 設定した時刻をカレンダーに設定
|
75
|
+
cal.set(cal.HOUR_OF_DAY, resetHour);
|
76
|
+
cal.set(cal.MINUTE, resetMinute);
|
77
|
+
cal.set(cal.SECOND, 0);
|
78
|
+
cal.set(cal.MILLISECOND, 0);
|
79
|
+
|
80
|
+
Toast.makeText(this, String.format("%d時%d分にtoast表示のメソッドを呼びます", resetHour, resetMinute), Toast.LENGTH_LONG).show();
|
81
|
+
|
82
|
+
// AlramManager取得
|
83
|
+
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
|
84
|
+
// AlarmManagerにPendingIntentを登録
|
85
|
+
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), mResetSender);
|
86
|
+
|
87
|
+
|
88
|
+
Log.v(TAG, cal.getTimeInMillis() + "ms");
|
89
|
+
Log.v(TAG, "2:30に表示するメソッドを呼ぶ、セット完了");
|
90
|
+
}
|
91
|
+
|
92
|
+
private PendingIntent getPendingIntent() {
|
93
|
+
Intent intent = new Intent(this, MyResetService.class);
|
94
|
+
//
|
95
|
+
PendingIntent pendingIntent = PendingIntent.getService(this, PendingIntent.FLAG_ONE_SHOT, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
96
|
+
return pendingIntent;
|
97
|
+
|
98
|
+
}
|
99
|
+
|
100
|
+
|
101
|
+
}
|
102
|
+
```
|
103
|
+
//MyResetService
|
104
|
+
```
|
105
|
+
package com.example.android.sample.easy;
|
106
|
+
|
107
|
+
import android.app.Service;
|
108
|
+
import android.content.Intent;
|
109
|
+
import android.os.IBinder;
|
110
|
+
import android.util.Log;
|
111
|
+
|
112
|
+
public class MyResetService extends Service {
|
113
|
+
private static final String TAG = MyResetService.class.getSimpleName();
|
114
|
+
|
115
|
+
|
116
|
+
public MyResetService() {}
|
117
|
+
|
118
|
+
@Override
|
119
|
+
public IBinder onBind(Intent intent) {
|
120
|
+
return null;
|
121
|
+
}
|
122
|
+
|
123
|
+
|
124
|
+
@Override
|
125
|
+
public void onCreate() {
|
126
|
+
Thread thr = new Thread(null, mTask, "MyResetServiceThread");
|
127
|
+
thr.start();
|
128
|
+
Log.v(TAG,"スレッド開始");
|
129
|
+
}
|
130
|
+
|
131
|
+
|
132
|
+
// アラーム用サービス
|
133
|
+
Runnable mTask = new Runnable() {
|
134
|
+
public void run() {
|
135
|
+
|
136
|
+
Log.v(TAG,"あああああああああああ:これがLogに表示されれば");
|
137
|
+
|
138
|
+
// 役目を終えたサービスを止める
|
139
|
+
MyResetService.this.stopSelf();
|
140
|
+
Log.v(TAG,"サービス停止");
|
141
|
+
}
|
142
|
+
};
|
143
|
+
|
144
|
+
|
145
|
+
}
|
36
146
|
```
|
2
誤字
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
この下の行をどのように変えればいいのでしょうか? Alarmでなく、ただ12時になったら、メソッドを呼ぶようにしたいです。
|
2
2
|
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), mResetSender);
|
3
3
|
どうぞよろしくお願いします。
|
4
4
|
|
1
誤字
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
//⭐️この下の行をどのように変えればいいのでしょうか? Alarmでなく、ただ12時に
|
1
|
+
//⭐️この下の行をどのように変えればいいのでしょうか? Alarmでなく、ただ12時になったら、メソッドを呼ぶようにしたいです。。⭐️
|
2
2
|
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), mResetSender);
|
3
3
|
どうぞよろしくお願いします。
|
4
4
|
|
@@ -7,7 +7,7 @@
|
|
7
7
|
// Reset(リセット)を設定する
|
8
8
|
mResetSender = this.getPendingIntent();
|
9
9
|
|
10
|
-
// リセットする時間の設定
|
10
|
+
// リセットするメソッドを呼ぶ時間の設定
|
11
11
|
Calendar cal = Calendar.getInstance();
|
12
12
|
cal.setTimeInMillis(System.currentTimeMillis());
|
13
13
|
// 設定した時刻をカレンダーに設定
|
@@ -16,18 +16,18 @@
|
|
16
16
|
cal.set(Calendar.SECOND, 0);
|
17
17
|
cal.set(Calendar.MILLISECOND, 0);
|
18
18
|
|
19
|
-
Toast.makeText(c, String.format("%12d時%00d分にデータをリセット
|
19
|
+
Toast.makeText(c, String.format("%12d時%00d分にデータをリセットするメソッドを呼びます", resetHour, resetMinute), Toast.LENGTH_LONG).show();
|
20
20
|
|
21
|
-
//⭐️この下の行をどのように変えればいいのでしょうか? Alarmでなく、ただ12時にデータ
|
21
|
+
//⭐️この下の行をどのように変えればいいのでしょうか? Alarmでなく、ただ12時にデータをリセットするメソッドを呼びたいです。⭐️
|
22
22
|
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), mResetSender);
|
23
23
|
|
24
24
|
Log.v(TAG, cal.getTimeInMillis()+"ms");
|
25
|
-
Log.v(TAG, "12時にリセット、セット完了");
|
25
|
+
Log.v(TAG, "12時にリセットするメソッドを呼ぶ、セット完了");
|
26
26
|
}
|
27
27
|
|
28
28
|
|
29
29
|
private PendingIntent getPendingIntent() {
|
30
|
-
// 12時に起動するアプリケーションを登録
|
30
|
+
// 12時に起動するアプリケーションを登録(リセットするメソッド)
|
31
31
|
Intent intent = new Intent(c, MyResetService.class);
|
32
32
|
PendingIntent pendingIntent = PendingIntent.getService(c, PendingIntent.FLAG_ONE_SHOT, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
33
33
|
return pendingIntent;
|