日期:2014-05-20 浏览次数:20888 次
public class main
{
public static void main(String args[])
{
MainClass mInstance = new MainClass();
mInstance.RegisterListener();
//mInstance.UnRegisterListener();
}
}
interface IInterface
{
public void onStatusChanged(int newStatus);
}
class ServiceClass
{
IInterface mListener = null;
public void RegisterListener(IInterface ls)
{
mListener = ls;
}
private void notifyMainClass(int newStatus)
{
if (mListener != null)
{
try
{
mListener.onStatusChanged(newStatus);
}
catch(NullPointerException e)
{
mListener = null;
e.printStackTrace();
}
}
}
}
class MainClass
{
private int mNewStatus = -1;
ServiceClass mService = new ServiceClass();
private IInterface mListener = new IInterface()
{
public void onStatusChanged(int newStatus)
{
/* process newStatus */
System.out.println("" + newStatus);
mNewStatus = newStatus;
}
};
void RegisterListener()
{
mService.RegisterListener(mListener);
}
void UnRegisterListener()
{
mListener = null;
}
}