現在、アラームを作成しています。
Broadcast receiverを通して起動時間になるとスリープと画面ロックを解除しアラームのアクテイビティが起動して
notificationを通知しアラームが鳴るようになっているのですが端末が長い時間スリープ状態(5時間ぐらい?)
になるとnotificationは通知されるのですがアクテイビティが立ち上がらないあるいは遅れて立ち上がる(5~10分ぐらい)現象が発生します。機種本体にパスコードをかけてるのが問題なのかと思ったのですが他のアラームアプリはちゃんと動くので原因がよくわかりません。
お知恵をお貸しください。よろしくお願いします。
環境は
機種 Zenfone 3 Laser
android ver 7.1.1.1
となっています。
また、本機種の省電力設定から当アプリを除外しています。
下記に設定した時間になった際に起動するアクテビティのソースコードを掲載させていただきます。(一部関係のない部分は消去しています)
ここのwakeFromSleep()でスリープの解除や画面のロックを解除しています。
java
1 2import android.app.Notification; 3import android.app.NotificationManager; 4import android.app.PendingIntent; 5import android.content.BroadcastReceiver; 6import android.content.Context; 7import android.content.Intent; 8import android.support.v4.content.ContextCompat; 9import android.support.v7.app.NotificationCompat; 10import android.util.Log; 11import java.util.Locale; 12 13public class Alarm_vib extends Activity { 14 //スリープ解除用関数 15 private KeyguardManager.KeyguardLock keylock; 16 private PowerManager.WakeLock wakelock; 17 //ローカライズ用変数 18 Locale locale = Locale.getDefault(); 19 //変数の宣言 20 private LoopEngine loopEngine = new LoopEngine();//ハンドラ用 21 //チェックボックス状態保存 22 private Integer[] user_chkbox_status=new Integer[7]; 23 //グローバル変数の設定 24 MyApplication MyGlobal; 25 //コンテキスト設定 26 Context context; 27 PendingIntent pending; 28 AlarmManager am; 29 //テキストビュー宣言 30 TextView local_textview; 31 //変数 32 URL url; 33 private static final String TAG = "MainActivity"; 34 //アラームのポジション 35 private int position_alarm; 36 @Override 37 protected void onCreate(Bundle savedInstanceState) { 38 super.onCreate(savedInstanceState); 39 setContentView(R.layout.vib_layout); 40 //広告 41 //スリープの解除 42 wakeFromSleep(); 43 loopEngine.start(); 44 //グローバル変数の取得 45 MyGlobal = (MyApplication) this.getApplication(); 46 context=MyApplication.getAppContext(); 47 //GLOBALの初期化 48 MyGlobal.MyapplicationAllInit(); 49 //レイアウトの関連付け 50 //壁紙の取得とはLinearLayoutへの設定 51 //全体レイアウトの関連付け 52 LinearLayout Entire_layout =(LinearLayout) findViewById(R.id.Entire_layout); 53 WallpaperManager wpm = WallpaperManager.getInstance(getApplication()); 54 Drawable wallpaper = wpm.getDrawable(); 55 Entire_layout.setBackground(wallpaper); 56 //遷移前のインテントの受け取り 57 Intent intent_Alarm_Variable =getIntent(); 58 //アラームの再設定 59 final int position=intent_Alarm_Variable.getIntExtra("SET_ALARM_POSITION",0); 60 position_alarm=position; 61 //ボタン 62 final Button btn = (Button) findViewById(R.id.stop_button); 63 btn.setOnClickListener(new View.OnClickListener() { 64 public void onClick(View v) { 65 loopEngine.stop(); 66 //アラームの再セット 67 Alarm_set(); 68 //アプリの終了 69 keylock.reenableKeyguard(); 70 finish(); 71 } 72 }); 73 } 74 //スリープ解除関数 75 private void wakeFromSleep() { 76 wakelock = ((PowerManager) getSystemService(android.content.Context.POWER_SERVICE)) 77 .newWakeLock(PowerManager.FULL_WAKE_LOCK 78 | PowerManager.ACQUIRE_CAUSES_WAKEUP 79 | PowerManager.ON_AFTER_RELEASE, "disableLock"); 80 wakelock.acquire(); 81 wakelock.release(); 82 //ロック解除 83 KeyguardManager keyguard = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE); 84 keylock = keyguard.newKeyguardLock("disableLock"); 85 keylock.disableKeyguard(); 86 } 87//一定時間後にupdateを呼ぶためのオブジェクト 88class LoopEngine extends Handler { 89 private boolean isUpdate; 90 public void start() { 91 this.isUpdate = true; 92 handleMessage(new Message()); 93 } 94 public void stop() { 95 this.isUpdate = false; 96 } 97 //バイブレータ 98 public void update() 99 { 100 Vibrator vibrator = (Vibrator) MyApplication.getAppContext().getSystemService(VIBRATOR_SERVICE); 101 vibrator.vibrate(1000); 102 } 103 @Override 104 public void handleMessage(Message msg) { 105 this.removeMessages(0);//既存のメッセージは削除 106 if (this.isUpdate) { 107 this.update();//自信が発したメッセージを取得してupdateを実行 108 sendMessageDelayed(obtainMessage(0), 1000);//1000ミリ秒後にメッセージを出力 109 } 110 } 111 112} 113 114
あなたの回答
tips
プレビュー