開機自啓動,當時在開發Qt的時候主要是通過寫註冊表來實現的,也就是把你的軟件名稱的路徑寫入註冊表中,然後當電腦啓動的時候,就會啓動你的軟件!呵呵。。。PC端是這樣,那手機端是怎麼樣呢?今天看到博客說是當手機啓動的時候會發出一個廣播ACTION_BOOT_COMPELETE,因此我們需要做就是接受這個廣播,然後再執行我們的操作,就是啓動第一個activity然後就可以啦!
ok,廢話不多說,直接上代碼。
1,首先我們要定義一個類來接收廣播
Public class autostartReceiver extends BoradcastReceiver{
@Override
Public void onReceive(Context context,Intent intent){
if(Intent.getAction().equals("android.intent.action.BOOT_COMPELETED")){
Intent intent1=new Intent();
intent1.setClass(context,MainActivity.calss);
intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}
}
2,然後在AndroidManifest.xml中定義改類,並添加開機權限
<receiver android:name=".autostartReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPELETED"/>
<category android:name="android.intent.category.LAUNCHER"/>
</Intent-filter>
</receiver>
啓動開機權限
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPELETED"/>
ok,上面就是關於android開機自啓動的,下面我也給大家講述一下Qt開機自啓動的方法吧!
Qt中主要是用到QSetting這個類來進行開機自啓動的處理,要詳細查看的話就去看幫助文檔,再詳細不過,這裏不講述。
這裏寫一個函數來,參數是bool型的,true就自啓動,false不自啓動
直接上代碼:
#define path "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run" //這是註冊表的地址
void autoStart(bool isStart)
{
QString app_name = QApplication::applicationName();
QSettings setting = new QSettting(path,Qsettings::NativeFormat);
if(isStart)
{
QString app_path = QApplication::applicationPath();
setting->setValue(app_name,app_path.replace("/","\\"));
}
else
{setting->remove(app_name);}
}