Following the development of an app, I find myself in a fragment with some buttons. I want a vibration to occur when I click, but when I enter it, it says "can not resolve VIBRATOR_SERVICE" ....
public class AsientosFragment extends Fragment implements View.OnTouchListener, View.OnClickListener {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
ImageView btnup;
ImageView btndown;
ImageView btnrear;
ImageView btnfront;
RadioButton rb1;
RadioButton rb2;
RadioButton rb3;
RadioButton rb4;
RadioButton rb5;
ImageView imgasiento;
int seleccionRadioBoton=1;
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
public AsientosFragment() {
// Required empty public constructor
}
// TODO: Rename and change types and number of parameters
public static AsientosFragment newInstance(String param1, String param2) {
AsientosFragment fragment = new AsientosFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
System.out.println("AQUIIIIII");
View view=inflater.inflate(R.layout.fragment_asientos,container,false);
btnup=(ImageView)view.findViewById(R.id.btnup);
btndown=(ImageView)view.findViewById(R.id.btndown);
btnrear=(ImageView)view.findViewById(R.id.btnrear);
btnfront=(ImageView)view.findViewById(R.id.btnfront);
rb1=(RadioButton)view.findViewById(R.id.rb1);
rb2=(RadioButton)view.findViewById(R.id.rb2);
rb3=(RadioButton)view.findViewById(R.id.rb3);
rb4=(RadioButton)view.findViewById(R.id.rb4);
rb5=(RadioButton)view.findViewById(R.id.rb5);
imgasiento=(ImageView)view.findViewById(R.id.imgasiento);
rb1.setOnClickListener(this);
rb2.setOnClickListener(this);
rb3.setOnClickListener(this);
rb4.setOnClickListener(this);
rb5.setOnClickListener(this);
btnup.setOnTouchListener(this);
btndown.setOnTouchListener(this);
btnrear.setOnTouchListener(this);
btnfront.setOnTouchListener(this);
return view;
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
MainActivity mainAct = (MainActivity) this.getActivity();
Vibrator v = (Vibrator) mainAct.getSystemService(VIBRATOR_SERVICE);
if (mainAct.myThreadConnected != null) {
switch (view.getId()){
case R.id.btnup:
switch (seleccionRadioBoton){
case 1:
if (motionEvent.getAction()==MotionEvent.ACTION_DOWN){
byte[] envio = "3".getBytes();
btnup.setImageResource(R.drawable.up2);
mainAct.enviarCadena(envio);
}else if(motionEvent.getAction()==MotionEvent.ACTION_UP){
byte[] envio = "h".getBytes();
btnup.setImageResource(R.drawable.up1);
mainAct.enviarCadena(envio);
}
break;
case 2:
if (motionEvent.getAction()==MotionEvent.ACTION_DOWN){
byte[] envio = "7".getBytes();
btnup.setImageResource(R.drawable.up2);
mainAct.enviarCadena(envio);
}else if(motionEvent.getAction()==MotionEvent.ACTION_UP){
byte[] envio = "ñ".getBytes();
btnup.setImageResource(R.drawable.up1);
mainAct.enviarCadena(envio);
}
break;
case 3:
if (motionEvent.getAction()==MotionEvent.ACTION_DOWN){
byte[] envio = "q".getBytes();
btnup.setImageResource(R.drawable.up2);
mainAct.enviarCadena(envio);
}else if(motionEvent.getAction()==MotionEvent.ACTION_UP){
byte[] envio = "v".getBytes();
btnup.setImageResource(R.drawable.up1);
mainAct.enviarCadena(envio);
}
break;
case 4:
if (motionEvent.getAction()==MotionEvent.ACTION_DOWN){
byte[] envio = "t".getBytes();
btnup.setImageResource(R.drawable.up2);
mainAct.enviarCadena(envio);
}else if(motionEvent.getAction()==MotionEvent.ACTION_UP){
byte[] envio = "Q".getBytes();
btnup.setImageResource(R.drawable.up1);
mainAct.enviarCadena(envio);
}
break;
case 5:
if (motionEvent.getAction()==MotionEvent.ACTION_DOWN){
byte[] envio = "o".getBytes();
btnup.setImageResource(R.drawable.up2);
mainAct.enviarCadena(envio);
}else if(motionEvent.getAction()==MotionEvent.ACTION_UP){
byte[] envio = "T".getBytes();
btnup.setImageResource(R.drawable.up1);
mainAct.enviarCadena(envio);
}
break;
}
break;
case R.id.btndown:
switch (seleccionRadioBoton){
case 1:
if (motionEvent.getAction()==MotionEvent.ACTION_DOWN){
byte[] envio = "4".getBytes();
btndown.setImageResource(R.drawable.down2);
mainAct.enviarCadena(envio);
}else if(motionEvent.getAction()==MotionEvent.ACTION_UP){
byte[] envio = "j".getBytes();
btndown.setImageResource(R.drawable.down1);
mainAct.enviarCadena(envio);
}
break;
case 2:
if (motionEvent.getAction()==MotionEvent.ACTION_DOWN){
byte[] envio = "8".getBytes();
btndown.setImageResource(R.drawable.down2);
mainAct.enviarCadena(envio);
}else if(motionEvent.getAction()==MotionEvent.ACTION_UP){
byte[] envio = "z".getBytes();
btndown.setImageResource(R.drawable.down1);
mainAct.enviarCadena(envio);
}
break;
case 3:
if (motionEvent.getAction()==MotionEvent.ACTION_DOWN){
byte[] envio = "w".getBytes();
btndown.setImageResource(R.drawable.down2);
mainAct.enviarCadena(envio);
}else if(motionEvent.getAction()==MotionEvent.ACTION_UP){
byte[] envio = "b".getBytes();
btndown.setImageResource(R.drawable.down1);
mainAct.enviarCadena(envio);
}
break;
case 4:
if (motionEvent.getAction()==MotionEvent.ACTION_DOWN){
byte[] envio = "y".getBytes();
btndown.setImageResource(R.drawable.down2);
mainAct.enviarCadena(envio);
}else if(motionEvent.getAction()==MotionEvent.ACTION_UP){
byte[] envio = "w".getBytes();
btndown.setImageResource(R.drawable.down1);
mainAct.enviarCadena(envio);
}
break;
case 5:
if (motionEvent.getAction()==MotionEvent.ACTION_DOWN){
byte[] envio = "p".getBytes();
btndown.setImageResource(R.drawable.down2);
mainAct.enviarCadena(envio);
}else if(motionEvent.getAction()==MotionEvent.ACTION_UP){
byte[] envio = "Y".getBytes();
btndown.setImageResource(R.drawable.down1);
mainAct.enviarCadena(envio);
}
break;
}
break;
case R.id.btnrear:
switch (seleccionRadioBoton){
case 1:
if (motionEvent.getAction()==MotionEvent.ACTION_DOWN){
byte[] envio = "2".getBytes();
btnrear.setImageResource(R.drawable.left2);
mainAct.enviarCadena(envio);
}else if(motionEvent.getAction()==MotionEvent.ACTION_UP){
byte[] envio = "g".getBytes();
btnrear.setImageResource(R.drawable.left1);
mainAct.enviarCadena(envio);
}
break;
case 2:
if (motionEvent.getAction()==MotionEvent.ACTION_DOWN){
byte[] envio = "6".getBytes();
btnrear.setImageResource(R.drawable.left2);
mainAct.enviarCadena(envio);
}else if(motionEvent.getAction()==MotionEvent.ACTION_UP){
byte[] envio = "l".getBytes();
btnrear.setImageResource(R.drawable.left1);
mainAct.enviarCadena(envio);
}
break;
case 3:
if (motionEvent.getAction()==MotionEvent.ACTION_DOWN){
byte[] envio = "0".getBytes();
btnrear.setImageResource(R.drawable.left2);
mainAct.enviarCadena(envio);
}else if(motionEvent.getAction()==MotionEvent.ACTION_UP){
byte[] envio = "c".getBytes();
btnrear.setImageResource(R.drawable.left1);
mainAct.enviarCadena(envio);
}
break;
case 4:
if (motionEvent.getAction()==MotionEvent.ACTION_DOWN){
byte[] envio = "r".getBytes();
btnrear.setImageResource(R.drawable.left2);
mainAct.enviarCadena(envio);
}else if(motionEvent.getAction()==MotionEvent.ACTION_UP){
byte[] envio = "m".getBytes();
btnrear.setImageResource(R.drawable.left1);
mainAct.enviarCadena(envio);
}
break;
case 5:
if (motionEvent.getAction()==MotionEvent.ACTION_DOWN){
byte[] envio = "i".getBytes();
btnrear.setImageResource(R.drawable.left2);
mainAct.enviarCadena(envio);
}else if(motionEvent.getAction()==MotionEvent.ACTION_UP){
byte[] envio = "R".getBytes();
btnrear.setImageResource(R.drawable.left1);
mainAct.enviarCadena(envio);
}
break;
}
break;
case R.id.btnfront:
switch (seleccionRadioBoton){
case 1:
if (motionEvent.getAction()==MotionEvent.ACTION_DOWN){
byte[] envio = "1".getBytes();
btnfront.setImageResource(R.drawable.right2);
mainAct.enviarCadena(envio);
}else if(motionEvent.getAction()==MotionEvent.ACTION_UP){
byte[] envio = "f".getBytes();
btnfront.setImageResource(R.drawable.right1);
mainAct.enviarCadena(envio);
}
break;
case 2:
if (motionEvent.getAction()==MotionEvent.ACTION_DOWN){
byte[] envio = "5".getBytes();
btnfront.setImageResource(R.drawable.right2);
mainAct.enviarCadena(envio);
}else if(motionEvent.getAction()==MotionEvent.ACTION_UP){
byte[] envio = "k".getBytes();
btnfront.setImageResource(R.drawable.right1);
mainAct.enviarCadena(envio);
}
break;
case 3:
if (motionEvent.getAction()==MotionEvent.ACTION_DOWN){
byte[] envio = "9".getBytes();
btnfront.setImageResource(R.drawable.right2);
mainAct.enviarCadena(envio);
}else if(motionEvent.getAction()==MotionEvent.ACTION_UP){
byte[] envio = "x".getBytes();
btnfront.setImageResource(R.drawable.right1);
mainAct.enviarCadena(envio);
}
break;
case 4:
if (motionEvent.getAction()==MotionEvent.ACTION_DOWN){
byte[] envio = "e".getBytes();
btnfront.setImageResource(R.drawable.right2);
mainAct.enviarCadena(envio);
}else if(motionEvent.getAction()==MotionEvent.ACTION_UP){
byte[] envio = "n".getBytes();
btnfront.setImageResource(R.drawable.right1);
mainAct.enviarCadena(envio);
}
break;
case 5:
if (motionEvent.getAction()==MotionEvent.ACTION_DOWN){
byte[] envio = "w".getBytes();
btnfront.setImageResource(R.drawable.right2);
mainAct.enviarCadena(envio);
}else if(motionEvent.getAction()==MotionEvent.ACTION_UP){
byte[] envio = "E".getBytes();
btnfront.setImageResource(R.drawable.right1);
mainAct.enviarCadena(envio);
}
break;
}
break;
}
}
return true;
}
@Override
public void onClick(View view) {
// Is the button now checked?
System.out.println("IEEEEEE");
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.rb1:
if (checked) {
seleccionRadioBoton=1;
imgasiento.setImageResource(R.drawable.butaca1);
System.out.println(String.valueOf(seleccionRadioBoton));
}
break;
case R.id.rb2:
if (checked){
seleccionRadioBoton=2;
imgasiento.setImageResource(R.drawable.butaca2);
System.out.println(String.valueOf(seleccionRadioBoton));
}
break;
case R.id.rb3:
if (checked){
seleccionRadioBoton=3;
imgasiento.setImageResource(R.drawable.butaca3);
System.out.println(String.valueOf(seleccionRadioBoton));
}
break;
case R.id.rb4:
if (checked){
seleccionRadioBoton=4;
imgasiento.setImageResource(R.drawable.butaca4);
System.out.println(String.valueOf(seleccionRadioBoton));
}
break;
case R.id.rb5:
if (checked){
seleccionRadioBoton=5;
imgasiento.setImageResource(R.drawable.butaca5);
System.out.println(String.valueOf(seleccionRadioBoton));
}
break;
}
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
The vibrator is in the ontouch method ... but I can not get it right ....