在我的应用程序中,即使屏幕处于关闭状态,屏幕已锁定,使用服务也会在十分钟后移动到另一个活动。
我的问题当屏幕处于 ON 状态时运行服务,但是在屏幕 OFF 中或锁定屏幕状态意味着在解锁屏幕后服务停止意味着服务从那里开始。我不知道怎么做这件事。任何人都知道请帮我解决这个问题。
我的服务时间编码
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;
}
}
答案 0 :(得分:0)
您必须在关闭时为屏幕注册接收器
private static BroadcastReceiver mReceiver = new ScreenReceiver();
制作一个方法“regScreenReciever()”将此代码放入
中IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
appContext.registerReceiver(mReceiver, filter);
然后创建一个广播接收器,覆盖其onRecieve方法,然后放入
if(intent.getAction().equalsIgnoreCase(Intent.ACTION_SCREEN_ON)){
Util.disableKeygaurd();
}
Intent linkuryIntent = new Intent(context,UpdateService.class);
context.startService(linkuryIntent);
您的代码运行正常