从Service传递String数据调用活动

时间:2012-10-25 09:15:49

标签: android android-activity android-service

我想从Android中的服务调用活动A(并将一些字符串传递给它)。此活动A inturns使用startActivityForResult().调用另一个活动B.基本上,我希望我的服务等到获得活动B的结果。请告诉我如何实现这个实现(以及在intent中设置哪个标志)?

服务

Intent intent = new Intent(getBaseContext(),ActivityA.class);       
intent.putExtra("code",script);
intent.putExtra("type", type);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

ActivityA

Intent intent = new Intent(this,ActivityB.class);
intent.putExtra("code",code);
intent.putExtra("type", type);      
startActivityForResult(intent,REQUEST_CODE); 

1 个答案:

答案 0 :(得分:0)

首先创建一个界面

public interface MyActions {
public void doActions();
}

现在让您的服务类实现该接口

public class MyService extends Service implements MyActions{

并覆盖方法

@Override
public void doActions() {
    //Some code
}

现在您的活动A通过startActivityForResult方法调用B,因此覆盖A的onActivityResult

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    //Some code
}

在这个A类中,现在创建一个接口的静态实例

static MyActions mActions;

现在编写一个像这样的静态方法

public static void setMyListener(MyActions act){
    mActions = act;
}

现在,在你为A类重写的onActivityResult中添加了这行代码

mActions.doActions();

所以你得到了

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    mActions.doActions();
}

现在在onStart()方法的Service类中,在调用活动

之前写下这一行
ActA.setMyListener(this);

现在,从活动B开始,调用转到A的onActivityResult,而来自A,Serivce可以听取覆盖的doActions方法()

将所有代码写入您想要执行的doActions()服务方法

这是代码

第1次活动:ActA.java

public class ActA extends Activity {
static MyActions mActions;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.act1);
    Button btn = (Button) findViewById(R.id.button1);
    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            startActivityForResult(new Intent(ActA.this,ActB.class), 15);
        }
    });
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Log.d("Response","requestCode" + requestCode + " resultCode" + resultCode);
    mActions.doActions();
}

public static void setMyListener(MyActions act){
    mActions = act;
}
}

第二项活动:ActB.java

public class ActB extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.act2);
}
}

应用启动时的MainActivity

public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //Start the service
    startService(new Intent(this, MyService.class));
}
}

界面:MyActions.java

public interface MyActions {
public void doActions();
}

Serivce类:MyService.java

public class MyService extends Service implements MyActions{


@Override
public IBinder onBind(Intent arg0) {
    return null;
}

public void onStart(Intent intent, int startId) {

    super.onStart(intent, startId);
    Log.d("Response", "Started");
    ActA.setMyListener(this);
    startActivity(new Intent(this,ActA.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
}

@Override
public void doActions() {
    Log.d("Response", "Listener working");
}

}