Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Android background service launches itself after closing UI

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 366
    Answer it

    There is a requirement to start the background service from the another application where the current services are only installed and not in running state. So I started the service from the another application by using the below code in MainActivity class in onCreate method :

    static Context mconrtext;
        static Context mpconrtext;
        @Override
        public void onCreate(Bundle savedInstanceState)
        {   
     int flag = 0;
        int flagM = 0;
        ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
        for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) 
        {
    
            if ("com.GeoFencing.android.androidservice.MyService".equals(service.service.getClassName())){                   
             flag = 1;
    
        } 
    
    
         if ("com.Misper.android.Notification.MyService".equals(service.service.getClassName())){                   
                flagM = 1;
    
        }
    
    
        try
        {
    
        if(flag == 0){
             this.mconrtext=this;
            Intent i = new Intent();
            i.setComponent(new ComponentName("com.GeoFencing.android.androidservice", "com.GeoFencing.android.androidservice.LaunchTracker"));
             mconrtext.startService(i);
        } 
        }
        catch(Exception exe)
        {
    
        } 
    
    
        try
        {
                if(flagM == 0){
                    mpconrtext=this;
                   Intent j = new Intent();
    
                   j.setComponent(new ComponentName("com.Misper.android.Notification", "com.Misper.android.Notification.LaunchMPush"));
    
    
                   mpconrtext.startService(j);
        }  
    
        }
                catch(Exception exe)
        {
    
    
        }
    }

    Updated AndroidManifest.xml by adding following code :

    <service android:enabled="true"
                android:exported = "true"
                android:name="com.GeoFencing.android.androidservice.LaunchTracker">
    
                 <intent-filter>
                <action android:name="com.GeoFencing.android.androidservice.LaunchTracker" />
            </intent-filter>
            </service>
    
          <service android:enabled="true"
                android:exported = "true"
                android:name="com.Misper.android.Notification.LaunchMPush">
    
                 <intent-filter>
                <action android:name="com.Misper.android.Notification.LaunchMPush" />
            </intent-filter>
            </service>

    In the Background Service for LaunchMpush class I added the following code

    import android.app.AlarmManager;
    import android.app.PendingIntent;
    import android.app.Service;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.os.IBinder;
    import android.widget.Toast;
    
    public class LaunchMPush extends Service {
        static int Timervalue=120;
        public  static Context mContex;
        static DisplayUiLisner displayLisner;
        private PendingIntent pendingIntent;
        private AlarmManager manager;
        static double course ;
        static int TimerIntervalVal=120;
        @Override
        public IBinder onBind(Intent intent) {
            // TODO: Return the communication channel to the service.
            throw new UnsupportedOperationException("Not yet implemented");
        }
    
    
        @Override
        public void onCreate() {
    
            mContex =this;
        }
     public static void setLisnerDisplay(DisplayUiLisner setDisplayLisner){
        displayLisner =setDisplayLisner;
     }
        @Override
        public void onStart(Intent intent, int startId) {
            Intent dialogIntent = new Intent(mContex,MainActivity.class);
            //dialogIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    
            startActivity(dialogIntent);
    
    
        }
    
        @Override
        public void onDestroy() {
            Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
    
        }
    }

    Now : 1. Application is launching the background services and services are being stared so there is no issue. 2. The issue is when the background services are launched and user close the UI of these background serivces then the UI of these services keep opening itslef. Please help me to resolve this issue.

 0 Answer(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: