Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I created a resultreceiver class,passed it using intent to the service and implemented the send() in the service to pass the Bundle to the Activity.

I guess I have followed the right steps, I have also implemented the runonUithread() method in the onresultReceive() method.


The goal is simple , as the user clicks on the button, the service gets started and then the Hardcoded String data is to be passed to the Activity to update a textview.

PROBLEM : when I click on the button, Nothing happens. There is no output in the logcat as well as not change in the textview. I guess I have missed something in the code, but I have no idea what it is.

passing my whole code here :

-----------------------------------------------------------
activity_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textview1"
        android:layout_width="0dp"
        android:layout_height="1dp"
        app:layout_constraintBottom_toTopOf="@+id/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="216dp"
        android:onClick="StartService"
        android:text="Click"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>
----------------------------------------------

MainActivity.java :
package com.deepesh.startedserviceapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.ResultReceiver;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    public static final String MESSAGE_KEY="message_key";
    public static final String RECEIVER_TAG = "RECEIVER_TAG";

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

    public void setTextviewtext(String s ){
        TextView textView = findViewById(R.id.textview1);
        textView.setText(s);
    }


    public void StartService(View view) {

        ResultReceiver receiver = new MyResultReceiver(null);
        Intent intent = new Intent(MainActivity.this,started_service.class);
        intent.putExtra(MainActivity.RECEIVER_TAG,receiver);

        startService(intent);
    }


    class MyResultReceiver extends ResultReceiver{

        public MyResultReceiver(Handler handler) {
            super(handler);
        }


        @Override
        protected void onReceiveResult(int resultCode, Bundle resultData) {
            super.onReceiveResult(resultCode,resultData);

           if (resultData !=null){

              final String data = resultData.getString(MainActivity.MESSAGE_KEY);

               MainActivity.this.runOnUiThread(new Runnable() {
                   @Override
                   public void run() {
                       setTextviewtext(data);
                   }
               });

           }
        }

    }


}
---------------------------------------------------------

started_service.java :
package com.deepesh.startedserviceapp;

import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.os.ResultReceiver;


public class started_service extends Service {

    public started_service() {
    }

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

        ResultReceiver receiver = intent.getParcelableExtra(MainActivity.RECEIVER_TAG);

        Bundle bundle = new Bundle();
        bundle.putString(MainActivity.MESSAGE_KEY,"Hello World !");

        receiver.send(MainActivity.RESULT_OK,bundle);

        return Service.START_REDELIVER_INTENT;
    }

    @Override
    public IBinder onBind(Intent intent) {

        return null;
    }

}


What I have tried:

I tried to google but there is no recent updated answer for this problem.
Posted
Updated 15-Jul-20 22:52pm
v3
Comments
Garth J Lancaster 16-Jul-20 2:44am    
Well, you've stated the intent, shown some code, but you haven't actually described 'what happens' - do you get an error, exception, or does the code not even compile ? .. it may be a good idea to use Improve question and say what the actual issue is.
[no name] 16-Jul-20 4:52am    
Done. added it
David Crow 16-Jul-20 22:38pm    
"There is no output in the logcat..."

Maybe because there are no calls to any of the Log methods.

Are you missing try/catch blocks on purpose?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900