Back to project page ScoponeDaPolso.
The source code is released under:
GNU General Public License
If you think the Android project ScoponeDaPolso listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package org.gdg.bari.scopone.fragment; /* ww w .ja v a 2s. c o m*/ import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.Toast; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import org.gdg.bari.entities.Card; import org.gdg.bari.scopone.R; import org.gdg.bari.scopone.activity.MobileMainActivity; import org.gdg.bari.scopone.util.LogUtil; import java.lang.reflect.Type; import java.util.List; public class GameFragment extends Fragment implements View.OnClickListener{ private static final String TAG = GameFragment.class.getSimpleName(); private MobileMainActivity mActivity; private OnGameFragmentInteractionListener mListener; private Button doneBtn; private CardReceiver cardReceiver; private IntentFilter shootedCardIntentFilter; public static final String ACTION_SHOOTED_CARD = "shootedCard"; private Gson gson; public Button getDoneBtn(){ return this.doneBtn; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); shootedCardIntentFilter = new IntentFilter(ACTION_SHOOTED_CARD); cardReceiver = new CardReceiver(); gson = new Gson(); } @Override public void onStart() { super.onStart(); getActivity().registerReceiver(cardReceiver, shootedCardIntentFilter); } @Override public void onStop() { super.onStop(); getActivity().unregisterReceiver(cardReceiver); } @Override public void onAttach(Activity activity) { super.onAttach(activity); mActivity = (MobileMainActivity) activity; try { mListener = (OnGameFragmentInteractionListener) mActivity; } catch (ClassCastException ex) { LogUtil.w(TAG, TAG + " ClassCastException"); LogUtil.e(TAG, Log.getStackTraceString(ex)); throw new ClassCastException(activity.toString() + " must implement OnFragmentInteractionListener"); } } @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_game, container, false); doneBtn = (Button) v.findViewById(R.id.doneBtn); doneBtn.setOnClickListener(this); return v; } @Override public void onClick(View v) { switch(v.getId()){ case R.id.doneBtn: mListener.onNextTurnClicked(this.doneBtn); break; } } public interface OnGameFragmentInteractionListener { public void onNextTurnClicked(Button btn); } //BROADCAST RECEIVER public class CardReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String jsonCard = intent.getExtras().getString("shootedCardJson"); Log.d(TAG, "GameFragment:OnReceive()"); Log.d(TAG, jsonCard); //Type cardListType = new TypeToken<Card>() {}.getType(); Card playedCard = gson.fromJson(jsonCard, Card.class); Toast.makeText(getActivity().getApplicationContext(), "giocato " + playedCard.toString(), Toast.LENGTH_LONG).show(); } } }