如何从另一个类调用OnhandleIntent?

时间:2016-04-27 06:34:42

标签: java android android-intent intentservice android-intentservice

我有两个类Register.java和fragment1.java 这是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();

    }

}
}

这是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;
}

}

我想从fragment1调用方法OnhandleIntent但不知道如何调用它,我尝试使用

startServices() 

但我不知道如何传递意图。

1 个答案:

答案 0 :(得分:1)

AndroidManifest

并且,不要忘记在{{1}}

中声明您的服务