Delay starting activity from receiver on screen off
<p>I'm developing a lock screen for Android 4.0+. I'm using a service that registers a receiver for screen OFF. This receiver launches an activity onReceived.</p>

<p>The problem is that this whole process is not fast enough. The receiver has a minor delay, but the real problem is the launch of the activity, that takes like 3-4 seconds.</p>

<p>I've seen similar apps, like:
<a href="https://github.com/Pi-Developers/Pi-Locker" rel="nofollow">https://github.com/Pi-Developers/Pi-Locker</a>.
In this case, everything works perfectly, but i can't figure out what I'm doing differently.</p>

<p><strong>Manifest</strong></p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="xxxxx" &gt;


    &lt;application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" &gt;

        &lt;activity
            android:name=".MainActivity"
            android:screenOrientation="portrait"
            android:label="@string/app_name" &gt;
            &lt;intent-filter&gt;
                &lt;action android:name="android.intent.action.MAIN" /&gt;
                &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
            &lt;/intent-filter&gt;
        &lt;/activity&gt;

        &lt;activity
            android:name=".LockScreenActivity"
            android:screenOrientation="portrait"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"
            android:excludeFromRecents="true"
            android:windowSoftInputMode="stateAlwaysHidden" &gt;
            &lt;intent-filter&gt;
                &lt;category android:name="android.intent.category.HOME" /&gt;
            &lt;/intent-filter&gt; &gt;
        &lt;/activity&gt;

        &lt;receiver
            android:name=".LockBoot"
            android:enabled="true" &gt;
            &lt;intent-filter&gt;
                &lt;action android:name="android.intent.action.BOOT_COMPLETED" /&gt;
            &lt;/intent-filter&gt;
        &lt;/receiver&gt;
        &lt;receiver
            android:name=".LockReceiver"
            android:enabled="true" &gt;
        &lt;/receiver&gt;

        &lt;service
            android:name=".LockerService"
            android:icon="@drawable/ic_launcher"
            android:process=":background" &gt;
        &lt;/service&gt;

    &lt;/application&gt;

    &lt;uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/&gt;
    &lt;uses-permission android:name="android.permission.DISABLE_KEYGUARD" /&gt;
    &lt;uses-permission android:name="android.permission.WAKE_LOCK" /&gt;
    &lt;uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /&gt;
    &lt;uses-permission android:name="android.permission.READ_PHONE_STATE"/&gt;
    &lt;uses-permission android:name="android.permission.WRITE_SETTINGS"/&gt;

&lt;/manifest&gt;
</code></pre>

<p><strong>Main Activity</strong></p>

<pre><code>public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent i = new Intent(MainActivity.this, LockerService.class);
        startService(i);

        setContentView(R.layout.activity_main);
    }
}
</code></pre>

<p><strong>LockerService</strong></p>

<pre><code>public class LockerService extends Service {


    static SharedPreferences spf;
    static Context c;
    int mActive;
    String on;
    LockReceiver mReceiver = new LockReceiver();


    @Override
    public IBinder onBind(Intent arg0) {

        return null;

    }

   @Override
   public int onStartCommand(Intent intent, int flags, int startId) {

            return START_STICKY;
        }


    /**
     * 
     * mStaus is Variable of int we need to find status of locker to make it
     * enable or disable 
     * 
     **/

    @Override
    public void onCreate() {

        mActive = 1; //todo

        if (mActive == 0) {

            stopSelf();
            return;

        }

        if (mActive == 1) {

            try {

            IntentFilter intentFilter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
                intentFilter.setPriority(9999);

            registerReceiver(mReceiver, intentFilter);


            } catch (Exception e) {

                unregisterReceiver(mReceiver);

            }

        } else  {

            try {
                unregisterReceiver(mReceiver);
            } catch(Exception e){

                e.printStackTrace();

            }

        }
    }

    @Override
    public void onDestroy() {

        super.onDestroy();


        /**
         * 
         * make sure service still running 
         *
         */

            startService(new Intent(LockerService.this , LockerService.class));


    }

}
</code></pre>

<p><strong>LockReceiver</strong></p>

<pre><code>public class LockReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {


        TelephonyManager ts = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

        int callState = ts.getCallState();

        if (callState == TelephonyManager.CALL_STATE_IDLE) {

            context.startActivity(new Intent(context, LockScreenActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));

        }

    }
}
</code></pre>

<p>LockScreenActivity is just an empty activity, with a simple layout. No actions yet.</p>

<p>Any ideas?</p>