Как закрыть другое Activity из класса, наследующего Service?
Мой класс MyService
:
public class MyService extends Service
{
IBinder mBinder;
public static boolean status = true;
@Override
public void onCreate()
{
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
someTask();
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy()
{
Log.e("MY_SERVICE", "onDestroy()");
new CheckThread().stop();
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent)
{
return mBinder;
}
public void someTask()
{
new CheckThread().start();
}
public class CheckThread implements Runnable
{
Thread th;
@Override
public void run()
{
main();
}
public void start()
{
th = new Thread(this);
th.setPriority(Thread.NORM_PRIORITY);
Log.e("CheckThread", "start()");
th.start();
}
public synchronized void stop()
{
th = null;
status = false;
Log.e("CheckThread", "stop()");
notify();
}
public void main()
{
while (status)
{
if ( ! status)
stop();
boolean connect = Vars.connect;
if ( ! connect)
{
Intent intent = new Intent("android.intent.action.MAIN");
intent.setClass(MyService.this, Disconnect.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
status = false;
stop();
}
try
{
Thread.sleep(3000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
}
Класс Disconnect
запускается, когда закрывается Socket
-соединение (класс не наследует Activity
, функции с UI недоступны).
new Thread(new Runnable()
{
public void run()
{
String str = "";
try
{
while ((str = in.readLine()) != null)
{
}
}
catch (IOException e)
{
}
Vars.connect = false;
}
}).start();
Возможно ли как-то сделать так, чтобы при открытии класса Disconnect
в MyService
закрывалось другое активное Activity
, чтобы был активен только Disconnect
?
Ответов, как закрыть другое Activity
из класса, наследующего Service
, я не нашёл.
Источник: Stack Overflow на русском