使用ResultReceiver在应用程序之间进行Android通信

时间:2016-01-28 16:45:56

标签: android android-intent android-service parcelable intentservice

我有两个申请。第一个应用程序是简单的服务应用程序,它在后台完成一些工作。我决定使用IntentService

@Override
protected void onHandleIntent(Intent intent) {

    if (intent == null) {
        return;
    }

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

在我的情况下,第二个应用程序必须使用第一个使用Intent的IntentService

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

我得到了:

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)

0 个答案:

没有答案