Android communication between applications using R

2019-08-13 13:37发布

问题:

I have two applications. First application is simple service application which do some work in background and finish. I decided to use IntentService.

@Override
protected void onHandleIntent(Intent intent) {

    if (intent == null) {
        return;
    }

    ResultReceiver callbacks = intent.getExtras().getParcelable(EXTRA_CALLBACKS);
    ...

The second application must consume the IntentService from the first using Intent in my case.

public class SendActivity extends AppCompatActivity {


private ResultReceiver callbacks = new ResultReceiver(new Handler()) {
    @Override
    protected void onReceiveResult(int resultCode, Bundle resultData) {
        super.onReceiveResult(resultCode, resultData);
        System.out.println(String.format("%d, %s", resultCode, resultData.toString()));
    }
};

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

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("text/plain");
            intent.putStringExtra("TEXT", "Text");
            intent.putExtra("CALLBACKS", callbacks);
            intent.setClassName("com.service", "com.service.MyIntentService");
            startService(intent);
        }
    });
}

And I got:

E/Parcel: Class not found when unmarshalling: com.service.demo.SendActivity$1
   java.lang.ClassNotFoundException: com.service.demo.SendActivity$1
       at java.lang.Class.classForName(Native Method)
       at java.lang.Class.forName(Class.java:309)
       at android.os.Parcel.readParcelableCreator(Parcel.java:2281)
       at android.os.Parcel.readParcelable(Parcel.java:2245)
       at android.os.Parcel.readValue(Parcel.java:2152)
       at android.os.Parcel.readArrayMapInternal(Parcel.java:2485)
       at android.os.BaseBundle.unparcel(BaseBundle.java:221)
       at android.os.Bundle.getParcelable(Bundle.java:755)
       at com.servce.MyIntentService.onHandleIntent(MyIntentService.java:64)
       at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:135)
       at android.os.HandlerThread.run(HandlerThread.java:61)
    Caused by: java.lang.ClassNotFoundException: Didn't find class "com.service.demo.SendActivity$1" on path: DexPathList[[zip file "/data/app/com.service-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
       at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
       at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
       at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
       at java.lang.Class.classForName(Native Method) 
       at java.lang.Class.forName(Class.java:309) 
       at android.os.Parcel.readParcelableCreator(Parcel.java:2281) 
       at android.os.Parcel.readParcelable(Parcel.java:2245) 
       at android.os.Parcel.readValue(Parcel.java:2152) 
       at android.os.Parcel.readArrayMapInternal(Parcel.java:2485) 
       at android.os.BaseBundle.unparcel(BaseBundle.java:221) 
       at android.os.Bundle.getParcelable(Bundle.java:755) 
       at com.service.MyIntentService.onHandleIntent(MyIntentService.java:64) 
       at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65) 
       at android.os.Handler.dispatchMessage(Handler.java:102) 
       at android.os.Looper.loop(Looper.java:135) 
       at android.os.HandlerThread.run(HandlerThread.java:61) 
    Suppressed: java.lang.ClassNotFoundException: com.service.demo.SendActivity$1
       at java.lang.Class.classForName(Native Method)
       at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
       at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
       at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
            ... 14 more
    Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
01-28 18:16:11.498 12168-12193/com.service E/AndroidRuntime: FATAL EXCEPTION: IntentService[MyIntentService]
       Process: com.service, PID: 12168
       android.os.BadParcelableException: ClassNotFoundException when unmarshalling: com.service.demo.SendActivity$1
           at android.os.Parcel.readParcelableCreator(Parcel.java:2295)
           at android.os.Parcel.readParcelable(Parcel.java:2245)
           at android.os.Parcel.readValue(Parcel.java:2152)
           at android.os.Parcel.readArrayMapInternal(Parcel.java:2485)
           at android.os.BaseBundle.unparcel(BaseBundle.java:221)
           at android.os.Bundle.getParcelable(Bundle.java:755)
           at com.servce.MyIntentService.onHandleIntent(MyIntentService.java:64)
           at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
           at android.os.Handler.dispatchMessage(Handler.java:102)
           at android.os.Looper.loop(Looper.java:135)
           at android.os.HandlerThread.run(HandlerThread.java:61)