2

I recently started Android coding and wanted to create a little program for changing the screen brightness... Well.. i know there are already some questions about it, but i tried everything suggested here and really dunno how i can solve my problem :) I understood that you have to "refresh" the screen after setting brightness. And at this point my problem starts... I've created some kind of dummy activity and also have an intent in my main activity, but it seems like the intent dont sart the dummy activity... Heres the relevant part of my main activity:

button1.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Settings.System.putInt(getContentResolver(),
            Settings.System.SCREEN_BRIGHTNESS, 255);

            Intent in = new Intent(Test.this,DummyBrightnessActivity.class); //it is working...
            startActivity(in); //it is working...
        }

and the dummy code:

public class DummyBrightnessActivity extends Activity{
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    this.finish();

}

}

the manifest.xml:

    <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".Test"
        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="com.Test.DummyBrightnessActivity"
        android:taskAffinity="com.Test.Dummy"
        android:excludeFromRecents="true"
        android:theme="@style/EmptyActivity"></activity>
</application>

maybe relevant, the styles.xml:

<resources>

<style name="EmptyActivity" parent="android:Theme.Dialog">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Toast</item>
    <item name="android:background">#00000000</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:colorForeground">#000</item>
    <item name="android:backgroundDimEnabled">false</item>
    <item name="android:windowIsFloating">true</item>
</style>


</resources>

oh, btw... I dont get any errors... The dummy just wont start (I think so, because when i run it without android:excludeFromRecents="true" then it wont appear in the recent apps list.

I hope someone can help me... Have a nice day

//EDIT: Well... it looks like the code is working properly xD Just relooked and put some text instead of the "finish()" and the activity run properly... but i thought that the finish have to be there :/ Maybe you have any suggestions how to "reset" the screen instead? Looks like i understood one of the tutorials wrong...

//EDIT2: Well... I cant post an answer to my own question in the first 8hours:D So i post it in here:

Thank you all for the help and tips, but now i found the solution for myself :D this one: Refreshing the display from a widget? the part "kicking off an empty activity and executing the WindowManager refresh" is working for me. i came across this before asking here, but back then i just couldnt get it to work :D So, anyways, thank you very much ;) This was just an example of hard it can be to code "a little, fast-coded beginner app" ;)

Community
  • 1
  • 1
user1467583
  • 31
  • 1
  • 4
  • I don't have the answer, but since you seem like a new user I thought I would at lest give you an alternative resource http://android.stackexchange.com/ – wavedrop Jun 19 '12 at 21:24

2 Answers2

0

You may also want to consider calling invalidate() on your View instead of launching and closing a new one.

I am not sure if it will work for this scenario, as I have not tried changing the screen brightness. However, as per http://developer.android.com/reference/android/view/View.html#invalidate(), calling invalidate() on a view will cause onDraw() to be called, which sounds like what you want.

Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120
  • hmm... sounds interesting.. but i dont get how to implement this... I cant get it to work with the OnClickListener.. Maybe you have any code example? :) – user1467583 Jun 19 '12 at 21:43
  • Let's say that your main View is `R.id.main`. You can call `findViewById(R.id.main).invalidate()` to invalidate that View, and force that View to call onDraw(). – Bryan Herbst Jun 19 '12 at 21:48
  • Hmm... Yes... that makes sense... :D Will try it... Hope it works.. Looks like a really easy solution... – user1467583 Jun 19 '12 at 21:51
  • Now it would be good to know, what the main view is... but actually i think this method will only force the view of the running activity to "reload"... but to apply the brightness change to the whole system, the whole screen have to be refreshed... – user1467583 Jun 19 '12 at 22:13
0

I think this should work. I use this when I am moving from one page to another. It simply identifies the class and starts the activity, running whatever your new activity is going to do.

public void onClick(View v) {
    // (IN THIS EXAMPLE) IDENTIFIES WHAT IS BEING PRESSED 
    if(v.getId() == R.id.activity) {
        // STARTS THE OTHER ACTIVITY
        Intent i = new Intent(this,Activity2.class);
        this.startActivity(i);
    }

Also, remove finish(); because your activity initializes and closes at the same time. Make sure that you set the content view (setContentView(R.layout.main);) so you can tell whether or not the activity you started actually brings you to the page you want.

EDIT: You say the button press isn't detected. Try implementing the OnClickListener directly to your activity android import it like this.

public class MyApplication extends Activity implements OnClickListener {

Also, make sure your button is properly initialized. Try removing this: button1.setOnClickListener(new OnClickListener(), and then add this to your code:

button1 = (Button) findViewById(R.id.activity);
button1.setOnClickListener(this);
Kurty
  • 475
  • 2
  • 16
  • this even doesnt recognize the button-press, i think. when i add the "Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, 255);" it doesnt change the build-in "Brightnessbar" of Android. With the other code it does. – user1467583 Jun 19 '12 at 22:19
  • as i said in the edited question above the problem isnt the start of the second activity, im very sorry for this :/ problem seems to be how to "refresh" the screen... – user1467583 Jun 19 '12 at 22:44
  • Try adding `finish();` right above `this.startActivity(i);` and see if it works. I also found several links that may have what you are looking for: http://stackoverflow.com/questions/3053761/reload-activity-in-android | http://stackoverflow.com/questions/8240934/android-refresh-activity-when-returns-to-it | http://stackoverflow.com/questions/6547969/android-refresh-current-activity | http://stackoverflow.com/questions/1397361/how-do-i-restart-an-android-activity – Kurty Jun 19 '12 at 22:51
  • This wasnt what i was looking for :D I need to refresh the screen of the whole phone, not just the view of the activity... but now i found the solution... (I will post it in a seperate answer) – user1467583 Jun 19 '12 at 23:02