Restart the service even if app is force-stopped and Keep running service in background even after closing the app How?

前端 未结 8 574
陌清茗
陌清茗 2020-11-30 02:24

I am trying to run a service in the background. What my app does is when user checks checkbox then service starts and when it is unchecked service is stopped. Which is worki

8条回答
  •  眼角桃花
    2020-11-30 02:55

    use my method if you want to start a hidden app for just first time I make a transparent Launcher activity like this

    
        
            
                
        
    
    

    So I make the app hidden in launcher by placing this code in oncreat() [Code]

    PackageManager p = getPackageManager();
        ComponentName componentName = new ComponentName(this, MainActivity.class); // activity which is first time open in manifiest file which is declare as 
    p.setComponentEnabledSetting(componentName,PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
    

    So I use this code for show app icon on launcher and make it run able on service class that use broadcast receiver boot and in network connection broadcast receiver class too(autostart.java and networkConnectinCheck.java):

    PackageManager p = context.getPackageManager();
        ComponentName componentName = new ComponentName(context, MainActivity.class);
        p.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
    

    Now I can run app for first time by user hands and after this I use my receiver's to lunch app any time.

提交回复
热议问题