4

デバイスの明るさを制御できる Andorid 設定ウィジェットを模倣しようとしています。意図したとおりに機能し、設定で確認できます。しかし、デバイスが明るくなることはありません。

コードは次のとおりです。

WindowManager.LayoutParams lp = getWindow().getAttributes();
    SeekBar brightnessControl     = (SeekBar) findViewById(R.id.sbBrightness);
    int currentBrightness         = 10;

    brightnessControl.setProgress(currentBrightness);
    lp.screenBrightness = currentBrightness/100f;

    brightnessControl.setOnSeekBarChangeListener(new OnSeekBarChangeListener() 
    {

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {}

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {}

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) 
        {
            //set local brightness
            WindowManager.LayoutParams lp   = getWindow().getAttributes();
            progress                        = (progress <= 10)?10:progress;
            lp.screenBrightness             = progress/100f;
            getWindow().setAttributes(lp);

            //put local to system wide brightness
            int sysWideBrightness           = (int) (progress/100f * 255);
            android.provider.Settings.System.putInt(
                    getContentResolver(),
                    android.provider.Settings.System.SCREEN_BRIGHTNESS,
                    sysWideBrightness);
        }
    });

このアクティビティは [アクティビティ] タブで実行されます。タブに入れる前は正常に動作しますが、そうではありません。

デバイスに新しいバージョンの Android (現在は 4.2.2 がルート化されています) をインストールすると、意図したとおりに動作することがわかりました。ルート化されたジンジャーブレッド デバイスでテストしましたが、動作しません。

これが役立つ場合があります。これは Main.java ファイルで、ActivityTab を開始する方法は次のとおりです。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    /* TabHost will have Tabs */
    tabHost = (TabHost)findViewById(android.R.id.tabhost);

    TabSpec firstTabSpec    = tabHost.newTabSpec("tid1");
    TabSpec secondTabSpec   = tabHost.newTabSpec("tid2");
    TabSpec thirdTabSpec    = tabHost.newTabSpec("tid3");
    TabSpec fourthTabSpec   = tabHost.newTabSpec("tid4");
    TabSpec fifthTabSpec    = tabHost.newTabSpec("tid5");

    firstTabSpec.setIndicator("", getResources().getDrawable(R.drawable.clear_cache_tab)).setContent(showClearCache());
    secondTabSpec.setIndicator("", getResources().getDrawable(R.drawable.move_sd_tab)).setContent(showMoveToSd());
    thirdTabSpec.setIndicator("", getResources().getDrawable(R.drawable.remove_app_tab)).setContent(showRemoveApp());
    fourthTabSpec.setIndicator("", getResources().getDrawable(R.drawable.feature_manager_tab)).setContent(showFeatureManager());
    fifthTabSpec.setIndicator("", getResources().getDrawable(R.drawable.feature_manager_tab)).setContent(showProcessKill());

    tabHost.setOnTabChangedListener(this);
    tabHost.addTab(firstTabSpec);
    tabHost.addTab(secondTabSpec);
    tabHost.addTab(thirdTabSpec);
    tabHost.addTab(fourthTabSpec);
    tabHost.addTab(fifthTabSpec);

    for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
    {
        tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#303030"));
    }

    tabHost.getTabWidget().setCurrentTab(0);
    tabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.parseColor("#C8C8C8"));
}

@Override
public void onTabChanged(String tabId) {
    // TODO Auto-generated method stub
    for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
    {
        tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#303030"));
    } 

    tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#C8C8C8"));
}
4

3 に答える 3

0

NightSky の答えはほぼ正しいですが、それがうまくいかない理由はわかっていると思います。確かに、明るさの変化を処理して更新する透明な「ダミー」アクティビティを作成する必要があります。これが SeekBar でどのように機能するかはわかりませんが、ウィンドウを更新する唯一の方法だと思います。

次のコードを透明なアクティビティの onCreate() に配置します (現在のアクティビティではなく、ダミーのアクティビティに):-

WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = brightness; //Use your brightness value or an arbitrary one
getWindow().setAttributes(lp);

/*Here is the real difference. You need to allow some time for you screen to be
refreshed before you call finish() on your activity. This means we should make a
thread that calls finish() after a small amount of time, say 300ms (You can use a 
different value if you want). */

new Handler().postDelayed(new Runnable() {

    @Override
    public void run() {
        finish();
    }

}, 300);

これは過去に私のために働いていました。先に述べたように、値を変更すると明るさが連続的に変化する SeekBar を使用できるようにする方法はないと思います。画面をリフレッシュする必要があります。

于 2013-08-07T11:17:06.540 に答える