1

I work on app that acts like phone guard and should run on startup (or when launched by user) and keep running until user manually won't finish it. When application started (after device boot completed) i use moveTaskToBack for hiding it in background. After about ~12 seconds my application stop working (killed by system i suspect) without any notice, no logs at all (but still stay in history stack). Checked by app timer with log, and also when i start programm by clicking icon - new instance runs. As i noticed, if i execute moveTaskToBack from Handler which delayed even by 500ms - app won't be killed! Tested on galaxy tab 2 (4.1.2) and on alcatel one touch (2.3.6). Here the sample code for reproduce:

MainActivity

public class MainActivity extends Activity
{
    Timer timerCheck;
    int ticks = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        timerCheck = new Timer();
        timerCheck.schedule(taskCheck, 0, 1000);

        if (IsStartup())
            moveTaskToBack(true);

//      if (IsStartup())
//      {
//          new Handler().postDelayed(new Runnable()
//          {
//              @Override
//              public void run()
//              {
//                  moveTaskToBack(true);
//              }
//          }, 1000);
//      }
    }

    TimerTask taskCheck = new TimerTask()
    {
        @Override
        public void run()
        {
            runOnUiThread(timerTickCheck);
        }

        private Runnable timerTickCheck = new Runnable()
        {
            public void run()
            {
                Log.e("testapp", "alive for " + ++ticks * 1000 + " ms");
            }
        };
    };

    private boolean IsStartup()
    {
        return getIntent().hasExtra("startup");
    }
}

StartupReceiver

public class StartupReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context c, Intent i)
    {
        Intent in = new Intent();
        in.setClassName("com.example.startuptest",
                "com.example.startuptest.MainActivity");
        in.putExtra("startup", "1");
        in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        c.startActivity(in);
    }
}

manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.startuptest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

        <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >


        <receiver android:name="com.example.startuptest.StartupReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

        <activity
            android:name="com.example.startuptest.MainActivity"
            android:label="@string/app_name"
            android:launchMode="singleTop" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

So there is my questions - why android system has such behavior? Anyway to instantly hide app on startup?

Maxdestroyer
  • 185
  • 4
  • 12

1 Answers1

0

The android OS can kill any background process if it needs it's resources. In your case, the system boot is a highly resource-consuming period, and activities in background have quite low priority when it comes to what to keep. Anyway, if you want something to run in background for a prolonged time, I suggest you to check out services: http://developer.android.com/guide/components/services.html

Tamas
  • 221
  • 1
  • 8