从屏幕关闭时延迟启动接收器活动。

7
我正在开发一个适用于Android 4.0+的锁屏应用。我使用了一个服务来注册一个监听器以便在屏幕关闭时启动一个活动。但整个过程不够快,监听器有轻微延迟,而真正的问题在于活动的启动需要3-4秒钟。
我已经看到了类似的应用程序,比如: https://github.com/Pi-Developers/Pi-Locker。 在这种情况下,一切都运行得很完美,但我无法弄清楚我做错了什么。 清单
<?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>

主活动

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

锁定服务

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


    }

}

锁屏接收器

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是一个空的活动,带有简单的布局。目前还没有任何操作。

有什么想法吗?


你尝试过多少台设备? - Elltz
你的意思是说,你的活动显示在默认锁屏界面下方吗? - Dinash
不,我已经移除了默认的锁屏。问题在于当你快速关闭和打开屏幕时,该活动并没有准备好。你必须等待3-4秒钟才能看到它。 - jonyjm
@jonyjm,请记录所有场景,检查哪个部分需要延迟。 - Hiren Patel
1个回答

3

请查看您的IntentFilter,其中设置了优先级:

intentFilter.setPriority(9999);

现在看一下关于意图过滤器的文档

该值必须是一个整数,例如"100"。数字越大,优先级越高。默认值为0。该值必须大于-1000且小于1000。


我已将优先级更改为999,但行为仍然相同。 - jonyjm
我会将“9999”替换为“IntentFilter.SYSTEM_HIGH_PRIORITY - 1”,以获得更准确的结果。 - Amin Guermazi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接