最近做Android手机GPS定位系统应用软件的开发,发现手机通过usb或屏幕亮的时候能够通过三种方式定位,一旦手机在休眠的时候,手机的cpu也休眠了,创建的线程会继续sleep,不会发生自己预期的效果,郁闷的事情困扰着自己,再闷也有解决的方案吧,加上蒸笼似的天气。
我的查阅了好许资料,也许自己的想法是正确的,什么都要靠事实说话,最好的解决方案就是在手机屏幕休眠时让cpu继续,才能让我理想的线程continue. 自己写了个demo,测试到凌晨一切正常。
public class GpsTest extends Activity {
/** Called when the activity is first created. */
private PowerManager.WakeLock myWakeLock;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//启动一个线程
new Thread(new MyThread()).start();
PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
myWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "DoNotDimScreen");
//不让程序进入activity界面,直接跳转到手机界面
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
boolean bo = myWakeLock.isHeld();
if (myWakeLock == null || ! bo) {
myWakeLock.acquire();
}
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
boolean bo = myWakeLock.isHeld();
if (myWakeLock == null || ! bo) {
myWakeLock.acquire();
}
}
private void print(String str) {
FileWriter fw = null;
BufferedWriter bw = null;
try {
SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd" + " "
+ "hh:mm:ss");
String datetime = tempDate.format(new java.util.Date()).toString();
String ss = "/sdcard/test.txt";
fw = new FileWriter(ss, true);//
// 创建FileWriter对象,用来写入字符流
bw = new BufferedWriter(fw); // 将缓冲对文件的输出
String myreadline = datetime + "[]" + str;
bw.write(myreadline + ""); // 写入文件
bw.newLine();
bw.flush(); // 刷新该流的缓冲
bw.close();
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
try {
bw.close();
fw.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
print("打印异常" + e1.toString());
}
}//
}
class MyThread implements Runnable {
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
try {
//print("进入线程");
Thread.sleep(30*1000);// 5 * 60 *
print("飞哥跑了一次 \n \n");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
print("线程外部异常" + e.toString());
}
}
}
}
}
要加入权限才可以
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DEVICE_POWER" />
API小资料
各种锁的类型对CPU 、屏幕、键盘的影响:
PARTIAL_WAKE_LOCK:保持CPU 运转,屏幕和键盘灯有可能是关闭的。
SCREEN_DIM_WAKE_LOCK:保持CPU 运转,允许保持屏幕显示但有可能是灰的,允许关闭键盘灯
SCREEN_BRIGHT_WAKE_LOCK:保持CPU 运转,允许保持屏幕高亮显示,允许关闭键盘灯
FULL_WAKE_LOCK:保持CPU 运转,保持屏幕高亮显示,键盘灯也保持亮度
ACQUIRE_CAUSES_WAKEUP:Normal wake locks don't actually turn on the illumination. Instead, they cause the illumination to remain on once it turns on (e.g. from user activity). This flag will force the screen and/or keyboard to turn on immediately, when the WakeLock is acquired. A typical use would be for notifications which are important for the user to see immediately.
ON_AFTER_RELEASE:f this flag is set, the user activity timer will be reset when the WakeLock is released, causing the illumination to remain on a bit longer. This can be used to reduce flicker if you are cycling between wake lock conditions.