aeiou718
@aeiou718

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

Intentの値を更新させたいです

解決したいこと

Intentの値を更新させたいです。
startService()による更新ではなく、bindService()による更新をさせたいです。

該当するソースコード

MainActivity.java
public class MainActivity extends AppCompatActivity {
    private ServiceConnection mConnection = new ServiceConnection() {
        MyService mBindService;

        public void onServiceConnected(ComponentName className, IBinder service) {
            Log.d("debug", "onServiceConnected");
            mBindService = ((MyService.BindServiceBinder) service).getService();
        }

        public void onServiceDisconnected(ComponentName className) {
            Log.d("debug", "onServiceDisconnected");

            mBindService = null;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        Button button1 = findViewById(R.id.btButtonA);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new TextChange(MainActivity.this).execute();
            }
        });

        Button button2 = findViewById(R.id.btButtonB);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, MyService.class);
                TextView tv = findViewById(R.id.tvTestA);
                intent.putExtra("Test", tv.getText().toString());
                startService(intent);
                bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
                unbindService(mConnection);
            }
        });
    }
}
MyService.java
public class MyService extends Service {

    String default_text="";

    private final IBinder mBinder = new BindServiceBinder();

    public class BindServiceBinder extends Binder {
        MyService getService() {
            return MyService.this;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.d("debug", "onBind");
        default_text = intent.getStringExtra("Test");
        Log.d("debug", default_text);
        return mBinder;
    }

    @Override
    public void onRebind(Intent intent){
        Log.d("debug","onRebind");
        default_text = intent.getStringExtra("Test");
        Log.d("debug", default_text);
        super.onRebind(intent);
    }

    @Override
    public boolean onUnbind(Intent intent){
        Log.d("debug","onUnbind");
        super.onUnbind(intent);
        return true;
    }

    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d("debug", "onStartCommand");
        default_text = intent.getStringExtra("Test");
        Log.d("debug", default_text);
        return START_NOT_STICKY;
    }

    @Override
    public void onDestroy(){
        Log.d("debug","onDestroy");
        super.onDestroy();
    }
}
TextChange.java
public class TextChange extends AsyncTask<URL, Void, String> {
    private Activity mainActivity;
public TextChange(Activity activity) {
    this.mainActivity = activity;
}
    @Override
    protected String doInBackground(URL... urls) {
        return "TextChange";
    }
    @Override
    protected void onPostExecute(String result) {
        TextView tv= mainActivity.findViewById(R.id.tvTestA);
        tv.setText(result);
    }
}

このコードについて

Button2によってMyServiceを呼ばせた後、Button1によってTextChangeを呼んでTextViewを変化させた後、再びButton2でMyServiceを呼ぶとonStartCommand()のdebugのタグのログはTextChangeに変化するのですが、onRebind()のdebugのタグのログは変化しないんです。
どうにかしてonRebind()のdebugのタグのログを変化させたいのですが、何か良い案はないでしょうか。
よろしくお願いします。

0

1Answer

Comments

  1. @aeiou718

    Questioner

    いろいろ試した結果、onDestroy()を呼ばないと中身が更新されないようです。
    回答していただきありがとうございます

Your answer might help someone💌