我正在尝试创建一个允许用户通过SeekBar调整屏幕亮度的应用。
SeekBar是RelativeLayout视图的子视图,当按下按钮时,所有视图都覆盖父视图。因此,只有在按下按钮时才会出现SeekBar。
我在线查看了各种教程,然后按this one,但是当我运行应用程序并移动滑块时,屏幕亮度不会改变。什么都没有变得更暗或更亮。
有谁知道我哪里出错了?任何帮助,将不胜感激!另外,有没有办法在不需要写权限的情况下这样做?或者有没有办法调整叠加视图的亮度?
编辑:有关错误日志和测试的评论可以在代码下方找到。
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.screenbrightness">
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:paddingBottom="0px"
android:paddingLeft="0px"
android:paddingRight="0px"
android:paddingTop="0px"
tools:context="com.example.android.screenBrightness.MainActivity">
<ImageButton
android:id="@+id/btnFront"
android.contentDescription="@null"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:src="@drawable/front" />
<RelativeLayout
android:id="@+id/overlay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="0.95"
android:background="#ffffff"
android:visibility="gone">
<TextView
android:id="@+id/brightnessLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Brightness"
android:textColor="#5c5c5c"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="20dp"
android:layout_marginLeft="10dp"
android:textSize="17sp" />
<SeekBar
android:id="@+id/brightnessBar"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:max="100"
android:progress="0"
android:layout_marginBottom="20dp"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@+id/brightnessLabel"/>
</RelativeLayout></RelativeLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
boolean isFrontOn;
ImageButton btnFront;
SeekBar brightnessBar;
float brightness;
ContentResolver cResolver;
RelativeLayout overlayLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnFront = (ImageButton) findViewById(R.id.btnFront);
brightnessBar = (SeekBar) findViewById(R.id.brightnessBar);
cResolver = getContentResolver();
brightnessBar.setMax(255);
brightnessBar.setKeyProgressIncrement(1);
btnFront.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!isFrontOn)
turnFrontOn();
else
turnFrontOff();
}
});
try
{
brightness = Settings.System.getInt(cResolver, Settings.System.SCREEN_BRIGHTNESS);
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
int screenBrightness = (int) brightness;
brightnessBar.setProgress(screenBrightness);
brightnessBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
int progressVal = 0;
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (progress <= 20)
progressVal = 20;
else
progressVal = progress;
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
Settings.System.putInt(cResolver, Settings.System.SCREEN_BRIGHTNESS, progressVal);
}
});
}
}
编辑:此延续是对@CyprienAubry关于错误日志的问题的回应。
我正在模拟器上测试应用程序和我的Samsung Note 3(它无法与Android Studio一起使用,因此我必须传输apk并手动安装)。
在三星Note 3上,它在安装过程中要求摄像头和写入权限,当我移动亮度滑块时没有变化,我看不清或者不知道如何查看自手机以来是否有任何错误不适用于Android Studio。
使用模拟器,当我单击按钮以显示叠加层时,会出现以下错误:
07-31 00:37:36.158 2580-2690/com.example.android.screenbrightness E/Surface: getSlotFromBufferLocked: unknown buffer: 0xae471700
07-31 00:37:38.167 2580-2690/com.example.android.screenbrightness E/Surface: getSlotFromBufferLocked: unknown buffer: 0xaa0ce310
当我尝试移动亮度滑块时,应用程序崩溃并出现以下错误。我将它们归因于无法在模拟器上获得写入权限,所以我在Samsung Note上的测试也是如此。但是西普里安的问题促使我在我的Nexus 7上测试应用程序,我将在下面讨论。目前,使用模拟器的错误:
07-31 00:39:11.414 2580-2580/com.example.android.screenbrightness E/InputEventReceiver: Exception dispatching input event.
07-31 00:39:11.414 2580-2580/com.example.android.screenbrightness E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
07-31 00:39:11.417 2580-2580/com.example.android.screenbrightness E/MessageQueue-JNI: java.lang.SecurityException: com.example.android.screenbrightness was not granted this permission: android.permission.WRITE_SETTINGS.
at android.os.Parcel.readException(Parcel.java:1599)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
at android.content.ContentProviderProxy.call(ContentProviderNative.java:646)
at android.provider.Settings$NameValueCache.putStringForUser(Settings.java:1322)
at android.provider.Settings$System.putStringForUser(Settings.java:1671)
at android.provider.Settings$System.putIntForUser(Settings.java:1776)
at android.provider.Settings$System.putInt(Settings.java:1770)
at com.example.android.screenbrightness.MainActivity$4.onStopTrackingTouch(MainActivity.java:108)
at android.widget.SeekBar.onStopTrackingTouch(SeekBar.java:121)
at android.widget.AbsSeekBar.onTouchEvent(AbsSeekBar.java:604)
at android.view.View.dispatchTouchEvent(View.java:9294)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at com.android.internal.policy.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2403)
at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1737)
at android.app.Activity.dispatchTouchEvent(Activity.java:2765)
at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
at com.android.internal.policy.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2364)
at android.view.View.dispatchPointerEvent(View.java:9514)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4230)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4096)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3787)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3669)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3844)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3669)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5922)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5896)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5857)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6025)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:323)
at android.os.Looper.loop(L
07-31 00:39:11.417 2580-2580/com.example.android.screenbrightness D/AndroidRuntime: Shutting down VM
--------- beginning of crash
07-31 00:39:11.418 2580-2580/com.example.android.screenbrightness E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.lights, PID: 2580
java.lang.SecurityException: com.example.android.screenbrightness was not granted this permission: android.permission.WRITE_SETTINGS.
at android.os.Parcel.readException(Parcel.java:1599)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
at android.content.ContentProviderProxy.call(ContentProviderNative.java:646)
at android.provider.Settings$NameValueCache.putStringForUser(Settings.java:1322)
at android.provider.Settings$System.putStringForUser(Settings.java:1671)
at android.provider.Settings$System.putIntForUser(Settings.java:1776)
at android.provider.Settings$System.putInt(Settings.java:1770)
at com.example.android.screenbrightness.MainActivity$4.onStopTrackingTouch(MainActivity.java:108)
at android.widget.SeekBar.onStopTrackingTouch(SeekBar.java:121)
at android.widget.AbsSeekBar.onTouchEvent(AbsSeekBar.java:604)
at android.view.View.dispatchTouchEvent(View.java:9294)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at com.android.internal.policy.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2403)
at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1737)
at android.app.Activity.dispatchTouchEvent(Activity.java:2765)
at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
at com.android.internal.policy.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2364)
at android.view.View.dispatchPointerEvent(View.java:9514)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4230)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4096)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3787)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3669)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3844)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3669)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5922)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5896)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5857)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6025)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.Me
所以在Nexus 7上,当我将它连接到我的计算机并通过Android Studio运行应用程序时,当我点击覆盖按钮时,没有错误。
当我尝试移动亮度滑块时,应用程序崩溃并产生与模拟器相同的错误,即:
07-31 08:56:28.260 14030-14030/com.example.android.screenbrightness E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.screenbrightness, PID: 14030
java.lang.SecurityException: com.example.android.screenbrightness was not granted this permission: android.permission.WRITE_SETTINGS.
但与三星Note相反,当我将apk转移到Nexus 7并尝试手动安装时,该应用程序说它不需要任何特殊访问,并且当我尝试移动亮度时仍然崩溃滑块。