0

I need to call onHandleIntent and receive a value from app1 and get result in app2 without user notices ( different apps, one activity will call the servixe of other app). However, when onHandleIntent is starting in this line:

 mReceiver = workIntent.getParcelableExtra("receiver");

Brings the error

E/Parcel: Class not found when unmarshalling: com.example.idscomercial.myapplication.AddressResultReceiver
          java.lang.ClassNotFoundException: com.example.idscomercial.myapplication.AddressResultReceiver
             ...
           Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.idscomercial.myapplication.AddressResultReceiver" on path: DexPathList[[zip file "/data/app/com.veritran.vttokentest-2/base.apk"],nativeLibraryDirectories=[/data/app/com.veritran.vttokentest-2/lib/arm, /vendor/lib, /system/lib]]

I actually read :

https://stackoverflow.com/questions/28589509/android-e-parcel-class-not-found-when-unmarshalling-only-on-samsung-tab3

But still I cannot find the solution

This is the code:

App1

public class servicev extends IntentService {

    protected ResultReceiver mReceiver;

    public servicev() {
        super("yeah");
    }

    @Override
    protected void onHandleIntent(Intent workIntent) {
        Toast b = Toast.makeText(this.getApplicationContext(), "onHandle starting",  Toast.LENGTH_LONG    );
        b.show();
        Log.d("just", "tellme");
        String dataString = workIntent.getDataString();
        mReceiver = workIntent.getParcelableExtra("receiver");
       // mReceiver = new AddressResultReceiver(new Handler());

        deliverResultToReceiver(1,"value of servicev");
    }

    private void deliverResultToReceiver(int resultCode, String message) {
        Bundle bundle = new Bundle();
        bundle.putString("receiver", message);
        mReceiver.send(resultCode, bundle);
    }



}

App2:

public class MainActivity extends AppCompatActivity {

    protected ResultReceiver mResultReceiver;

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

    protected void startIntentService() {
        //Intent intent = new Intent();
        mResultReceiver = new AddressResultReceiver(new Handler());
        Intent intent=new Intent("com.veritran.vttokentest.servicev.START_SERVICE");


        intent.setPackage("com.veritran.vttokentest");
        //Intent intent = new Intent(this, FetchAddressIntentService.class);
        intent.putExtra("receiver", mResultReceiver);
        startService(intent);
    }




}

==> 2

public
class AddressResultReceiver extends ResultReceiver {

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

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

        int duration = Toast.LENGTH_LONG;
        String mAddressOutput = resultData.getString("1");
        //Toast toast = Toast.makeText(context, "hi brow", duration);
        //toast.show();
        if (resultCode == 1) {



        }

    }
}

Manifest app1:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.veritran.vttokentest">

    <application
        android:allowBackup="false"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name="com.veritran.vttokentest.MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service
            android:enabled="true"
            android:exported="true"
            android:name="com.veritran.vttokentest.servicev">
            <intent-filter>
            <action android:name="com.veritran.vttokentest.servicev.START_SERVICE" />
            </intent-filter>
            </service>


    </application>

</manifest>
arnoldssss
  • 468
  • 3
  • 9
  • 22

1 Answers1

2

There could be a couple things going on here. First you are going to need a receiver to ever get information back from your service to the main activity. To create a receiver, make a new class that looks like this:

public class myReceiver extends ResultReceiver {
private Receiver mReceiver;

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

public void setReceiver(Receiver receiver) {
    mReceiver = receiver;
}

public interface Receiver {
    void onReceiveResult(int resultCode, Bundle resultData);
}

@Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
    if (mReceiver != null) {
        mReceiver.onReceiveResult(resultCode, resultData);
    }
}

Then, in your main activity, create the receiver onCreate() with the following code:

mReceiver = new myReceiver(new Handler());
mReceiver.setReceiver(this);

This will be necessary to actually get any data back. you will also need to pass the receiver into the intent as an extra intent.putExtra("receiver", mReceiver); in the section where you define the intent. Then, in the IntentService, you can send your info back to the MainActivity with workIntent.getParcelableExtra("receiver").send(1, b) (where 1 is the result code you want and b is a Bundle of all the info you want to send back).

As far as why your service is never being called, I would check here to see if you are actually sending a correct intent to the service. You should also try removing the intent-filter from your service as it is unnecessary.

Generally you are not going to want to create a new MainActivity inside the service and use the receiver instead. Otherwise your service appears to be correct so I would make sure you check the intent that you are trying to pass in. If that still fails there are a number of other threads here about this problem.

I am new here so please let me know if you need more help.

  • 1
    I'll follow your advices throughout the day, and post here how it result – arnoldssss Jan 16 '18 at 16:06
  • This is the code until now... still onHandleIntent is not entering... feel free to change it http://collabedit.com/96uem – arnoldssss Jan 17 '18 at 03:55
  • Hmmmm, how do you know that it is not getting to the handle intent? – Matthew Flathers Jan 17 '18 at 13:21
  • Ahhhh I would try running the debugger or putting in log statements because the toast is not a reliable debugging method. Toasts have to be created in the main UI thread and will not show if you create them in the service. – Matthew Flathers Jan 17 '18 at 15:31
  • yes, I also run the app in the debugger with a breakpoint in onHandleIntent first line, open the app2 and it does not start , idea? – arnoldssss Jan 17 '18 at 15:42
  • I would try entering some log statements everywhere to check your intent, make sure that the HandleIntent is never called. The only other difference between my code and yours them seems like I called startService(intent) instead of startIntentService(). You could try that too – Matthew Flathers Jan 17 '18 at 15:53
  • ok I will try that , could it be possible to get your code in a git repo? thanks for all until now – arnoldssss Jan 17 '18 at 16:01
  • Sadly I can't make my code public because it's company property :( It also implements the service as a new package which is slightly different. – Matthew Flathers Jan 17 '18 at 16:09
  • ok, but you actually use the service to comunicate something to a different apllication right? – arnoldssss Jan 17 '18 at 16:42
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/163345/discussion-between-matthew-flathers-and-arnoldssss). – Matthew Flathers Jan 17 '18 at 17:53
  • Hi Matthew I was able to run onHandleIntent but know app1 says: Class not found when unmarshalling: com.example.idscomercial.myapplication.MainActivity$AddressResultReceiver – arnoldssss Jan 19 '18 at 22:14