简体   繁体   English

有没有办法在后台监视应用程序并且对用户不可见

[英]is there a way to monitor application in background & invisible from user

What I want to do is to create an application which can perform it's feature without user interaction. 我要做的是创建一个无需用户干预即可执行其功能的应用程序。 This shouldn't have any appicon at Applications page in Device. 设备的“应用程序”页面上不应包含任何appicon。 After installation user don't need to aware of application running in device. 安装后,用户无需知道设备中正在运行的应用程序。 I tried with No Launcher Activity in a Demo Application but it is not running code of application and that's obvious. 我尝试在演示应用程序中使用“没有启动器活动”,但是它没有运行应用程序的代码,这很明显。 Is there a way to accomplish this task, Does this make any sense? 有没有办法完成这项任务,这有​​意义吗?

Yes it is possible, and it makes lot of sense. 是的,这是可能的,而且很有意义。 But it takes lot's stuff to do, for example. 但是,例如,这需要做很多事情。

1). 1)。 You need to make your app as boot start-up means whenever user restart mobile or device your app should automatically start's. 您需要将应用程序设置为启动启动方式,这意味着只要用户重新启动移动设备或设备,您的应用程序就会自动启动。

 <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). 2)。 Obviously you have to make app with no launcher mode as it's first activity and then call second activity as a service not as an activity. 显然,你必须让应用程序没有启动模式,因为它的第一个活动,然后调用次活动不作为活动相关的服务。

so basically you have to create something like this. 因此,基本上,您必须创建类似这样的内容。

public class AppService extends WakefulIntentService{
       // your stuff goes here
}

and while calling service from your mainActivity define it like this. 在从mainActivity调用服务时,应按以下方式定义它。

Intent intent = new Intent(MainActivity.this, AppService.class);
startService(intent);
hideApp(getApplicationContext().getPackageName());

hideApp // use it outside the mainActivity. 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). 3)。 Then in manifest define this service as like below. 然后在清单中定义此服务,如下所示。

 <service android:name=".AppService" >
        </service>

Edit 编辑

WakefulIntentService is a new abstract class. WakefulIntentService是一个新的抽象类。 Please check below. 请在下面检查。 So create a new java file and paste the beloe code in it. 因此,创建一个新的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM