Activity之间的通信方式,暂时我能想到的有3种分别是:1.Intent方式,2.BroadcastReciver方式下面就用来代码来描述下
Intent方式一般分为2种:1.传统Intent请求方式2.通过startActivityForResult()+setResult()方式
针对传统的Intent请求方式,这里需要结合启动模式表现更佳.
第一种方式代码如下:
package intent1.activity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import cn.activitycommunication.com.R;
/**
* Created by Administrator on 2016/11/26 0026.
*/
public class Activity1 extends Activity
{
private TextView tetxtview;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity1);
tetxtview=(TextView) findViewById(R.id.tetxtview);
btn=(Button) findViewById(R.id.btn);
btn.setText("跳转到Activity2");
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(Activity1.this,Activity2.class);
intent.putExtra("data","123");
startActivity(intent);
}
});
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
tetxtview.setText("收到信息:【"+intent.getStringExtra("data")+"】");
}
}
package intent1.activity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import cn.activitycommunication.com.R;
/**
* Created by Administrator on 2016/11/26 0026.
*/
public class Activity2 extends Activity
{
private TextView tetxtview;
private Button btn;
private String data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity1);
data=getIntent().getStringExtra("data");
tetxtview=(TextView) findViewById(R.id.tetxtview);
tetxtview.setText("收到信息:【"+data+"】");
btn=(Button) findViewById(R.id.btn);
btn.setText("回到到Activity1");
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(Activity2.this,Activity1.class);
intent.putExtra("data","456");
startActivity(intent);
}
});
}
}
运行截图:
startActivityForResult+setResult方式:
package intent2.activity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import cn.activitycommunication.com.R;
/**
* Created by Administrator on 2016/11/26 0026.
*/
public class Activity1 extends Activity
{
private TextView tetxtview;
private Button btn;
private final int REQUESTCODE=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity1);
tetxtview=(TextView) findViewById(R.id.tetxtview);
btn=(Button) findViewById(R.id.btn);
btn.setText("跳转到Activity2");
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Activity1.this, Activity2.class);
intent.putExtra("data", "123");
startActivityForResult(intent, REQUESTCODE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if(resultCode==RESULT_OK)
{
if(requestCode==REQUESTCODE)
{
tetxtview.setText("收到信息:【"+intent.getStringExtra("data")+"】");
}
}
}
}
package intent2.activity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import cn.activitycommunication.com.R;
/**
* Created by Administrator on 2016/11/26 0026.
*/
public class Activity2 extends Activity
{
private TextView tetxtview;
private Button btn;
private String data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity1);
data=getIntent().getStringExtra("data");
tetxtview=(TextView) findViewById(R.id.tetxtview);
tetxtview.setText("收到信息:【"+data+"】");
btn=(Button) findViewById(R.id.btn);
btn.setText("回到到Activity1");
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=getIntent();
intent.putExtra("data","456");
setResult(RESULT_OK, intent);
finish();
}
});
}
}
运行截图:
package broadcast.activity;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import cn.activitycommunication.com.R;
/**
* Created by Administrator on 2016/11/26 0026.
*/
public class Activity1 extends Activity
{
private TextView tetxtview;
private Button btn;
private MyBroadCastReciver reciver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity1);
reciver=new MyBroadCastReciver();
IntentFilter intentFilter=new IntentFilter();
intentFilter.addAction("com.action.broadcast");
registerReceiver(reciver, intentFilter);
tetxtview=(TextView) findViewById(R.id.tetxtview);
btn=(Button) findViewById(R.id.btn);
btn.setText("跳转到Activity2");
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Activity1.this, Activity2.class);
intent.putExtra("data", "123");
startActivity(intent);
}
});
}
class MyBroadCastReciver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) {
tetxtview.setText("收到信息:【"+intent.getStringExtra("data")+"】");
}
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(reciver);
}
}
package broadcast.activity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import cn.activitycommunication.com.R;
/**
* Created by Administrator on 2016/11/26 0026.
*/
public class Activity2 extends Activity
{
private TextView tetxtview;
private Button btn;
private String data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity1);
data=getIntent().getStringExtra("data");
tetxtview=(TextView) findViewById(R.id.tetxtview);
tetxtview.setText("收到信息:【"+data+"】");
btn=(Button) findViewById(R.id.btn);
btn.setText("回到到Activity1");
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent("com.action.broadcast");
intent.putExtra("data","456");
sendBroadcast(intent);
finish();
}
});
}
}
运行截图: