Android 在空闲时间关闭应用程序
有人能帮我吗,我在安卓系统 我想在10分钟空闲时间后关闭应用程序,我在网上搜索并找到以下链接: 但我对这部分代码有一个小问题:Android 在空闲时间关闭应用程序,android,Android,有人能帮我吗,我在安卓系统 我想在10分钟空闲时间后关闭应用程序,我在网上搜索并找到以下链接: 但我对这部分代码有一个小问题: // soft stopping of thread public synchronized void stop() { stop = true; } 这个错误出现了: 这条线上有多个标记 -无法从线程重写最终方法 -重写java.lang.Thread.stop 请帮助我理解它 谢谢 public class test extends Activit
// soft stopping of thread
public synchronized void stop() {
stop = true;
}
这个错误出现了:
这条线上有多个标记
-无法从线程重写最终方法
-重写java.lang.Thread.stop
请帮助我理解它
谢谢
public class test extends Activity {
ImageView _playButton;
Handler h;
Runnable r;
LinearLayout mainLayout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mainLayout=(LinearLayout)findViewById(R.id.mainLayout);
h=new Handler();
r=new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
finish();
}
};
mainLayout.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
shutIdleApp();
return false;
}
});
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
h.removeCallbacks(r);
}
public void shutIdleApp()
{
h.removeCallbacks(r);
h.postDelayed(r,10*1000);
}
}
下面是我针对这个问题的实现,以防人们对使用复杂的代码感到不舒服
下面是我针对这个问题的实现,以防人们对使用复杂的代码感到不舒服 似乎不需要stop()
来解决问题。我建议按以下方式进行:
public class Waiter extends Thread {
private static final String TAG=Waiter.class.getName();
private long lastUsed;
private long period;
private boolean stop;
private final WeakReference<Context> mContextRef;
public Waiter(final long period, final Context context) {
this.period = period;
stop = false;
mContextRef = new WeakReference<Context>(context);
}
public void run() {
long idle = 0;
this.touch();
do {
idle = System.currentTimeMillis()-lastUsed;
Log.d(TAG, "Application is idle for " + idle + " ms");
try {
Thread.sleep(5000); //check every 5 seconds
if(idle > period) {
final Context context = mContextRef.get();
if (context != null) {
// start activity
startActivity(context);
}
stop = true;
}
}
catch (InterruptedException e) {
Log.d(TAG, "Waiter interrupted!");
// set stop, because smb has called interrupt() on the thread
stop = true;
}
}
while(!stop);
Log.d(TAG, "Finishing Waiter thread");
}
private void startActivity(final Context context) {
final Intent intent = new Intent(context, MyActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
context.startActivity(intent);
} catch (ActivityNotFoundException e) {
// If there is nothing that can send a text/html MIME type
e.printStackTrace();
}
}
public synchronized void touch() {
lastUsed=System.currentTimeMillis();
}
public synchronized void setPeriod(long period) {
this.period=period;
}
}
public类扩展线程{
私有静态最终字符串标记=waterer.class.getName();
私人长期使用;
私人长期;
私有布尔停止;
私有最终WeakReference mContextRef;
公共服务生(最终长周期,最终上下文){
这个周期=周期;
停止=错误;
mContextRef=新的WeakReference(上下文);
}
公开募捐{
长怠速=0;
这个。touch();
做{
idle=System.currentTimeMillis()-上次使用;
Log.d(标记“应用程序空闲时间为”+idle+“ms”);
试一试{
Thread.sleep(5000);//每5秒检查一次
如果(空闲>周期){
final Context Context=mContextRef.get();
if(上下文!=null){
//开始活动
星触觉(语境);
}
停止=真;
}
}
捕捉(中断异常e){
Log.d(标记“服务员打断了!”);
//设置stop,因为smb已在线程上调用中断()
停止=真;
}
}
而(!停止);
Log.d(标记为“完成服务员线程”);
}
私有void startActivity(最终上下文){
最终意图=新意图(上下文,MyActivity.class);
intent.setFlags(intent.FLAG\u活动\u新任务);
试一试{
背景。开始触觉(意图);
}捕获(ActivityNotFounde异常){
//如果没有任何东西可以发送text/html MIME类型
e、 printStackTrace();
}
}
公共同步void touch(){
lastUsed=System.currentTimeMillis();
}
公共同步无效设置周期(长周期){
这个周期=周期;
}
}
因此,它将是一个一次性线程(一旦空闲超时,您将需要创建一个新线程),您可以通过使用标准API随时停止该线程。似乎不需要stop()
来修复该问题。我建议按以下方式进行:
public class Waiter extends Thread {
private static final String TAG=Waiter.class.getName();
private long lastUsed;
private long period;
private boolean stop;
private final WeakReference<Context> mContextRef;
public Waiter(final long period, final Context context) {
this.period = period;
stop = false;
mContextRef = new WeakReference<Context>(context);
}
public void run() {
long idle = 0;
this.touch();
do {
idle = System.currentTimeMillis()-lastUsed;
Log.d(TAG, "Application is idle for " + idle + " ms");
try {
Thread.sleep(5000); //check every 5 seconds
if(idle > period) {
final Context context = mContextRef.get();
if (context != null) {
// start activity
startActivity(context);
}
stop = true;
}
}
catch (InterruptedException e) {
Log.d(TAG, "Waiter interrupted!");
// set stop, because smb has called interrupt() on the thread
stop = true;
}
}
while(!stop);
Log.d(TAG, "Finishing Waiter thread");
}
private void startActivity(final Context context) {
final Intent intent = new Intent(context, MyActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
context.startActivity(intent);
} catch (ActivityNotFoundException e) {
// If there is nothing that can send a text/html MIME type
e.printStackTrace();
}
}
public synchronized void touch() {
lastUsed=System.currentTimeMillis();
}
public synchronized void setPeriod(long period) {
this.period=period;
}
}
public类扩展线程{
私有静态最终字符串标记=waterer.class.getName();
私人长期使用;
私人长期;
私有布尔停止;
私有最终WeakReference mContextRef;
公共服务生(最终长周期,最终上下文){
这个周期=周期;
停止=错误;
mContextRef=新的WeakReference(上下文);
}
公开募捐{
长怠速=0;
这个。touch();
做{
idle=System.currentTimeMillis()-上次使用;
Log.d(标记“应用程序空闲时间为”+idle+“ms”);
试一试{
Thread.sleep(5000);//每5秒检查一次
如果(空闲>周期){
final Context Context=mContextRef.get();
if(上下文!=null){
//开始活动
星触觉(语境);
}
停止=真;
}
}
捕捉(中断异常e){
Log.d(标记“服务员打断了!”);
//设置stop,因为smb已在线程上调用中断()
停止=真;
}
}
而(!停止);
Log.d(标记为“完成服务员线程”);
}
私有void startActivity(最终上下文){
最终意图=新意图(上下文,MyActivity.class);
intent.setFlags(intent.FLAG\u活动\u新任务);
试一试{
背景。开始触觉(意图);
}捕获(ActivityNotFounde异常){
//如果没有任何东西可以发送text/html MIME类型
e、 printStackTrace();
}
}
公共同步void touch(){
lastUsed=System.currentTimeMillis();
}
公共同步无效设置周期(长周期){
这个周期=周期;
}
}
因此,它将是一个一次性线程(一旦空闲超时,您需要创建一个新线程),您可以通过使用标准API随时停止该线程。您好,还有一个问题,代码运行得很好,但我刚刚注意到关于空闲时间的规则只运行一次。我希望每次打开应用程序时都能运行相同的规则。我必须在哪里重置计时器?你的意思是,如果你的应用程序关闭,它不会再次触发吗?嗯,我看到这个流程如下:1。第一个活动(例如,启动屏幕调用应用程序类以启动计时器);2.如果退出应用程序(或用户按home键),则activity可能会调用application class来停止计时器。是的,我想在每次打开应用程序时启动计时器,但如果我按home键,它不应该停止计时器,时间只有在达到时间时才会停止(当然,除非用户关闭应用程序),我的另一个活动打开并要求用户关闭应用程序。。这也是我问您如何打开另一个intent的原因。好的,我认为您可以在应用程序类中引入某种活动计数器,以确定