鎖屏狀態下點亮螢幕,並彈出鬧鐘提示資訊

來源:互聯網
上載者:User

標籤:android   style   io   使用   ar   sp   cti   代碼   on   

鎖屏狀態下點亮螢幕,並彈出鬧鐘提示資訊,可以在鎖定畫面上取消鬧鐘;使用廣播接收鬧鐘定時:


下面是例子裡的核心代碼如下


android 設定定時鬧鐘(包括提醒一次和迴圈提醒):
Intent intent = new Intent(MainActivity.this,MyAlarmBroadCast.class);  
        
        PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);  
          
          
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);  
      
          
        alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);  
              
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 5*1000, pendingIntent);  

android取消指定鬧鐘:
Intent intent = new Intent(AlarmReminderActivity.this,MyAlarmBroadCast.class);  

        PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);  
          
          
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);  
        
        alarmManager.cancel(pendingIntent);

android喚醒鎖定螢幕:
PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
WakeLock mWakelock = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP |PowerManager.SCREEN_DIM_WAKE_LOCK, "SimpleTimer");
        mWakelock.acquire();

該方法的使用一定要伴隨mWakelock.release();否則會報異常;
推薦使用方法:在喚醒螢幕顯示的activity的onResume方法中喚醒,在onPause方法中release;


如何讓activity顯示在鎖定畫面上:
首先要喚醒鎖定螢幕,然後設定activity的屬性,使它可以顯示在鎖定畫面上;
喚醒螢幕的方法參考上面;
activity的設定包括兩部分:
1-onCreate方法中設定如下代碼:
super.onCreate(savedInstanceState);
final Window win = getWindow();
 win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
 | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
 win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
 | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
setContentView(R.layout.act_alarmreminder);
2-AndroidManifest.xml對該activity的聲明中設定屬性:
android:label="@string/app_name" 
            android:launchMode="singleInstance"
android:excludeFromRecents="true"
android:taskAffinity=""
android:theme="@android:style/Theme.Wallpaper.NoTitleBar"(這一行很重要)
android:configChanges="orientation|keyboardHidden|keyboard|navigation"

鎖屏狀態下點亮螢幕,並彈出鬧鐘提示資訊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.