有没有办法在后台监控应用程序?用户看不见

时间:2014-03-04 10:10:08

标签: java android service background spy

我想要做的是创建一个可以在没有用户交互的情况下执行其功能的应用程序。这不应该在Device中的Applications页面上有任何appicon。安装后,用户无需知道设备中运行的应用程序。我在演示应用程序中尝试使用No Launcher Activity但它没有运行应用程序代码,这很明显。有没有办法完成这项任务,这有​​什么意义吗?

1 个答案:

答案 0 :(得分:5)

是的,这是可能的,而且很有意义。但是,例如,需要做很多事情。

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();
    }
}