Activtiy之间数据通信3种方式

Activity 之间数据通信 Intent / bundle

第一个activity跳转代码

		Bundle bundle = new Bundle();
        bundle.putString("name","mile");
        bundle.putInt("age",18);
        Intent intent = new Intent(this,MainActivity.class);
        intent.putExtras(bundle);
        startActivity(intent);

MainActivity 接受代码

Intent intent = getIntent();
String name = intent.getStringExtra("name");
int age = intent.getIntExtra("age",0)

Activity与Fragment通信

一、Activity奖数据传递给Fragment

1、Bundle
2、直接在Activity定义一个方法

bundle
FragmentActivity里传递数据代码

public class FragmentActivity extends Activity {
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
        Bundle bundle = new Bundle();
        bundle.putString("id","sssss");
        Fragment fragment = null;//伪代码
        fragment.setArguments(bundle);
    }
}

MyFragment 获取数据代码

public class MyFragment extends Fragment {
    public String productId;

    @Override
    public void onStart() {
        super.onStart();
        if (isAdded()){//判断Fragment已经衣服Activity
            productId = getArguments().getString("id");

        }
    }
}

直接在Activity定义一个方法

public class FragmentActivity extends Activity {
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);

    }


    public String getTitles(){
        return "title";
    }
}

在Fragment中获取

public class MyFragment extends Fragment {
    public String title;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        title = ((FragmentActivity)activity).getTitles();
    }
}

二、Fragment奖数据传递给Activity(广播之类的数据传递不提)

接口回调
1、在fragment中定义一个内部回调接口
2、fragment的方法onAttach()
3、调用onDetach方法,这个时候要把传递进来的activity对象释放掉

Fragment代码

public class MyFragment extends Fragment implements View.OnClickListener{


    //一、定义了所有activity必须实现的接口方法
    public interface FragmentListener{
        void process(String str);
    }
    //二、定义用来与外部activity交互,获取到宿主activity
    private FragmentListener listener;

    //当Fragment被加载到activity的时候会被回调
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof FragmentListener){
            listener = (FragmentListener) activity; //二、二获取到宿主activity并赋值
        }else {

            throw new IllegalArgumentException("activity must implements FragmentListener");
        }
    }

    @Override
    public void onClick(View view) {
        listener.process("我是接口回调数据");//三、执行回调

    }

    @Override
    public void onDetach() {
        super.onDetach();
        listener = null; //四、释放对象
    }
}

Activity代码


public class FragmentActivity extends Activity implements MyFragment.FragmentListener{
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);

    }




    @Override
    public void process(String str) {

        Toast.makeText(this,"从fragment中获取到的数据:"+str,Toast.LENGTH_SHORT).show();
    }
}

Activity与Service数据通信三种方式

一、绑定服务,利用ServiceConnection类
二、简单通信,利用Intent进行传值
三、定义一个callback接口来监听服务中的进程的变化

一、绑定服务,利用ServiceConnection类
Activity里代码

public class FragmentActivity extends Activity implements ServiceConnection,View.OnClickListener{
    public MyService.Binder myBinder = null;// 1
    private Intent intent;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
        intent = new Intent(this,MyService.class);

    }


    @Override
    public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
        myBinder = (MyService.Binder)iBinder;//2

    }

    @Override
    public void onServiceDisconnected(ComponentName componentName) {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.all://启动服务
                if (myBinder != null)
                    myBinder.setData("启动服务了"); //数据通信
                break;
            case R.id.ALT://终止服务
                stopService(intent);
                break;
        }
    }

service里的代码


public class MyService extends Service {
    public String data = "服务器正在执行中";
    public MyService() {
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return new Binder();
    }

    public class Binder extends android.os.Binder{
        public void setData(String data){
            MyService.this.data = data;
        }
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        data = intent.getStringExtra("data");
        return super.onStartCommand(intent,flags,startId);
    }
}

二、简单通信,利用Intent进行传值
activity里代码


public class FragmentActivity extends Activity implements View.OnClickListener{
    public MyService.Binder myBinder = null;// 1
    private Intent intent;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
        intent = new Intent(this,MyService.class);

    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.all://启动服务
                intent.putExtra("data","data");
                startService(intent);
                break;
            case R.id.ALT://终止服务
                stopService(intent);
                break;
        }
    }
}

service代码


public class MyService extends Service {
    public String data = "服务器正在执行中";
    public MyService() {
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return new Binder();
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        data = intent.getStringExtra("data");
        return super.onStartCommand(intent,flags,startId);
    }
}

三、定义一个callback接口来监听服务中的进程的变化

service代码


public class MyService extends Service {
    public String data = "服务器正在执行中";
    Callback callback;
    public MyService() {
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return new Binder();
    }

    public class Binder extends android.os.Binder{
        public void setData(String data){
            MyService.this.data = data;
        }

        public MyService getMyService(){
            return MyService.this;
        }
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        data = intent.getStringExtra("data");
        return super.onStartCommand(intent,flags,startId);
    }
    public void setCallback(Callback callback){
        this.callback = callback;
    }

    public Callback getCallback(){

        return callback;
    }

    public interface Callback {
        void onDataChange(String data);
    }
}

activity里代码


public class FragmentActivity extends Activity implements View.OnClickListener,ServiceConnection{
    public MyService.Binder myBinder = null;// 1
    private Intent intent;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
        intent = new Intent(this,MyService.class);

    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.all://启动服务
                intent.putExtra("data","data");
                startService(intent);
                break;
            case R.id.ALT://终止服务
                stopService(intent);
                break;
        }
    }

    @Override
    public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
        myBinder = (MyService.Binder) iBinder;
        myBinder.getMyService().setCallback(new MyService.Callback() {
            @Override
            public void onDataChange(String data) {
                //这里能拿到服务的数据可以通过handle 更新界面
            }
        });
    }

    @Override
    public void onServiceDisconnected(ComponentName componentName) {

    }
}

你可能感兴趣的:(Java,Android)