この下の行をどのように変えればいいのでしょうか? Alarmでなく、ただ12時になったら、メソッドを呼ぶようにしたいです。
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), mResetSender);
どうぞよろしくお願いします。
java
1 public void addReset(int resetHour, int reseMinute){ 2 // Reset(リセット)を設定する 3 mResetSender = this.getPendingIntent(); 4 5 // リセットするメソッドを呼ぶ時間の設定 6 Calendar cal = Calendar.getInstance(); 7 cal.setTimeInMillis(System.currentTimeMillis()); 8 // 設定した時刻をカレンダーに設定 9 cal.set(Calendar.HOUR_OF_DAY, resetHour); 10 cal.set(Calendar.MINUTE, resetMinute); 11 cal.set(Calendar.SECOND, 0); 12 cal.set(Calendar.MILLISECOND, 0); 13 14 Toast.makeText(c, String.format("%12d時%00d分にデータをリセットするメソッドを呼びます", resetHour, resetMinute), Toast.LENGTH_LONG).show(); 15 16//⭐️この下の行をどのように変えればいいのでしょうか? Alarmでなく、ただ12時にデータをリセットするメソッドを呼びたいです。⭐️ 17 am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), mResetSender); 18 19 Log.v(TAG, cal.getTimeInMillis()+"ms"); 20 Log.v(TAG, "12時にリセットするメソッドを呼ぶ、セット完了"); 21 } 22 23 24 private PendingIntent getPendingIntent() { 25 // 12時に起動するアプリケーションを登録(リセットするメソッド) 26 Intent intent = new Intent(c, MyResetService.class); 27 PendingIntent pendingIntent = PendingIntent.getService(c, PendingIntent.FLAG_ONE_SHOT, intent, PendingIntent.FLAG_UPDATE_CURRENT); 28 return pendingIntent; 29 } 30}
実際にAlarmManagerを使えたコードです。<完成>
//MainActivity
java
1package com.example.android.sample.easy; 2 3import android.app.AlarmManager; 4import android.app.PendingIntent; 5import android.content.Intent; 6import android.support.v7.app.AppCompatActivity; 7import android.os.Bundle; 8import android.util.Log; 9import android.widget.Toast; 10 11 12 13public class MainActivity extends AppCompatActivity { 14 15 private static final String TAG = MainActivity.class.getSimpleName(); 16 17 @Override 18 protected void onCreate(Bundle savedInstanceState) { 19 super.onCreate(savedInstanceState); 20 setContentView(R.layout.activity_main); 21 22 addReset(14,53); 23 getPendingIntent(); 24 } 25 26 27 public void addReset(int resetHour, int resetMinute) { 28 29 PendingIntent mResetSender = this.getPendingIntent(); 30 31 // メソッドを呼ぶ時間の設定 32 java.util.Calendar cal = java.util.Calendar.getInstance(); 33 cal.setTimeInMillis(System.currentTimeMillis()); 34 // 設定した時刻をカレンダーに設定 35 cal.set(cal.HOUR_OF_DAY, resetHour); 36 cal.set(cal.MINUTE, resetMinute); 37 cal.set(cal.SECOND, 0); 38 cal.set(cal.MILLISECOND, 0); 39 40 Toast.makeText(this, String.format("%d時%d分にtoast表示のメソッドを呼びます", resetHour, resetMinute), Toast.LENGTH_LONG).show(); 41 42 // AlramManager取得 43 AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); 44 // AlarmManagerにPendingIntentを登録 45 am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), mResetSender); 46 47 48 Log.v(TAG, cal.getTimeInMillis() + "ms"); 49 Log.v(TAG, "2:30に表示するメソッドを呼ぶ、セット完了"); 50 } 51 52 private PendingIntent getPendingIntent() { 53 Intent intent = new Intent(this, MyResetService.class); 54 // 55 PendingIntent pendingIntent = PendingIntent.getService(this, PendingIntent.FLAG_ONE_SHOT, intent, PendingIntent.FLAG_UPDATE_CURRENT); 56 return pendingIntent; 57 58 } 59 60 61}
//MyResetService
package com.example.android.sample.easy; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.util.Log; public class MyResetService extends Service { private static final String TAG = MyResetService.class.getSimpleName(); public MyResetService() {} @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { Thread thr = new Thread(null, mTask, "MyResetServiceThread"); thr.start(); Log.v(TAG,"スレッド開始"); } // アラーム用サービス Runnable mTask = new Runnable() { public void run() { Log.v(TAG,"あああああああああああ:これがLogに表示されれば"); // 役目を終えたサービスを止める MyResetService.this.stopSelf(); Log.v(TAG,"サービス停止"); } }; }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/04/23 04:37