[英]Android service sleep when screen is off
In my application service I'm fetch server to get new message. 在我的应用程序服务中,我正在获取服务器以获取新消息。
After found new message my serivce application must be screen on and start activity to display a new message. 找到新消息后,必须启动我的服务应用程序并启动活动以显示新消息。
In service I don't have problem to resolve data from server, but when screen is off that cause of sleeping service and after start activity, that could not screen on. 在使用中,我没有问题可以解析来自服务器的数据,但是当屏幕关闭时,这是睡眠服务的原因,并且在启动活动后,也无法打开屏幕。
activity start by service: 活动由服务开始:
public void onCreate (Bundle savedInstanceState) {
super.onCreate ( savedInstanceState );
requestWindowFeature ( Window.FEATURE_NO_TITLE );
getWindow ().setFlags ( WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN );
PowerManager powermanager = ((PowerManager)getBaseContext ().getSystemService( Context.POWER_SERVICE));
wakeLock=powermanager.newWakeLock(
PowerManager.SCREEN_BRIGHT_WAKE_LOCK |
PowerManager.ACQUIRE_CAUSES_WAKEUP, "TsmsScreenOn");
wakeLock.acquire ( 10000 );
WindowManager.LayoutParams params = getWindow().getAttributes();
params.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
params.screenBrightness = 1.0f;
getWindow().setAttributes(params);
setContentView ( R.layout.service_view_dialog );
}
My summarized service: 我的总结服务:
public class ToobaPayamakService extends Service {
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onStart(Intent intent, int startId) {
}
private Runnable sendUpdatesToUI = new Runnable() {
};
private void DisplayLoggingInfo() {
}
public int callRequestFromServer(){
if (G.checkInternetConnection ()) {
try {
Cursor c = db.getCursorFirstItemReceived ( username );
c.moveToFirst ();
if (c.moveToFirst ()) {
receive_lastID = c.getString ( c.getColumnIndex ( "lastId" ) );
}
c.close ();
unread = checkWebService ( Integer.valueOf ( receive_lastID ) );
} catch (Exception e) {
e.printStackTrace ();
}
Log.e ( "unread: ", unread + "" );
if (unread != 0) {
G.config_username = username;
G.config_password = password;
try {
G.getRequestFromServerByService ( Long.parseLong ( receive_lastID ), unread, contentResolver );
result_count = unread;
} catch (JSONException e) {
e.printStackTrace ();
}
}
}
return result_count;
}
/* ----------------------------------------------------------------------------------------------------------------- notifyTest */
public void notifyTest ( int unread ) {
Intent i = new Intent ();
i.setClass ( this, ServiceDialog.class );
i.putExtra ( "username", username );
i.putExtra ( "password", password );
i.putExtra ( "unread" , counter );
i.putExtra ( "notify" , notify );
i.setFlags ( Intent.FLAG_ACTIVITY_REORDER_TO_FRONT );
i.setFlags ( Intent.FLAG_ACTIVITY_NEW_TASK );
startActivity ( i );
}
}
答案很简单:您必须在服务中获取唤醒锁。
You can also use this flag to keep your screen ON - 您还可以使用此标志将屏幕保持打开状态-
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
One more solution using Wakelock - 使用Wakelock的另一种解决方案-
private PowerManager.WakeLock wl;
Inside onCreate - 里面的onCreate-
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "doneDimScreen");
Inside onPause - 内部onPause-
wl.release();
Inside onResume - 里面onResume-
wl.acquire();
you have to add this permission as well for the Wakelock to work - 您还必须添加此权限才能使Wakelock正常工作-
<uses-permission android:name="android.permission.WAKE_LOCK" />
For more detail on Wakelock refer - this 有关Wakelock的更多详细信息,请参考- 此
Cheers :) 干杯:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.