1

在我的应用程序中,即使屏幕处于关闭状态,屏幕锁定,十分钟后也会使用服务移动到另一个活动。

我的问题服务在屏幕处于开启状态时运行,但在屏幕关闭或锁定屏幕状态时意味着服务在我解锁屏幕后停止意味着服务从那里开始。我不知道该怎么做。可以任何人都知道请帮我解决这个问题。

我的服务时间编码

public class Time_out_services extends Service {

Handler mHandler;

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    //TODO do something useful

    mHandler = new Handler();


    mHandler.postDelayed(first_Task, 4 * 60 * 1000);


    return Service.START_NOT_STICKY;
  }

Runnable first_Task = new Runnable() {
    public void run() {

        mHandler = new Handler();

        Intent i = new Intent();
        i.setClass(Time_out_services.this, Dialog_actvity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(i);      

        stopSelf();
    }
    }
};

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}
}
4

1 回答 1

1

you have to register a reciever for the screen on off

    private static BroadcastReceiver mReceiver = new ScreenReceiver();

make a method "regScreenReciever()" put this code in that

IntentFilter filter = new IntentFilter();
                    filter.addAction(Intent.ACTION_SCREEN_ON);
                    filter.addAction(Intent.ACTION_SCREEN_OFF);
                    appContext.registerReceiver(mReceiver, filter);

then create a broadcast reciever , override its onRecieve method and then put

if(intent.getAction().equalsIgnoreCase(Intent.ACTION_SCREEN_ON)){
                    Util.disableKeygaurd();
                }
                Intent linkuryIntent = new     Intent(context,UpdateService.class);
                context.startService(linkuryIntent);

your code will run fine

于 2013-06-06T11:35:33.003 回答