Android 从Activity返回数据

Main.java

public class Main extends Activity {

    private Button button;
    private EditText input1, input2, result;
    private final static int REQUEST_CODE=1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        input1 = (EditText)this.findViewById(R.id.input1);
        input2 = (EditText)this.findViewById(R.id.input2);


        button = (Button)this.findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                String input1str = input1.getText().toString();
                String input2str = input2.getText().toString();
                int a = Integer.parseInt(input1str);
                int b = Integer.parseInt(input2str);

                Intent intent = new Intent(Main.this, OtherActivity.class);
                intent.putExtra("a",a);
                intent.putExtra("b",b);
                startActivityForResult(intent, REQUEST_CODE);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==OtherActivity.RESULT_CODE){
            if(requestCode==REQUEST_CODE){
                int return_int = data.getIntExtra("result",0);
                result = (EditText)this.findViewById(R.id.result);
                result.setText(return_int);
            }
        }
    }
}


OtherActivity.java

public class OtherActivity extends Activity{

    private TextView label;
    private EditText input;
    private Button button;
    public final static int RESULT_CODE=2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.other);

        label = (TextView)this.findViewById(R.id.label);
        input = (EditText)this.findViewById(R.id.input);
        button =(Button)this.findViewById(R.id.button);

        Intent intent = getIntent();
        int a = intent.getIntExtra("a", 0);
        int b = intent.getIntExtra("b", 0);
        label = (TextView)this.findViewById(R.id.label);
        label.setText(a +  " + " + b + " = ?");

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int input_int =  Integer.parseInt(input.getText().toString());

                Intent intent = new Intent();
                intent.putExtra("result", input_int);
                //通过Intent对象返回结果,使setResult
                setResult(RESULT_CODE,intent);
                //结束当前的Activity生命周期
                finish();
            }
        });

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要在 Android Activity 中显示 Service 数据,您可以使用以下步骤: 1. 创建 Service 类并在其中实现您的逻辑。 2. 在 Service 类中创建一个公共方法,该方法返回您想要在 Activity 中显示的数据。 3. 在 Activity 中创建一个服务连接器类,该类实现 ServiceConnection 接口并在其中实现 onServiceConnected() 和 onServiceDisconnected() 方法。 4. 在 Activity 中使用 bindService() 方法将 Activity 绑定到 Service 上。 5. 在 Activity 中实现 onActivityResult() 方法并在其中处理从 Service 返回数据。 6. 在 Activity 中使用 TextView 或其他 UI 控件来显示从 Service 返回数据。 以下是一个简单的示例: 在 Service 类中: ```java public class MyService extends Service { public String getData() { return "Hello from the service!"; } } ``` 在 Activity 类中: ```java public class MainActivity extends AppCompatActivity implements ServiceConnection { private MyService myService; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bindService(new Intent(this, MyService.class), this, BIND_AUTO_CREATE); } @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { MyService.MyBinder binder = (MyService.MyBinder) iBinder; myService = binder.getService(); String data = myService.getData(); TextView textView = findViewById(R.id.text_view); textView.setText(data); } @Override public void onServiceDisconnected(ComponentName componentName) { myService = null; } } ``` 在 ServiceConnection 类中: ```java public class MyServiceConnection implements ServiceConnection { private MainActivity activity; public MyServiceConnection(MainActivity activity) { this.activity = activity; } @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { MyService.MyBinder binder = (MyService.MyBinder) iBinder; MyService myService = binder.getService(); String data = myService.getData(); activity.showData(data); } @Override public void onServiceDisconnected(ComponentName componentName) { activity.showData(null); } } ``` 在 Activity 中使用 bindService() 方法将 Activity 绑定到 Service 上: ```java MyServiceConnection connection = new MyServiceConnection(this); bindService(new Intent(this, MyService.class), connection, BIND_AUTO_CREATE); ``` 在 Activity 中实现 onActivityResult() 方法并在其中处理从 Service 返回数据: ```java public void showData(String data) { TextView textView = findViewById(R.id.text_view); if (data != null) { textView.setText(data); } else { textView.setText("No data received from Service"); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_26182553

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值