發表文章

目前顯示的是 11月, 2017的文章

android 背景執行方法 IntentService、Service

圖片
此方法建議不要長期使用timer長期執行,因為手機螢幕關閉後呈待機狀態timer會停止運作,用sleep(1000)也是會停止計時,想做長期定時通知請看我做定時通知 那篇 。 IntentService是Service的簡易版,已內建一個thread,如果只是想用一個thread就好這個非常適合,建立IntentService比建立Service簡單。 IntentService、Service我做的實驗目前情況: IntentService在完全關閉activity後會停止(就是關掉手機佔存在背景activity) Service在完全關閉activity後也會停止,但是能透過重新啟動service的方式重新啟動,可是service的程式就要重新執行一遍。 service 的生命週期 建立class方式 在manifest.xml裡會有 <service android:name=".MyService" android:enabled="true" android:exported="true" /> 在activity啟動service方式 //open intentservice Intent intent = new Intent(this, NotificationService.class); Log.d(TAG, "service start"); startService(intent); Service public class MyService extends Service { public MyService() { } @Override public IBinder onBind(Intent intent) { // TODO: Return the communication channel to the service. throw new UnsupportedOperationException("Not yet implemented"); } @

android 定時通知(永久長期的) 本篇只講AlarmManager使用

圖片
本篇使用AlarmManager達到準時執行某程式,跟鬧鐘的方式一樣,我之前用過IntentService、Service、Handler,都不能長期使用。 Servicer簡單缺點整理: IntentService:只要關閉activity背景保存視窗,IntentService背景所執行的程式就被強迫結束,好像可以重開(我沒試過)。 Service、Handler:雖然關閉activity會繼續執行,但他是重新在開啟一次service,這到還好,但當手機螢幕關閉處於待機狀態時,service會停止運作暫停在那邊,等你喚醒再繼續執行,所以用這個加timer做定時通知會整個亂掉,timer會停下來不數了。 AlarmManager主要架構是 跟系統註冊鬧鐘甚麼時間點 → 時間到系統會發送一個鬧鈴 → 你的activity接收那個鬧鈴後執行你的程式 當你註冊好幾個鬧鐘已經晚於現在時間,他會馬上通通都執行完 先建立接收鬧鈴的程式 在Manifest.xml 中加入接收系統鬧鈴 <application> <activity android:name=".MainActivity"> </activity> <!-- 當鬧鈴時間到達時要執行的程式 --> <receiver android:name=".AlarmReceiver"> <intent-filter> <action android:name="activity_app" /> </intent-filter> </receiver> </application> 建立一個class檔,執行接收到鬧鈴後的程式 public class AlarmReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) {

android 手機螢幕翻轉 Activity 介面不更新

雖然用WebView可以顯示網頁,但是當手機翻轉後,他會重新再載入一次網頁,每翻轉一次就重新loading,使用者不舒服,service也會多負擔。 在 AndroidManifest.xml 加入 <manifest> <application> <activity ... android:name=".MainActivity" ... android:configchanges="orientation|keyboardhidden|screensize"> ↑ 加入這行 ↑ <intent-filter> <action android:name="android.intent.action.MAIN"/> </intent-filter> </activity > ... </application> </manifest> 從 Android 3.2 (API 13) 開始,螢幕翻轉螢幕尺寸也會跟著改變,所以要再加上 screenSize 在 Activity 中加入 @Override public void onConfigurationChanged(Configuration newConfig) { // TODO Auto-generated method stub super.onConfigurationChanged(newConfig); if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { // 什麼都不用寫 } else { // 什麼都不用寫 } } 參考自: 官方: https://developer.androi

C# 系統音量調整由程式控制

public class demo { //運用WinAPI控制電腦靜音與音量 private const int APPCOMMAND_VOLUME_MUTE = 0x80000; private const int APPCOMMAND_VOLUME_UP = 0x0a0000; private const int APPCOMMAND_VOLUME_DOWN = 0x090000; private const int WM_APPCOMMAND = 0x319; //private static IntPtr Handle; public static Process p = Process.GetCurrentProcess(); [DllImport("user32.dll")] public static extern IntPtr SendMessageW(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam); public static void btnVup_Click() { // 聲音變大 SendMessageW(p.Handle, WM_APPCOMMAND, p.Handle, (IntPtr)APPCOMMAND_VOLUME_UP); } public static void btnVdown_Click() { // 聲音變小 SendMessageW(p.Handle, WM_APPCOMMAND, p.Handle, (IntPtr)APPCOMMAND_VOLUME_DOWN); } public static void btnMute_Click() { // 靜音 SendMessageW(p.Handle, WM_APPCOMMAND, p.Handle, (IntPtr)APPCOMMAND_VOLUME_MUTE); } /** * xp以下的電腦適用 * */

C# 模擬鍵盤滑鼠控制電腦

滑鼠模擬 指定一個螢幕像素的座標位置 int x = 1255; int y = 161; Cursor.Position = new Point(x, y); ClassMouse.LeftClick(); //按下滑鼠左鍵 鍵盤模擬 官方文件 的範例是用小算盤程式,計算111*11= public class demo { [DllImport("USER32.DLL", CharSet = CharSet.Unicode)] public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); // Activate an application window. [DllImport("USER32.DLL")] public static extern bool SetForegroundWindow(IntPtr hWnd); // Send a series of key presses to the Calculator application. public static void Calculator() { // Get a handle to the Calculator application. The window class // and window name were obtained using the Spy++ tool. IntPtr calculatorHandle = FindWindow("ApplicationFrameWindow", "小算盤"); // Verify that Calculator is a running process. if (calculatorHandle == IntPtr.Zero) { MessageBox.Show("Calculator is not running.&q

第一次學習 C#

開啟外部程式 http://abgne.tw/code-snippets/dotnet-process.html 控制滑鼠 https://dotblogs.com.tw/sam319/2009/12/24/12643 加入cs (外部程式碼) https://social.msdn.microsoft.com/Forums/zh-TW/0b27dfdb-76e1-434a-ba64-80c972da82e2/c2cs?forum=233 invoke http://blog.xuite.net/merci0212/wretch/141175679-%5B%E8%BD%89%E8%BC%89%5DC%23+Invoke%E7%9A%84%E7%94%A8%E6%B3%95 https://dotblogs.com.tw/darrent/2010/12/02/19848 thread close http://www.programmer-club.com.tw/ShowSameTitleN/csharp/606.html https://dotblogs.com.tw/alexwang/2016/10/03/124332 c#  serial ports readline https://msdn.microsoft.com/zh-tw/library/system.io.ports.serialport.readline(v=vs.110).aspx get port to combobox http://catcsharp.blogspot.tw/2014/02/cwinform-rs-232serialport.html

raspberrypi 開機自動執行程式 與 在terminal開啟第二個terminal執行python

raspberrypi 開啟另一個terminal執行程式 在terminal輸入: lxterminal -e python3 demo.py 參考自: https://www.raspberrypi.org/forums/viewtopic.php?t=65607 ------------------------------------------------------------------------------------------------------------------------------------------------------- raspberrypi 開機自動執行程式 在terminal中輸入以下指令,開啟檔案: sudo nano /etc/rc.local 以下是加入執行python程式 #!/bin/sh -e # # rc.local # # This script is executed at the end of each multiuser runlevel. # Make sure that the script will "exit 0" on success or any other # value on error. # # In order to enable or disable this script just change the execution # bits. # # By default this script does nothing. # Print the IP address _IP=$(hostname -I) || true if [ "$_IP" ]; then    printf "My IP address is %s\n" "$_IP" fi sudo python /home/pi/demo.py  <==加入在這邊 exit 0 參考自: http://dirtypig8.blogspot.tw/2016/12/python.html#!/tcmbck

python 匯出成exe 主要功能介紹

python 匯出成exe 安裝 pyinstaller python2.x:sudo pip install pyinstaller python3.x:sudo pip3 install pyinstaller 操作 基本:pyinstaller demo.py 他會在terminal目前目錄下建立兩個資料夾build 和 dist 建立執行檔在 dist 資料夾 其他細部功能看參考資料 參考自: http://zmh00.blogspot.tw/2016/01/python-codeexe.html http://zwindr.blogspot.tw/2016/01/python-pyinstaller.html