2016-02-24 88 views
7

我正在開發Android 4.0+的鎖定屏幕。我正在使用將屏幕關閉的接收器註冊的服務。該接收器啓動onReceived活動。從屏幕上的接收器延遲開始活動關閉

問題是這整個過程不夠快。接收器有一個小的延遲,但真正的問題是活動的啓動,需要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); 
    } 
} 

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只是一個空活動,用一個簡單的佈局。還沒有動作。

任何想法?

+0

您試過多少個設備? – Elltz

+0

只是一個。 nexus 6 – jonyjm

+0

您的意思是說,您的活動顯示在默認鎖定屏幕下方? – Dinash

回答

2

看看你的IntentFilter,在這裏你可以設置優先級:

intentFilter.setPriority(9999); 

現在看documentation關於intentfilters:

值必須爲整數,如 「100」。較高的數字具有較高的優先級 。默認值爲0.該值必須大於 大於-1000且小於1000

+0

我已將優先級更改爲999,但行爲相同。 – jonyjm