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

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

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

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

Android Studio

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

Q&A

解決済

1回答

2564閲覧

[Android]NotificationManagerによる通知のバイブレーションカスタム、スリープ中の画面点灯を実現したい

tokoroten_12

総合スコア24

Java

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

Android Studio

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

0グッド

0クリップ

投稿2021/12/27 02:58

編集2021/12/27 03:26

前提・実現したいこと

Android Studio 4.2.1、言語はJava、API29を使用しています。
簡易的なタイマーアプリを作りたく、一定時間の経過で作業中→休憩中→終了と遷移するような動作をさせたいと考えています。
現在AlarmManagerとNotificationManagerを使い、作業中、休憩中の時間が経過した場合に通知を表示させています。

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

時間のカウントダウン、通知の表示は出来たのですが以下の2点の問題が発生しました。
①バイブレーションのカスタマイズが出来ない(1回震えるごとに時間を伸ばし4回振動させたい)
②スリープ中に通知が来ると画面が点灯しない(バイブレーションは発生するので通知自体は問題ない)

MainActivtyとAlarmManagerのコードは下記の通りです。
デバッグ用として作業60秒、休憩30秒で設定しています。

java

1public class MainActivity extends AppCompatActivity { 2 3 CountDownTimer countDownTimer; 4 public enum State{ 5 // 列挙型により現在の状態定義 6 WORK, 7 REST, 8 } 9 private State _Current_State; 10 11 @Override 12 protected void onCreate(Bundle savedInstanceState) { 13 super.onCreate(savedInstanceState); 14 setContentView(R.layout.activity_main); 15 // 状態設定 16 _Current_State = State.WORK; 17 // 60秒のタイマーを設定 18 TimerStart(60); 19 // AlarmManager設定 20 AlarmManagerSetting(60); 21 } 22 23 /** 24 * AlarmMangerを設定 25 */ 26 private void AlarmManagerSetting(int time) 27 { 28 // 呼び出したい時間をセットする 29 Calendar cal = Calendar.getInstance(); 30 cal.setTimeInMillis(System.currentTimeMillis()); 31 cal.add(Calendar.SECOND, time); 32 long alarm_time = cal.getTimeInMillis(); //カレンダーをミリ秒に変換して変数に格納 33 34 //アラームクロックインフォを作成してアラーム時間をセット 35 AlarmManager.AlarmClockInfo clockInfo = new AlarmManager.AlarmClockInfo(alarm_time,null); 36 37 // Intentの設定、 38 Intent intent = new Intent(MainActivity.this, AlarmManagerTest.class); 39 // Pending Intent使ってレシーバーをセットする 40 PendingIntent pending = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0); 41 42 // アラームをセットする 43 AlarmManager am = (AlarmManager)MainActivity.this.getSystemService(ALARM_SERVICE); 44 if(am != null){ 45 am.setAlarmClock(clockInfo, pending); 46// am.set(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(), pending); こっちでもダメ 47 } 48 49 } 50 51 /* 52 CountDownTimerを1秒毎に呼び出すように設定して時間のテキストを更新していく 53 */ 54 public void TimerStart(long time_sec) { 55 // 1秒毎に呼び出されるCountDownTimerを設定 56 countDownTimer = new CountDownTimer(time_sec*1000, 1000) { 57 @Override 58 public void onTick(long milliTillFinish) { 59 // 秒を計算する 60 int sec = (int) (milliTillFinish / 1000) % 60; 61 // TimerTextをxx形式で更新 62 String timerLeftFormatted = String.format(Locale.getDefault(), "%02d", sec+1); 63 ((TextView) findViewById(R.id.Timer_Text)).setText(timerLeftFormatted); 64 } 65 // 終了時の動作 66 public void onFinish() { 67 countDownTimer.cancel(); 68 if(_Current_State == State.WORK) 69 { 70 // 作業中の場合は休憩に遷移 71 ((TextView) findViewById(R.id.tv_Description)).setText("休憩中"); 72 _Current_State = State.REST; 73 // 休憩時間のカウントダウン(30秒) 74 TimerStart(30); 75 AlarmManagerSetting(30); 76 } 77 else{ 78 // 休憩が終わると終了 79 ((TextView) findViewById(R.id.Timer_Text)).setText("終了"); 80 } 81 } 82 }; 83 countDownTimer.start(); 84 } 85}

java

1public class AlarmManagerTest extends BroadcastReceiver { 2 3 @Override 4 public void onReceive(Context context, Intent intent) { 5 6 int requestCode = intent.getIntExtra("RequestCode",0); 7 String message = "時間経過"; 8 String channelId = "default"; 9 String title = context.getString(R.string.app_name); 10 int alarmType = intent.getIntExtra("alarm_type", 0); 11 12 // Intent作成 13 Intent tap_intent = new Intent(context, MainActivity.class); 14 tap_intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 15 PendingIntent pendingIntent = 16 PendingIntent.getActivity(context, requestCode, tap_intent, PendingIntent.FLAG_UPDATE_CURRENT); 17 18 // Notification Channel 設定 19 NotificationManager notificationManager = 20 (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); 21 22 Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 23 24 NotificationChannel channel = new NotificationChannel( 25 channelId, title , NotificationManager.IMPORTANCE_DEFAULT); 26 channel.setDescription(message); 27 channel.enableVibration(true); 28 channel.enableLights(true); 29 channel.setLightColor(Color.BLUE); 30 channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE); 31 channel.setSound(defaultSoundUri, null); 32 channel.setShowBadge(true); 33 channel.setVibrationPattern(new long[]{1000, 2000, 3000, 4000}); // ここを変えてもバイブレーションのパターンが変わらない 34 35 if(notificationManager != null){ 36 // 通知の作成 37 notificationManager.createNotificationChannel(channel); 38 39 Notification notification = new Notification.Builder(context, channelId) 40 .setContentTitle(title) 41 // android標準アイコンから 42 .setSmallIcon(android.R.drawable.ic_lock_idle_alarm) 43 .setContentText(message) 44 .setAutoCancel(true) 45 .setContentIntent(pendingIntent) 46 .setWhen(System.currentTimeMillis()) 47 .build(); 48 // 通知 49 notificationManager.notify(R.string.app_name, notification); 50 } 51 } 52}

xml

1<?xml version="1.0" encoding="utf-8"?> 2<manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.example.blogaplication"> 4 5 <uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" /> 6 <application 7 android:allowBackup="true" 8 android:icon="@mipmap/ic_launcher" 9 android:label="@string/app_name" 10 android:roundIcon="@mipmap/ic_launcher_round" 11 android:supportsRtl="true" 12 android:theme="@style/Theme.BlogAplication"> 13 <activity android:name=".MainActivity" android:exported="true"> 14 <intent-filter> 15 <action android:name="android.intent.action.MAIN" /> 16 17 <category android:name="android.intent.category.LAUNCHER" /> 18 </intent-filter> 19 </activity> 20 21 <!-- 通常の受付 --> 22 <receiver android:name=".AlarmManagerTest" /> 23 24 </application> 25 26</manifest>

試したこと

①のバイブレーションのカスタマイズですが、以下のsetVibrationPattern項目を様々な値に変更しても500msecの振動を2回繰り返す動作になってしまいます

channel.setVibrationPattern(new long[]{1000, 2000, 3000, 4000});

②のスリープ時の画面点灯対策としてAlarmManagerをRTC_WAKEUPに変更してみましたがうまくいきませんでした。

am.set(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(), pending);

①、②の動作を満たすためにはどのような設定をすれば実現可能でしょうか?

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

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

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

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

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

jimbe

2021/12/27 06:52

実機のユーザー補助のほうでバイブレータOFFになっていました。失礼しました。
guest

回答1

0

ベストアンサー

まず、 NotificationChannel は一度登録するとアプリをアンインストールするまでシステムに登録されっぱなしで、二度目からは上書きされません。
ですので、 NotificationChannel の設定を変更したら、一度アンインストールしてからインストールしてテストしてください。

また、 setVibrationPattern のパラメータは "停止時間", "振動時間" の繰り返しです。ですので 4 回振動させるなら
{ 0, 1000, 100, 2000, 100, 3000, 100, 4000 }
といったように、最初の停止時間を 0 にし、各振動時間の間に停止時間(例えば 100 )を入れる必要があります。

スリープ中の通知に関しましては、Android のディスプレイの設定で「通知が来たらスリープを抜ける」という設定("アンビエント")がありますので、アプリから何かするということは必要無いかもしれません。

投稿2021/12/27 07:29

編集2021/12/27 07:43
jimbe

総合スコア12648

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

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

tokoroten_12

2021/12/27 08:29

ありがとうございます、スリープ通知ですがAndroid側をアンビエント設定することで通知時に画面点灯するようになりました。 setVibrationPatternですが、アプリインストールを行い上記のようにlongパラメータを変更しましたがブッブッと500msec程度の振動を2回繰り返す処理から変わりません。 channel.enableVibration();をfalseに変更して振動しなくなることを確認したのでNotificationChannel の再設定は問題なく出来ていると思われます。 何か足りない設定等ありましたらご教授よろしくお願いいたします。
jimbe

2021/12/27 08:48

こちら Xperia XZ3(au SOV39 Android10 build:52.1.C.0.360) で setVibrationPattern のパラメータを回答のようにしただけで動作しています。 ( compileSdk 31, minSdk 29, targetSdk 31 ) 機種によってバイブレーションの設定が出来ない可能性はあるかもしれません。
tokoroten_12

2021/12/28 00:58

HUAWEI P30端末ではバイブレーションの設定が出来ませんでしたが、HUAWEI nova3で試してみた所バイブレーションの制御できました。 機種によって設定が出来ないようです、ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問