有什么方法可以让我们与生成意图的 class 通信并从扩展 IntentService class 的 class 发送它?
Is there any way by which we can communicate back to the class which generated an intent and sent it from a class which extends IntentService class?
在构建我的应用程序时,我有一个 class( class A) 扩展了另一个 class say class B。现在我从这个 class 并将此意图传递给另一个 class C,后者又扩展了 IntentService。现在,问题是:有什么方法可以让我从 class C.
与 class A 通信
class A extends B
{
.
.
.
void method1()
{
Intent intent = new Intent(context, C.class);
context.startService(intent);
}
void method2(String param)
{
.
.
.
}
}
class C.java如下:
class C extends IntentService
{
@Override
protected void onHandleIntent(Intent intent)
{
// Need to call method2 of the same object from which intent was created in class A
// What should be done?
}
}
我希望我能够正确解释这个问题。这个你能帮我吗。 TIA
我不熟悉 IntentService。但是每当我有另一个 class 做某事时,我通过 LocalBroadcastManager 返回它的结果,这意味着从 class C 你通过 LocalBroadcastManager 发回一个意图并通过注册到该 broadcastet 意图在 A 中接收它:
在 C 中:
Intent intent = new Intent("SERVICE_FINISHED");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
在答:
public A() {
broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("SERVICE_FINISHED")) {
//do stuff}
}
}
};
LocalBroadcastManager.getInstance(this).registerReceiver(broadcastReceiver, new IntentFilter("SERVICE_FINISHED"));
}
在构建我的应用程序时,我有一个 class( class A) 扩展了另一个 class say class B。现在我从这个 class 并将此意图传递给另一个 class C,后者又扩展了 IntentService。现在,问题是:有什么方法可以让我从 class C.
与 class A 通信class A extends B
{
.
.
.
void method1()
{
Intent intent = new Intent(context, C.class);
context.startService(intent);
}
void method2(String param)
{
.
.
.
}
}
class C.java如下:
class C extends IntentService
{
@Override
protected void onHandleIntent(Intent intent)
{
// Need to call method2 of the same object from which intent was created in class A
// What should be done?
}
}
我希望我能够正确解释这个问题。这个你能帮我吗。 TIA
我不熟悉 IntentService。但是每当我有另一个 class 做某事时,我通过 LocalBroadcastManager 返回它的结果,这意味着从 class C 你通过 LocalBroadcastManager 发回一个意图并通过注册到该 broadcastet 意图在 A 中接收它:
在 C 中:
Intent intent = new Intent("SERVICE_FINISHED");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
在答:
public A() {
broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("SERVICE_FINISHED")) {
//do stuff}
}
}
};
LocalBroadcastManager.getInstance(this).registerReceiver(broadcastReceiver, new IntentFilter("SERVICE_FINISHED"));
}