-1
我想要做的是創建一個應用程序,它可以在沒有用戶交互的情況下執行它的功能。這不應該在設備中的應用程序頁面上有任何appicon。安裝後用戶不需要知道設備中運行的應用程序。我在演示應用程序中嘗試使用No Launcher Activity,但它沒有運行應用程序代碼,這很明顯。有沒有辦法完成這個任務,這是否有意義?有沒有辦法監控後臺應用程序和用戶不可見
我想要做的是創建一個應用程序,它可以在沒有用戶交互的情況下執行它的功能。這不應該在設備中的應用程序頁面上有任何appicon。安裝後用戶不需要知道設備中運行的應用程序。我在演示應用程序中嘗試使用No Launcher Activity,但它沒有運行應用程序代碼,這很明顯。有沒有辦法完成這個任務,這是否有意義?有沒有辦法監控後臺應用程序和用戶不可見
是的,這是可能的,它很有意義。但是,例如,它需要很多東西來完成。
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();
}
}
我在嘗試,但'WakefulIntentService'在我的演示程序中未解決。你能否定義如何引用它。 –
@SureshSharma,檢查我的編輯。 – InnocentKiller
hideApp也未在MainActivity中解析,並且關於MainActivity將如何在清單中聲明。 –