我正在研究如何讓我的Android應用程序在前臺運行。如何讓前臺應用程序全天候運行?
這將是一個私分的應用程序,所以我可以做任何事情可以確保它一直運行在設備上(HDMI電視棒)
那麼,如何才能確保應用程序保持運行,無論什麼?該應用程序在資源使用方面相當輕量,因此讓其全天候運行應該不會成爲問題。
我讀過清單中的持久參數,但看起來它可能只適用於系統應用程序?
我應該讓我的應用成爲系統應用嗎?我會怎麼做,並幫助?
我正在研究如何讓我的Android應用程序在前臺運行。如何讓前臺應用程序全天候運行?
這將是一個私分的應用程序,所以我可以做任何事情可以確保它一直運行在設備上(HDMI電視棒)
那麼,如何才能確保應用程序保持運行,無論什麼?該應用程序在資源使用方面相當輕量,因此讓其全天候運行應該不會成爲問題。
我讀過清單中的持久參數,但看起來它可能只適用於系統應用程序?
我應該讓我的應用成爲系統應用嗎?我會怎麼做,並幫助?
使用本:
import android.os.PowerManager;
public class MyActivity extends Activity {
protected PowerManager.WakeLock mWakeLock;
/** Called when the activity is first created. */
@Override
public void onCreate(final Bundle icicle) {
setContentView(R.layout.main);
/* This code together with the one in onDestroy()
* will make the screen be always on until this Activity gets destroyed. */
final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
this.mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
this.mWakeLock.acquire();
}
@Override
public void onDestroy() {
this.mWakeLock.release();
super.onDestroy();
}
}
而且在清單:
<uses-permission android:name="android.permission.WAKE_LOCK" />
這事不太容易實現的前景應用程序在技術上不應該不停地運行。此外,如果Android內存不足,它將開始殺死風險最小的應用程序,然後需要用戶重新啓動應用程序。
如前所述,您可以將其製作爲系統應用程序,但我認爲您確實需要根設備或建立自己的ROM並使您的應用程序成爲ROM的一部分。可能不是您的需求的最佳解決方案,但很少有人能夠將ROM刷到他們的設備上。
我認爲最簡單的解決方案是放入清單中,表明您的應用程序是主屏幕替代品,即啓動器應用程序。我不知道從頭開始的確切代碼,但是這會進入android清單中的應用程序部分。這意味着只要設備啓動,或用戶按下主頁按鈕,他們就會被帶到您的應用程序。
OP也可能想要記住的一點是,使用自定義啓動後可能會遇到問題(至少我做過)家庭更換。 [這是我的經驗和我如何解決它。](http://stackoverflow.com/questions/17372781/home-launcher-issue-with-fragments-after-reboot) – TronicZomB
如果你想要一個外部應用程序使用:Autostart and StaY!
如果你想這樣做編程,您可以使用一個服務,調查每一個「X」毫秒,看看你的應用程序是在前臺。如果不是,它會啓動/將您的應用程序放在前臺。像這樣做:
public class PersistService extends Service {
private static final int INTERVAL = 3000; // poll every 3 secs
private static final string YOUR_APP_PACKAGE_NAME = "YOUR_APP_PACKAGE_NAME";
private static boolean stopTask;
private PowerManager.WakeLock mWakeLock;
@Override
public void onCreate() {
super.onCreate();
stopTask = false;
// Optional: Screen Always On Mode!
// Screen will never switch off this way
mWakeLock = null;
if (settings.pmode_scrn_on){
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "a_tag");
mWakeLock.acquire();
}
// Start your (polling) task
TimerTask task = new TimerTask() {
@Override
public void run() {
// If you wish to stop the task/polling
if (stopTask){
this.cancel();
}
// The first in the list of RunningTasks is always the foreground task.
RunningTaskInfo foregroundTaskInfo = activityManager.getRunningTasks(1).get(0);
String foregroundTaskPackageName = foregroundTaskInfo .topActivity.getPackageName();
// Check foreground app: If it is not in the foreground... bring it!
if (!foregroundTaskPackageName.equals(YOUR_APP_PACKAGE_NAME)){
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage(YOUR_APP_PACKAGE_NAME);
startActivity(LaunchIntent);
}
}
};
Timer timer = new Timer();
timer.scheduleAtFixedRate(task, 0, INTERVAL);
}
@Override
public void onDestroy(){
stopTask = true;
if (mWakeLock != null)
mWakeLock.release();
super.onDestroy();
}
}
上面的代碼也已經在「選項」,強制屏幕保持始終!當然,你需要以下權限:
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
,不也忘記註冊您的服務:
<service android:name="YOURPACAKGE.PersistService"
android:enabled="true"/>
你可以讓你的應用程序成爲了啓動程序,加入2個以下類別標記爲<intent-filter>
標籤:
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.HOME"/>
那麼你應該經常檢查是否有在上面的另一個應用程序運行,運行下面的代碼以直接用戶到我們的應用程序:
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
我已經試過這個解決方案,但它不能隱藏在頂部繪製的應用程序,如Facebook Messenger聊天頭。
我通過運行一個粘性服務來解決該問題,該服務在活動關閉時重新啓動應用程序。
//Your activity
@Override
public void onPause() {
super.onPause();
if (yourservice != null) {
yourservice.validateActivityOnPause();
}
}
,並在validateActivityOnPause()有類似:
//Your service
public void validateLynxActivityOnPause() {
//Do some stuff here
Intent startActivityIntent = new Intent(this, LynxActivity.class);
startActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(startActivityIntent);
}
我相信你只能讓你應用程式的系統應用程序,如果該設備是植根。您可能需要考慮讓您的應用成爲主屏幕替代品,例如自助服務終端模式應用。看到我的答案[這裏](http://stackoverflow.com/questions/16657300/disable-all-home-button-and-task-bar-features-on-nexus-7/16657359#16657359)。 – TronicZomB