3

I want to pass a data from a service to a fragment . Here my code in the service

public void putInfo(){
    Intent trIntent = new Intent("android.intent.action.MAIN");
    trIntent.setClass(this,WelcomeActivity.class);
    trIntent.putExtra("info", strengthAmplitude+"");
    trIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(trIntent);
    }

And then I want to use this data ("info") in my fragment (InfoFragment) like that

  String msg =  getActivity().getIntent().getExtras().getString("info");

I am getting errors.

E/AndroidRuntime(25036): java.lang.RuntimeException: Unable to start service 
com.BackupAndTools.service.InfoService@41e6b498 with Intent { 
cmp=com.BackupAndTools/.service.InfoService }: 
android.content.ActivityNotFoundException: Unable to find explicit activity class 
{com.BackupAndTools/com.BackupAndTools.views.InfoFragment}; have you declared this 
activity in your AndroidManifest.xml?

while it is a fragment not an activity to be declared in AndroidManifets.xml !!

8
  • I think you have to implement one Activity and through that activity call the Fragment, your problem will solve marya Commented May 13, 2014 at 10:21
  • In fact that is what I do actually. I call the fragment (InfoFragment) from an Activity (WelcomeActivity).I edited my code as you suggested so take a look pleaze. But I still can't get the data because I have a NullPointerException in this line String msg = getActivity().getIntent().getExtras().getString("info"); in the fragment
    – Marya
    Commented May 13, 2014 at 10:31
  • Ya, then call that Activity from Service also. Commented May 13, 2014 at 10:31
  • try putting String msg = ((WelcomeActivity)getActivity()).getIntent().getExtras().getString("info"); But you should also send an intent to the parent activity, not the fragment itself. Commented May 13, 2014 at 10:33
  • @Marya There should be other way also marya, if its feasible to your code Commented May 13, 2014 at 10:34

1 Answer 1

0

Try Adding a receiver in your Fragment :

class CustomReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String data= intent.getExtra(DATA);
    }
}

Now in your Fragment register the receiver with a intent filter:

@Override
public void onStart() {
    super.onStart();
    receiver = new CustomReceiver();
    IntentFilter filter = new IntentFilter();
    filter.addAction(MY_TRIGGER);
    getActivity().registerReceiver(receiver, filter);
}

Now in your Service:

public class NumberThoery extends Service {


public static String MY_TRIGGER = "trigger";

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    intent.putExtra(DATA,"test");
    sendBroadcast(intent,MY_TRIGGER);
    return START_STICKY;
}



@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.