[英]is there a way to monitor application in background & invisible from user
我要做的是創建一個無需用戶干預即可執行其功能的應用程序。 設備的“應用程序”頁面上不應包含任何appicon。 安裝后,用戶無需知道設備中正在運行的應用程序。 我嘗試在演示應用程序中使用“沒有啟動器活動”,但是它沒有運行應用程序的代碼,這很明顯。 有沒有辦法完成這項任務,這有意義嗎?
是的,這是可能的,而且很有意義。 但是,例如,這需要做很多事情。
1)。 您需要將應用程序設置為啟動啟動方式,這意味着只要用戶重新啟動移動設備或設備,您的應用程序就會自動啟動。
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name=".OnBootReceiver" >
<intent-filter
android:enabled="true"
android:exported="false" >
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
<receiver android:name=".OnGPSReceiver" >
</receiver>
2)。 顯然,你必須讓應用程序沒有啟動模式,因為它的第一個活動,然后調用次活動不作為活動相關的服務。
因此,基本上,您必須創建類似這樣的內容。
public class AppService extends WakefulIntentService{
// your stuff goes here
}
在從mainActivity調用服務時,應按以下方式定義它。
Intent intent = new Intent(MainActivity.this, AppService.class);
startService(intent);
hideApp(getApplicationContext().getPackageName());
hideApp //用它在MainActivity之外。
private void hideApp(String appPackage) {
ComponentName componentName = new ComponentName(appPackage, appPackage
+ ".MainActivity");
getPackageManager().setComponentEnabledSetting(componentName,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
}
3)。 然后在清單中定義此服務,如下所示。
<service android:name=".AppService" >
</service>
編輯
WakefulIntentService
是一個新的抽象類。 請在下面檢查。 因此,創建一個新的Java文件並將beloe代碼粘貼到其中。
abstract public class WakefulIntentService extends IntentService {
abstract void doWakefulWork(Intent intent);
public static final String LOCK_NAME_STATIC = "test.AppService.Static";
private static PowerManager.WakeLock lockStatic = null;
public static void acquireStaticLock(Context context) {
getLock(context).acquire();
}
synchronized private static PowerManager.WakeLock getLock(Context context) {
if (lockStatic == null) {
PowerManager mgr = (PowerManager) context
.getSystemService(Context.POWER_SERVICE);
lockStatic = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
LOCK_NAME_STATIC);
lockStatic.setReferenceCounted(true);
}
return (lockStatic);
}
public WakefulIntentService(String name) {
super(name);
}
@Override
final protected void onHandleIntent(Intent intent) {
doWakefulWork(intent);
//getLock(this).release();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.