PowerManager使用实例1(上)

文件1 MainActivity.java:
 
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.PowerManager;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
 
public class MainActivity extends Activity implements OnClickListener{
    /** Called when the activity is first created. */
final static int kStateKey=R.id.StateKey;
final static int kTextTurnOnKey=R.id.TextTurnOnKey;
final static int kTextTurnOffKey=R.id.TextTurnOffKey;
final static String kStateOff="off";
final static String kStateOn="on";
PowerManager pm;
PowerManager.WakeLock wl;
final static String tag="robin"; 
Handler handler=new MyHandler();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button btn=null;
        Resources res=this.getResources();
        for(int i=R.id.Button0;i<=R.id.Button5;i++)
        {
         btn=(Button)findViewById(i);
         btn.setTag(kStateKey, kStateOff);
        if(i==R.id.Button0)
        {
         btn.setTag(kTextTurnOnKey,res.getString(R.string.acquire_PARTIAL));
         btn.setTag(kTextTurnOffKey,res.getString(R.string.release_PARTIAL));
        }
        else if(i==R.id.Button1)
        {
         btn.setTag(kTextTurnOnKey,res.getString(R.string.acquire_SCREEN_DIM));
         btn.setTag(kTextTurnOffKey,res.getString(R.string.release_SCREEN_DIM));
        }
        else if(i==R.id.Button2)
        {
         btn.setTag(kTextTurnOnKey,res.getString(R.string.acquire_SCREEN_BRIGHT));
         btn.setTag(kTextTurnOffKey,res.getString(R.string.release_SCREEN_BRIGHT));
        }
        else if(i==R.id.Button3)
        {
         btn.setTag(kTextTurnOnKey,res.getString(R.string.acquire_FULL_WAKE_LOCK));
         btn.setTag(kTextTurnOffKey,res.getString(R.string.release_FULL_WAKE_LOCK));
        }
        else if(i==R.id.Button4)
        {
         btn.setTag(kTextTurnOnKey,res.getString(R.string.combineFlag0));
         btn.setTag(kTextTurnOffKey,res.getString(R.string.notCombineFlag0));
        }
        else if(i==R.id.Button5)
        {
         btn.setTag(kTextTurnOnKey,res.getString(R.string.combineFlag1));
         btn.setTag(kTextTurnOffKey,res.getString(R.string.notCombineFlag1));
        }
        btn.setText((String)btn.getTag(kTextTurnOnKey));
        btn.setOnClickListener(this);
        }
        OnClickListener listener=new OnClickListener()
        {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId()==R.id.Button6)
{
pm.goToSleep(SystemClock.uptimeMillis());
}
else if (v.getId()==R.id.Button7)
{
pm.reboot("robin test the reboot api");
}
else if(v.getId()==R.id.Button8)
{
   Message msgSend=handler.obtainMessage(kCmdUserActivity, 10, 0);
     handler.sendMessageDelayed(msgSend, 35*1000);
}
}
        
        };
        btn=(Button)findViewById(R.id.Button6);
        btn.setOnClickListener(listener);
        btn=(Button)findViewById(R.id.Button7);
        btn.setOnClickListener(listener);
        btn=(Button)findViewById(R.id.Button8);
        btn.setOnClickListener(listener);
        pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    }
    int mFlag=0;
    long time=0;
    public void onClick(View v) {
     time=System.currentTimeMillis();
// TODO Auto-generated method stub
TextView tv=(TextView)v;
int id=tv.getId();
if(id==R.id.Button4)
{
if(v.getTag(kStateKey).equals(kStateOff))
{
mFlag|=PowerManager.ACQUIRE_CAUSES_WAKEUP;
}
else
{
mFlag=mFlag&~PowerManager.ACQUIRE_CAUSES_WAKEUP;
}
}
else if(id==R.id.Button5)
{
if(v.getTag(kStateKey).equals(kStateOff))
{
mFlag|=PowerManager.ON_AFTER_RELEASE;
}
else
{
mFlag=mFlag&~PowerManager.ON_AFTER_RELEASE;
}
}
else
{
if(v.getTag(kStateKey).equals(kStateOff))
{
unReleaseOtherLock(id);
int flag=0;
if(id==R.id.Button0)
{
flag=PowerManager.PARTIAL_WAKE_LOCK;
}
else if (id==R.id.Button1)
{
flag=PowerManager.SCREEN_DIM_WAKE_LOCK;
}
else if(id==R.id.Button2)
{
flag=PowerManager.SCREEN_BRIGHT_WAKE_LOCK;
}
else if(id==R.id.Button3)
{
flag=PowerManager.FULL_WAKE_LOCK;
}
if(id!=R.id.Button0)
{
flag=flag|mFlag;
}
if(mFlag==0||id==R.id.Button0)
{
wl = pm.newWakeLock(flag,tag);
wl.acquire();
}
else
{
int delayMillis=1;
if((mFlag&PowerManager.ACQUIRE_CAUSES_WAKEUP)!=0)
delayMillis=50*1000;
Message msg=handler.obtainMessage(kCmdAcquireLock, flag, 0);
handler.sendMessageDelayed(msg, delayMillis);
if((mFlag&PowerManager.ON_AFTER_RELEASE)!=0)
{
msg=handler.obtainMessage(kCmdReleaseLock, flag, 0);
delayMillis=delayMillis+20*1000;
handler.sendMessageDelayed(msg, delayMillis);
}
}
}
else if(wl!=null)
{
wl.release();
wl=null;
}
}
if(v.getTag(kStateKey).equals(kStateOff))
{
tv.setTag(kStateKey, kStateOn);
tv.setText((String)tv.getTag(kTextTurnOffKey));
Log.i(tag, "set to turn off in onclick");
}
else
{
tv.setTag(kStateKey, kStateOff);
tv.setText((String)tv.getTag(kTextTurnOnKey));
Log.i(tag, "set to turn on in onclick");
}
}
    void unReleaseOtherLock(int id)
    {
     handler.removeMessages(kCmdAcquireLock);
     handler.removeMessages(kCmdReleaseLock);
     if(wl!=null)
     {
     wl.release();
     wl=null;
     }
     Button btn=null;
     for(int i=R.id.Button0;i<=R.id.Button3;i++)
     {
     if(id==i)
     continue;
     btn=(Button)findViewById(i);
     btn.setTag(kStateKey, kStateOff);
     btn.setText((String)btn.getTag(kTextTurnOnKey));
     Log.i(tag, "set to"+btn.getText());
     }
    }
    public void onResume()
    {
     time=System.currentTimeMillis();
     Log.i(tag,"OnResume");
     super.onResume();
    }
    public void onPause()
    {
     long t=(System.currentTimeMillis()-time)/1000;
     Log.i(tag,"pause after user activity time:"+t);
     super.onPause();
    }
    public void onDestroy()
    {
     if(wl!=null)
     {
     wl.release();
     wl=null;
     }
     super.onDestroy();
    }
    final static int kCmdAcquireLock=0;
    final static int kCmdReleaseLock=1;
    final static int kCmdUserActivity=2;
    class MyHandler extends Handler
    {
     public void handleMessage (Message msg) 
     {
     switch(msg.what)
     {
     case kCmdAcquireLock:
     int flag=msg.arg1;
wl = pm.newWakeLock(flag,tag);
wl.acquire();
     break;
     case kCmdReleaseLock:
if(wl!=null)
{
wl.release();
wl=null;
}
break;
     case kCmdUserActivity:
     pm.userActivity(SystemClock.uptimeMillis(), true);
     int loopCnt=msg.arg1;
     if(loopCnt-->0)
     {
     Message msgSend=handler.obtainMessage(kCmdUserActivity, loopCnt, 0);
     handler.sendMessageDelayed(msgSend, 20*1000);
     }
     break;
     }
     }
    }
}
 
 
 
 
 
posted @ 2014-07-01 13:23  催眠术  阅读(405)  评论(0编辑  收藏  举报