我需要调用onHandleIntent并从app1接收一个值,并在没有用户通知的情况下在app2中获得结果(不同的应用程序,一个活动将调用其他应用程序的servixe)。 但是,当onHandleIntent在此行中开始时:
mReceiver = workIntent.getParcelableExtra("receiver");
带来错误
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]]
我实际上读过:
https://stackoverflow.com/questions/28589509/android-e-parcel-class-not-found-when-unmarshalling-only-on-samsung-tab3
但我仍然无法找到解决方案
这是代码:
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) {
}
}
}
清单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>
答案 0 :(得分:1)
这里可能会发生一些事情。首先,您需要一个接收器才能从您的服务中获取有关主要活动的信息。要创建接收器,请创建一个如下所示的新类:
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);
}
}
然后,在主活动中,使用以下代码创建接收器onCreate():
mReceiver = new myReceiver(new Handler());
mReceiver.setReceiver(this);
这对于实际获取任何数据是必要的。您还需要将接收器作为额外intent.putExtra("receiver", mReceiver);
传递给定义意图的部分。然后,在IntentService中,您可以使用workIntent.getParcelableExtra("receiver").send(1, b)
将您的信息发送回MainActivity(其中1是您想要的结果代码,b是您要发回的所有信息的Bundle)。
至于为什么永远不会调用您的服务,我会检查here以确定您是否实际向服务发送了正确的意图。您还应尝试从服务中删除intent-filter,因为这是不必要的。
通常,您不希望在服务中创建新的MainActivity,而是使用接收器。否则你的服务似乎是正确的,所以我会确保你检查你试图传入的意图。如果仍然失败,这里有很多其他线程来解决这个问题。
我是新来的,如果您需要更多帮助,请告诉我。