質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Q&A

0回答

972閲覧

Android でバックグラウンドでの通知について

xue

総合スコア6

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

0グッド

0クリップ

投稿2020/07/15 05:45

前提・実現したいこと

Android studioで通知機能を搭載したアプリを作っています。
設定画面でトグルスイッチをonにすると,指定された時間に通知されるシステム(バックグラウンドでも)を作りたいです.

発生している問題・エラーメッセージ

トグルスイッチをonにすると,指定された時間に通知は来るのですが,画面を遷移する度に通知が来てしまいます. 画面遷移をする度にstartForegroundService(intent)が呼ばれているからだと思いますが,これがないとバックグラウンド処理が行われないので困っています.

該当のソースコード

//設定画面 @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); SharedPreferences defaultSharedPreferencesAlert2 = PreferenceManager.getDefaultSharedPreferences(requireActivity()); boolean isEnableAlert = defaultSharedPreferencesAlert2.getBoolean("preference_alert2",true); if(isEnableAlert) { Intent intent = new Intent(getActivity(), TestService.class); intent.putExtra("REQUEST_CODE", 1); requireActivity().startForegroundService(intent); } } //TestService.class @Override public int onStartCommand(Intent intent, int flags, int startId) { final NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); String chID = getString(R.string.app_name); int requestCode = intent.getIntExtra("REQUEST_CODE",0); Context context = getApplicationContext(); PendingIntent pendingIntent = PendingIntent.getActivity(context, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { //APIが「26」以上の場合 NotificationChannel notificationChannel = new NotificationChannel(chID, chID, NotificationManager.IMPORTANCE_DEFAULT); notificationChannel.setLightColor(Color.GREEN); notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE); notificationChannel.enableVibration(true); notificationChannel.setDescription(chID); assert notificationManager != null; notificationManager.createNotificationChannel(notificationChannel); notification = new Notification.Builder(this, chID) .setContentTitle(getString(R.string.app_name)) .setContentText("アドバイザーからコメントが届きました!") //通知内容 .setSmallIcon(R.drawable.ic_launcher_background) .setAutoCancel(true) .setContentIntent(pendingIntent) .setWhen(System.currentTimeMillis()) .build();//通知のビルド } //通知の発行 @SuppressLint("SimpleDateFormat") SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); final Timer timer = new Timer(false); TimerTask task = new TimerTask() { @Override public void run() { assert notificationManager != null; notificationManager.notify(0, notification); timer.cancel(); } }; try { timer.schedule(task, sdf.parse("2020/07/25 15:00:00")); } catch (ParseException e) { e.printStackTrace(); } startForeground(1, notification); return START_NOT_STICKY; }

補足情報

開発言語:Java
開発環境:Android studio

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問