有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java有没有一种方法可以在后台监控应用程序&用户看不见

我想做的是创建一个应用程序,它可以在没有用户交互的情况下执行其功能。在设备的应用程序页面上不应该有任何appicon。安装后,用户不需要知道应用程序正在设备中运行。我尝试在一个演示应用程序中没有启动程序活动,但它没有运行应用程序的代码,这是显而易见的。有没有办法完成这项任务,这有意义吗


共 (1) 个答案

  1. # 1 楼答案

    是的,这是可能的,而且很有意义。但它需要很多的东西去做,例如

    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//在主活动之外使用它

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