Activity間データ通信3方式

9011 ワード

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データ通信の3つの方式
一、サービスをバインドし、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;
        }
    }

サービス内のコード

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;
        }
    }
}

サービスコード

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インタフェースを定義してサービス中のプロセスの変化を傍受する
サービスコード

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);
    }
}

义齿

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) {

    }
}