Search code examples
javaandroidandroid-intentintentserviceandroid-intentservice

How to call OnhandleIntent from another class?


i have two class Register.java and fragment1.java this is Register.java

`import com.google.android.gms.gcm.GoogleCloudMessaging;
import com.google.android.gms.iid.InstanceID;
import java.io.IOException;


public class RegisterApp extends IntentService {
private static final String TAG = "RegIntentService";
public RegisterApp() {
    super(TAG);
}


@Override
protected void onHandleIntent(Intent intent) {
    InstanceID instanceID = InstanceID.getInstance(this);
    Toast.makeText(this,"inst", Toast.LENGTH_LONG).show();


    try {
      String  token = instanceID.getToken(getString(R.string.gcm_defaultSenderId), GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
        Toast.makeText(this,"before token", Toast.LENGTH_LONG).show();
        Toast.makeText(this, "New token is" + token, Toast.LENGTH_LONG).show();
    } catch (IOException e) {

        e.printStackTrace();

    }

}
}

and this is fragment.java

import com.google.android.gms.gcm.GoogleCloudMessaging;

 public class Fragment1 extends Fragment {


EditText clg,password;
Context ctx;


@Override
public View onCreateView(LayoutInflater inflater,
        @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View v =inflater.inflate(R.layout.fragment1_layout, container, false);
        v.setBackgroundColor(Color.WHITE);

        clg=(EditText) v.findViewById(R.id.ClgIdIn);
        password=(EditText) v.findViewById(R.id.passwordIn);
        Button  btn=(Button) v.findViewById(R.id.btn_login);

        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                String clgid=clg.getText().toString();
                String pass=password.getText().toString();
                String method="login";
                BackgroundTask BK=new BackgroundTask(getActivity());
                RegisterApp RA =new RegisterApp();


                BK.execute(method,clgid,pass);
            //  BK.sendSession(clgid,pass);
            //  getActivity().finish();
                Toast.makeText(getActivity(),"came here", Toast.LENGTH_LONG).show();
            }
        });

        return v;
}

}

i want to call method OnhandleIntent from fragment1 but dont know how to call it, i tried using

startServices() 

but i have no idea how to pass intent.


Solution

  • Intent i = new Intent(getActivity(), RegisterApp.class);
    getActivity().startService(i);
    

    And, do not forget to declare your Service in AndroidManifest