2015-06-24 41 views
1

我正在嘗試使用主密鑰鎖https://github.com/shaobin0604/Android-HomeKey-Locker 我希望能夠檢測並阻止主屏按鈕單擊鎖定屏幕。大部分答案都提到它不能被禁用。獲取類型爲attr的錯誤預期資源

這是HomeKeyLocker類:​​

package com.example.harshilshah.screenonoff; 

    import android.app.Activity; 
    import android.app.AlertDialog; 
    import android.os.Bundle; 
    import android.view.Gravity; 
    import android.view.MotionEvent; 
    import android.view.WindowManager; 
    import android.widget.FrameLayout; 

    import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL; 
    import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED; 
    import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ERROR; 


public class HomeKeyLocker { 
private OverlayDialog mOverlayDialog; 

public void lock(Activity activity) { 
    if (mOverlayDialog == null) { 
     mOverlayDialog = new OverlayDialog(activity); 
     mOverlayDialog.show(); 
    } 
} 

public void unlock() { 
    if (mOverlayDialog != null) { 
     mOverlayDialog.dismiss(); 
     mOverlayDialog = null; 
    } 
} 

    public static class OverlayDialog extends AlertDialog { 

    public OverlayDialog(Activity activity) { 
     super(activity, R.style.OverlayDialog); 
     WindowManager.LayoutParams params = getWindow().getAttributes(); 
     params.type = TYPE_SYSTEM_ERROR; 
     params.dimAmount = 0.0F; // transparent 
     params.width = 0; 
     params.height = 0; 
     params.gravity = Gravity.BOTTOM; 
     getWindow().setAttributes(params); 
     getWindow().setFlags(FLAG_SHOW_WHEN_LOCKED | FLAG_NOT_TOUCH_MODAL, 0xffffff); 
     setOwnerActivity(activity); 
     setCancelable(false); 
    } 

    public final boolean dispatchTouchEvent(MotionEvent motionevent) { 
     return true; 
    } 

    protected final void onCreate(Bundle bundle) { 
     super.onCreate(bundle); 
     FrameLayout framelayout = new FrameLayout(getContext()); 
     framelayout.setBackgroundColor(0); 
     setContentView(framelayout); 
    } 
    } 
} 

這是styles.xml

<resources> 

<!-- Base application theme. --> 
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
    <!-- Customize your theme here. --> 
    <item name="windowActionBar">false</item> 
    <item name="android:windowNoTitle">true</item> 
</style> 

<style name="OverlayDialog" parent="@android:style/Theme.Dialog"> 
    <item name="android:windowBackground">@android:color/transparent</item> 
    <item name="android:windowFrame">@null</item> 
    <item name="android:windowNoTitle">true</item> 
    <item name="android:windowIsFloating">true</item> 
    <item name="android:windowIsTranslucent">false</item> 
    <item name="android:backgroundDimEnabled">false</item> 
</style> 

</resources> 

我收到類型attr的錯誤預期的資源在以下行

public OverlayDialog(Activity activity) { 
     super(activity, R.style.OverlayDialog);//ERROR 

我該如何解決這個問題?

回答

0

我剛剛遇到了這個問題。造成它的原因是我導入了android.R class,而不是已在我的應用中定義的R類。所以它沒有使用在我的應用程序的值中定義的styles.xml,OverlayDialog是實現的,但在Android系統中定義的標準顯然沒有。一旦我刪除import android.R並添加import com.example.myapp.R它工作得很好。