7

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.

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.

I've seen similar apps, like: https://github.com/Pi-Developers/Pi-Locker. In this case, everything works perfectly, but i can't figure out what I'm doing differently.

Manifest

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


    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >

        <activity
            android:name=".MainActivity"
            android:screenOrientation="portrait"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <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" >
            <intent-filter>
                <category android:name="android.intent.category.HOME" />
            </intent-filter> >
        </activity>

        <receiver
            android:name=".LockBoot"
            android:enabled="true" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
        <receiver
            android:name=".LockReceiver"
            android:enabled="true" >
        </receiver>

        <service
            android:name=".LockerService"
            android:icon="@drawable/ic_launcher"
            android:process=":background" >
        </service>

    </application>

    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
    <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.WRITE_SETTINGS"/>

</manifest>

Main Activity

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

LockerService

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


    }

}

LockReceiver

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

        }

    }
}

LockScreenActivity is just an empty activity, with a simple layout. No actions yet.

Any ideas?

4
  • how many devices you tried on?
    – Elltz
    Mar 10, 2016 at 16:59
  • Did you mean to say, your activity is shown below the default lock screen?
    – Dinash
    Mar 16, 2016 at 12:47
  • No, i've removed the default lock screen. The point is that the activity is not ready when you turn off and turn on the screen quickly. You have to wait 3-4 seconds for it to appear.
    – jonyjm
    Mar 16, 2016 at 14:03
  • @jonyjm, please log all scenario, check which section takes time delay. Mar 17, 2016 at 12:34

1 Answer 1

3

Look at your IntentFilter, where you set the priority:

intentFilter.setPriority(9999);

Now look at the documentation regarding intentfilters:

The value must be an integer, such as "100". Higher numbers have a higher priority. The default value is 0. The value must be greater than -1000 and less than 1000

2
  • I've changed the priority to 999, but the behavior is the same.
    – jonyjm
    Mar 17, 2016 at 14:57
  • I'd replace 9999 with IntentFilter.SYSTEM_HIGH_PRIORITY - 1 to be more accurate Jul 25, 2022 at 0:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.