屏幕关闭时,Android服务会休眠

时间:2014-12-06 11:53:08

标签: android android-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 );
}

我的总结服务:

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 );
    }
}

2 个答案:

答案 0 :(得分:1)

答案很简单:你必须在服务中拿一个唤醒锁。

答案 1 :(得分:1)

您也可以使用此标志打开屏幕 -

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

使用Wakelock的另一个解决方案 -

private PowerManager.WakeLock wl;

在onCreate内部 -

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
            wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "doneDimScreen");

在onPause内部 -

wl.release();

内在onResume -

wl.acquire();

您必须添加此权限以便Wakelock正常工作 -

<uses-permission android:name="android.permission.WAKE_LOCK" />

有关Wakelock的详细信息,请参阅 - this

干杯:)