//タイマー終了後のところでNotificationを用いて、タイマー終了後にステータスバーに表示させたいのですができません。
よろしくお願いします。(OnFinishのところでtoastやsettextはタイマー終了後に反映されてます。)
java
1public class TimerActivity extends AppCompatActivity { 2 3 private TextView timerText; 4 private EditText edit; 5 private AlarmManager aManager; 6 private Calendar calendar; 7 private SharedPreferences data; 8 9 long countNumber = 5000; 10 11 // インターバル msec 12 long interval = 100; 13 14 // 表示形式を設定 15 private SimpleDateFormat dataFormat = new SimpleDateFormat("mm:ss.SSS"); 16 17 @Override 18 protected void onCreate(Bundle savedInstanceState) { 19 super.onCreate(savedInstanceState); 20 setContentView(R.layout.my_fragment); 21 22 edit= (EditText)findViewById(R.id.timesec); 23 aManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE); 24 25 data = PreferenceManager.getDefaultSharedPreferences(this); 26 27 // レイアウトと関連 28 Button startButton = findViewById(R.id.start_button); 29 Button stopButton = findViewById(R.id.stop_button); 30 Button resetButton = findViewById(R.id.reset_button); 31 Button setButton = findViewById(R.id.set_button); 32 Button button1 = (Button) findViewById(R.id.main_button); 33 34 //テキストに初期時間の設定msec 35 timerText = findViewById(R.id.timer); 36 timerText.setText(dataFormat.format(5000)); 37 38 // インスタンス生成 39 // CountDownTimer(long millisInFuture, long countDownInterval) 40 final CountDown countDown = new CountDown(countNumber, interval); 41 42 //タイマー開始 43 startButton.setOnClickListener(new View.OnClickListener() { 44 @Override 45 public void onClick(View v) { 46 // 開始 47 countDown.start(); 48 } 49 }); 50 51 //タイマー中止 52 stopButton.setOnClickListener(new View.OnClickListener(){ 53 54 @Override 55 public void onClick(View v) { 56 // 中止 57 countDown.cancel(); 58 } 59 }); 60 61 //タイマーリセット 62 resetButton.setOnClickListener(new View.OnClickListener(){ 63 @Override 64 public void onClick(View v) { 65 // リセット 66 timerText.setText(dataFormat.format(180000)); 67 } 68 }); 69 70 setButton.setOnClickListener(new View.OnClickListener() { 71 72 73 @Override 74 public void onClick(View v) { 75 //時間をセットする。 76// String timesec = edit.getText().toString(); 77// timerText.setText(timesec); 78// long timesec1 = Long.valueOf(timesec); 79// 80// countNumber = timesec1; 81 82// SharedPreferences data = getSharedPreferences("save", Context.MODE_PRIVATE); 83// SharedPreferences.Editor editor = data.edit(); 84// editor.putLong( "save", timesec1 ); 85// editor.commit(); 86 87 } 88 }); 89 } 90 91 class CountDown extends CountDownTimer { 92 93 CountDown(long millisInFuture, long countDownInterval) { 94 super(millisInFuture, countDownInterval); 95 } 96 //タイマー終了後 97 @Override 98 public void onFinish() { 99 100 // 完了 101 Toast.makeText(TimerActivity.this,"タイマーが終了しました。",Toast.LENGTH_LONG).show(); 102 103 //ステータスバーに通知:表示されない 104 Intent intent = new Intent(TimerActivity.this, AlarmNotificationActivity.class); 105 PendingIntent pendingIntent = PendingIntent.getActivity(TimerActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 106 107 NotificationCompat.Builder builder = new NotificationCompat.Builder(TimerActivity.this); 108 Notification notification = builder 109 110 // 通知のタイトル 111 .setContentTitle("タイマー終了!") 112 // この通知が未だ表示されていない時だけ、音やバイブレーション、ショートメッセージの表示を行う 113 .setOnlyAlertOnce(false) 114 // タップしたら消えるようにする 115 // 通知の詳細メッセージ 116 .setContentText("00:00.000") 117 // 通知のアイコン 118 .setSmallIcon(R.drawable.ic_launcher) 119 // 通知を表示した瞬間、通知バーに表示するショートメッセージ 120 .setTicker("通知だヨ!") 121 // 通知をタップした時に使う PendingIntent 122 .setContentIntent(pendingIntent) 123 .setAutoCancel(true) 124 .build(); 125 NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 126 127 manager.notify(0, notification); 128 129 timerText.setText(dataFormat.format(0)); 130 } 131 132 // インターバルで呼ばれる 133 @Override 134 public void onTick(long millisInFuture) { 135 timerText.setText(dataFormat.format(millisInFuture)); 136 137 } 138 } 139} 140 141 142public class AlarmNotificationActivity extends AppCompatActivity { 143 144 145 @Override 146 protected void onCreate(Bundle savedInstanceState) { 147 super.onCreate(savedInstanceState); 148 149 Notification notification = new Notification(); 150 NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 151 manager.notify(0, notification); 152 } 153} 154 155
回答1件
あなたの回答
tips
プレビュー