2014-02-13 269 views
0

在我的应用程序中,我想停止一个线程[只是一个循环来打印],在主活动onCreate时创建。 ,工作正常。如预期的那样,如何在主线程停止/暂停线程/ Activity在android中暂停/停止?

但是,我想停止相同的创建线程,而主线程/活动暂停或停止。作为中断的用户案例。

如何处理创建的线程?

+0

尝试this-> getActivity()。getMainLooper()。getThread()。join(2000); – Amrut

+0

我们正在尝试使用上面的行代码。我们需要放置这些代码, - onCreate或主线程或子线程中。 – user3268059

回答

0
getActivity().getMainLooper().getThread().join(2000); 

以上本身在主线程中工作。在要暂停主线程的代码行下面尝试。例如,我想在按钮单击时调用一个函数“Onclicked()”,并且该UI函数的某些部分需要在UI线程暂停时执行。所以我会写上面的代码行。示例代码: -

((ContainerBaseFragmentActivity) getActivity()).addProductToCart(product, mArrayListCartItemAttributes, price, true); // line Which will pause UI Thread. 
try 
{         
    getActivity().getMainLooper().getThread().join(2000); 
} 
catch(InterruptedException e) 
{ 
    e.printStackTrace(); 
}