2013年4月13日 星期六

防止手機進入待命狀態 - Android 新手學習筆記

在「權限 - Android 新手學習筆記」這篇已有提到

需要防止手機進入待命狀態(應該不叫休眠吧)時,則權限設定須要加入
Uses-Permission : android.permission.WAKE_LOCK
但並不是加入了權限就表示我們的 App 已經不會進入待命狀態
還必須寫些程式才行


import android.os.PowerManager;

public class MyActivity extends Activity {
private PowerManager.WakeLock wakeLock;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);

PowerManager pm = (PowerManager) this.getSystemService(
Context.POWER_SERVICE);
this.wakeLock = pm.newWakeLock(
PowerManager.SCREEN_BRIGHT_WAKE_LOCK
, this.getClass().getName());
this.wakeLock.acquire();
}

@Override
protected void onPause() {
if (this.wakeLock != null) {
try {
this.wakeLock.release();
//我本來想寫在 onDestroy
//結果Eclipse會提示應該寫在 onPause
} catch (Exception e) {
//handle exception
}
}
super.onPause();
}
}

沒有留言:

張貼留言

廣告訊息會被我刪除

Related Posts with Thumbnails