package demo.qihua.dt;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.os.PowerManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.Toast;
public class DianTongActivity extends Activity
{
private boolean ifLocked = false;
private PowerManager.WakeLock mWakeLock;
private PowerManager mPowerManager;
private LinearLayout mLinearLayout;
static final private int M_CHOOSE = Menu.FIRST;
static final private int M_EXIT = Menu.FIRST+1;
private int[] color={R.drawable.white,R.drawable.blue,
R.drawable.pink,R.drawable.green,
R.drawable.orange,R.drawable.yellow};
private int[] text={R.string.str_white,R.string.str_blue,
R.string.str_pink,R.string.str_green,
R.string.str_orange,R.string.str_yellow};
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags
(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN
);
setContentView(R.layout.main);
mLinearLayout=(LinearLayout)findViewById(R.id.myLinearLayout1);
mPowerManager = (PowerManager)
getSystemService(Context.POWER_SERVICE);
mWakeLock = mPowerManager.newWakeLock
(
PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "BackLight"
);
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = 1.0f;
getWindow().setAttributes(lp);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
int idGroup1 = 0;
int orderMenuItem1 = Menu.NONE;
int orderMenuItem2 = Menu.NONE+1;
menu.add(idGroup1,M_CHOOSE,orderMenuItem1,R.string.str_title);
menu.add(idGroup1,M_EXIT,orderMenuItem2,R.string.str_exit);
menu.setGroupCheckable(idGroup1, true, true);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch(item.getItemId())
{
case (M_CHOOSE):
new AlertDialog.Builder(DianTongActivity.this)
.setTitle(getResources().getString(R.string.str_title))
.setAdapter(new MyAdapter(this,color,text),listener1)
.setPositiveButton("先不选,取消",
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
}
})
.show();
break;
case (M_EXIT):
this.finish();
break;
}
return super.onOptionsItemSelected(item);
}
OnClickListener listener1=new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog,int which)
{
mLinearLayout.setBackgroundResource(color[which]);
Toast.makeText(DianTongActivity.this,
getResources().getString(text[which]),
Toast.LENGTH_LONG).show();
}
};
@Override
protected void onResume()
{
wakeLock();
super.onResume();
}
@Override
protected void onPause()
{
wakeUnlock();
super.onPause();
}
private void wakeLock()
{
if (!ifLocked)
{
ifLocked = true;
mWakeLock.acquire();
}
}
private void wakeUnlock()
{
if (ifLocked)
{
mWakeLock.release();
ifLocked = false;
}
}
}