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

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

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

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

Q&A

0回答

1518閲覧

Android kotlin 通知バーでアプリ起動後、戻るボタンで通知バーが再表示してしまう

退会済みユーザー

退会済みユーザー

総合スコア0

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

0グッド

0クリップ

投稿2020/06/21 13:27

編集2020/06/21 13:40

前提・実現したいこと

1.通知バーをタップ

2.アプリ起動

3.起動したアプリの戻るボタンを押す

4.ホーム画面へ(アプリは最小化)
という動きを実現したいです

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

上記3のあと再び通知バーが表示されてしまいます。
通知バーを表示せずにアプリを最小化してホーム画面へ移動させたいです。
ただし、通知バーはアプリのキャンセルボタン押下するまで、繰り返し表示するようにしています。
そのために戻るボタンで通知バーの繰り返し処理をキャンセルはしないようにしたいです。

該当のソースコード

kotlin

1MainActivity.kt 2package com.mafactory.myapplication 3 4import android.app.AlarmManager 5import android.app.PendingIntent 6import android.content.Context 7import android.content.Intent 8import android.os.Bundle 9import android.util.Log 10import android.widget.Button 11import android.widget.Toast 12import androidx.appcompat.app.AppCompatActivity 13import java.util.* 14 15private var am: AlarmManager? = null 16private var pending: PendingIntent? = null 17private val requestCode = 1 18class MainActivity : AppCompatActivity() { 19 override fun onCreate(savedInstanceState: Bundle?) { 20 super.onCreate(savedInstanceState) 21 setContentView(R.layout.activity_main) 22 //通知バーセット 23 setNotificationBar() 24 25 // アラームの取り消し 26 val buttonCancel = findViewById<Button>(R.id.button_cancel) 27 buttonCancel.setOnClickListener { 28 //通知バーキャンセル 29 cancelNotificationBar() 30 } 31 } 32 fun setNotificationBar() { 33 val intent = Intent(applicationContext, AlarmNotification::class.java) 34 intent.putExtra("RequestCode", requestCode) 35 pending = PendingIntent.getBroadcast( 36 applicationContext, requestCode, intent, 0) 37 38 val calendar = Calendar.getInstance() 39 calendar.timeInMillis = System.currentTimeMillis() 40 //時刻設定 41 calendar.set(Calendar.HOUR_OF_DAY,14 ) 42 calendar.set(Calendar.MINUTE, 0) 43 calendar.set(Calendar.SECOND, 0) 44 // アラームをセットする 45 am = getSystemService(Context.ALARM_SERVICE) as AlarmManager 46 if (am != null) { 47 am!!.setExact(AlarmManager.RTC_WAKEUP, 48 calendar.timeInMillis, pending) 49 } 50 //繰り返しをセットする 51 am?.setInexactRepeating( 52 AlarmManager.RTC_WAKEUP, 53 calendar.timeInMillis , 54 AlarmManager.INTERVAL_HOUR, 55 pending 56 ) 57 } 58 fun cancelNotificationBar() { 59 val indent = Intent(applicationContext, AlarmNotification::class.java) 60 val pending = PendingIntent.getBroadcast( 61 applicationContext, requestCode, indent, 0 62 ) 63 64 // アラームを解除する 65 val am = this@MainActivity.getSystemService(Context.ALARM_SERVICE) as AlarmManager 66 if (am != null) { 67 am.cancel(pending) 68 Toast.makeText( 69 applicationContext, 70 "alarm cancel", Toast.LENGTH_SHORT 71 ).show() 72 Log.d("debug", "cancel") 73 } else { 74 Log.d("debug", "null") 75 } 76 } 77}

kotlin

1AlarmNotification.kt 2package com.mafactory.myapplication 3 4import android.app.* 5import android.content.BroadcastReceiver 6import android.content.Context 7import android.content.Intent 8import android.graphics.Color 9import android.media.RingtoneManager 10 11class AlarmNotification : BroadcastReceiver() { 12 // データを受信した 13 override fun onReceive(context: Context, intent: Intent) { 14 val requestCode = intent.getIntExtra("RequestCode", 0) 15 val pendingIntent: PendingIntent = PendingIntent.getActivity( 16 context, 17 requestCode, 18 Intent(context , MainActivity::class.java), 19 PendingIntent.FLAG_UPDATE_CURRENT) 20 val channelId = "default" 21 val title = context.getString(R.string.app_name) 22 val message = "タップしてOKボタンを押して下さい" 23 val notificationManager = 24 context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager 25 val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION) 26 27 // Notification Channel 設定 28 val channel = NotificationChannel( 29 channelId, title, NotificationManager.IMPORTANCE_DEFAULT 30 ) 31 channel.description = message 32 channel.enableVibration(true) 33 channel.canShowBadge() 34 channel.enableLights(true) 35 channel.lightColor = Color.BLUE 36 // the channel appears on the lockscreen 37 channel.lockscreenVisibility = Notification.VISIBILITY_PRIVATE 38 channel.setSound(defaultSoundUri, null) 39 channel.setShowBadge(true) 40 if (notificationManager != null) { 41 notificationManager.createNotificationChannel(channel) 42 val notification = Notification.Builder(context, channelId) 43 .setSmallIcon(R.drawable.ic_icon) 44 .setContentTitle(title) 45 .setContentText(message) 46 .setAutoCancel(true) 47 .setContentIntent(pendingIntent) 48 .setWhen(System.currentTimeMillis()) 49 .build() 50 51 // 通知 52 notificationManager.notify(R.string.app_name, notification) 53 } 54 } 55}

kotlin

1AndroidManifest.xml 2<?xml version="1.0" encoding="utf-8"?> 3<manifest xmlns:android="http://schemas.android.com/apk/res/android" 4 package="com.mafactory.myapplication"> 5 <uses-permission android:name="android.permission.WAKE_LOCK"/> 6 <uses-permission android:name="android.permission.VIBRATE"/> 7 8 <application 9 android:allowBackup="true" 10 android:icon="@mipmap/ic_launcher" 11 android:label="@string/app_name" 12 android:roundIcon="@mipmap/ic_launcher_round" 13 android:supportsRtl="true" 14 android:theme="@style/AppTheme"> 15 <activity android:name=".MainActivity"> 16 17 <intent-filter> 18 <action android:name="android.intent.action.MAIN" /> 19 20 <category android:name="android.intent.category.LAUNCHER" /> 21 </intent-filter> 22 </activity> 23 <receiver 24 android:name=".AlarmNotification" 25 android:process=":remote" > 26 </receiver> 27 </application> 28 29</manifest>

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問