Android 从BillingReceiver内刷新屏幕
我有一个由多个活动组成的应用程序。可以购买多个项目。每个活动中显示的内容可能因购买的内容而异。我有一个带有Android 从BillingReceiver内刷新屏幕,android,Android,我有一个由多个活动组成的应用程序。可以购买多个项目。每个活动中显示的内容可能因购买的内容而异。我有一个带有purchaseStateChanged()方法的BillingReceiver。我需要在purchaseStateChanged()中做的是,无论哪个活动正在运行,请使其无效?/refresh?屏幕。有没有一个标准的方法可以做到这一点 编辑:看看Simon的评论,为了便于回答,让我们假设当BillingReceiver收到对purchaseStateChanged()的调用时,两个活动之一
purchaseStateChanged()
方法的BillingReceiver
。我需要在purchaseStateChanged()
中做的是,无论哪个活动正在运行,请使其无效?/refresh?屏幕。有没有一个标准的方法可以做到这一点
编辑:看看Simon的评论,为了便于回答,让我们假设当BillingReceiver收到对purchaseStateChanged()的调用时,两个活动之一可能正在运行,ActivityA或ActivityB。这些活动都包含方法void redraw_everything()
,它们都声明为:
void redraw_everything()
{
getWindow().getDecorView().invalidate();
}
我不知道设置回调的语法,但我猜答案可能类似于:
BillingReceiver.setupcallback(redraw_everything()); // ??
BillingReceiver.setupcallback(null); // ??
??? stored_method_to_call;
void setupcallback(???? method_to_call)
{
stored_method_to_call = method_to_call;
}
在创建这两个活动时,我们应该说:
BillingReceiver.setupcallback(redraw_everything()); // ??
BillingReceiver.setupcallback(null); // ??
??? stored_method_to_call;
void setupcallback(???? method_to_call)
{
stored_method_to_call = method_to_call;
}
在onDestroy()中,方法有如下内容:
BillingReceiver.setupcallback(redraw_everything()); // ??
BillingReceiver.setupcallback(null); // ??
??? stored_method_to_call;
void setupcallback(???? method_to_call)
{
stored_method_to_call = method_to_call;
}
然后在BillingReceiver中创建一个类似以下内容的方法:
BillingReceiver.setupcallback(redraw_everything()); // ??
BillingReceiver.setupcallback(null); // ??
??? stored_method_to_call;
void setupcallback(???? method_to_call)
{
stored_method_to_call = method_to_call;
}
然后在purchaseStateChanged()中有如下代码:
if (stored_method_to_call != null) // ?? not sure about syntax
{
stored_method_to_call(); // ?? not sure about syntax
}
定义一个接口并使用回调让活动知道已购买
public Interface PurchaseStateChangedListener {
void onPurchaseStateChanged();
}
在您的账单接收器中
ArrayList<PurchaseStateChangedListener > listeners = new ArrayList<PurchaseStateChangedListener >();
...
public void setPurchaseStateChangedListener(PurchaseStateChangedListener listener){
listeners.add(listener);
}
在您的活动中:
public class Test extends Activity implements PurchaseStateChangedListener {
...
@Override
public void onCreate(Bundle savedInstanceState)
{
...
billingReceiver.setPurchaseStateChangedListener(this);
...
}
public void onPurchaseStateChanged(){
// do whatever you need to do
}
您可以通过添加removePurchaseStateChangedListener并检查是否在setPurchaseStateChangedListener中两次添加相同的侦听器来改进billing receiver类。定义接口并使用BillingReceiver对活动的回调。“活动”注册了onPurchaseStateChangedListener(),并根据需要进行响应。@Simon:谢谢,这看起来可能是正确的,但我对回调不太适应,无法轻松运行它。因此,我在问题中添加了一些额外的内容,以使其更容易回答-也许您可以将您的评论作为答案,这样您就可以将其标记为正确。完成,在顶部添加一些糖。谢谢。这一切看起来都很有希望,我已经准备好了所有的东西要编译(需要添加一些静态的东西)。我会给它打勾,只要我已经确定它的工作正常。