こんにちは。
現在、私はこのサイトをみながら定時になれば、指定した動作をするようにコードを考えています。
http://gupuru.hatenablog.jp/entry/2014/04/16/191310
しかしAlarmNotificationReceiveでToastは通るのに、それ以外のコードは通りません。
というかLog.d("test1","ここを通った");
もLogcatに出てくれません。
私のコードがおかしいのかチェックしてください。
一つ目のコードは定時になったら呼ばれるクラスです。
Java
1AlarmNotificationReceive.java 2 3public class AlarmNotification extends BroadcastReceiver{ 4 //定時になったら文字を表示する 5 public static int okfalse; 6@Override 7 public void onReceive(Context context, Intent intent) { 8okfalse = 1; 9 Toast.makeText(context, "Toast example", Toast.LENGTH_LONG).show(); 10 Log.d("test1","ここを通った"); 11 okfalse = okfalse +1; 12 } 13
二つ目のコードはMainActiity(Fragment)です。
Java
1MainFragment 2 3 @Override 4 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ 5 View view = inflater.inflate(R.layout.fragment_main,container,false); 6 7... 8 AlarmNotification alarmNotification = new AlarmNotification(); 9 if(alarmNotification.okfalse == 1){ 10 Log.d("no","表示しない"); 11 }else if (alarmNotification.okfalse == 2){ 12 Log.d("ok","表示する。"); 13 } 14} 15 16 17 Calendar calendar = Calendar.getInstance(); 18 // Calendarを使って現在の時間をミリ秒で取得 19 calendar.setTimeInMillis(System.currentTimeMillis()); 20 // 5秒後に設定 21 calendar.add(Calendar.SECOND, 5); 22// calendar.set(Calendar.HOUR_OF_DAY,9); 23 //明示的なBroadCast 24 Intent intent = new Intent(getActivity().getApplicationContext(), 25 AlarmNotification.class); 26 PendingIntent pending = PendingIntent.getBroadcast( 27 getActivity().getApplicationContext(), 0, intent, 0); 28 // アラームをセットする 29 AlarmManager am = (AlarmManager) getActivity().getSystemService(ALARM_SERVICE); 30 if(am != null) { 31 am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pending); 32 Log.d("ここ","dane"); 33 Toast.makeText(getActivity().getApplicationContext(), 34 "Set Alarm ", Toast.LENGTH_SHORT).show(); 35 }
お手数をおかけしますが、アドバイス等や改善等を教えてください。
あなたの回答
tips
プレビュー