Vibrator doesn't work - Htc Desire Froyo 2.2

Put your problem here if it does not fit any of the other categories.

Vibrator doesn't work - Htc Desire Froyo 2.2

Postby hamorabi » Mon Sep 06, 2010 6:40 pm

Hi,

I am trying to activate the vibrate on my htc desire phone but from some reason it doesn't work.
any idea what could be the problem ?
Code: Select all
package com.example.vibrator;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Vibrator;

public class vibrator extends Activity {
    /** Called when the activity is first created. */
   Vibrator myvibrator;
   Context context;
   private void initVibrator() {
       try {
         myvibrator=(Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
      } catch (Exception e) {
          //this is a test
      }
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initVibrator();
      if (myvibrator != null) {
         try {
            this.myvibrator.vibrate(1000);
         } catch (Exception e) {}
      }
    }
}


Regards,
Hamorabi
hamorabi
Freshman
Freshman
 
Posts: 9
Joined: Mon Sep 06, 2010 6:28 pm

Top

Re: Vibrator doesn't work - Htc Desire Froyo 2.2

Postby jonbonazza » Tue Sep 07, 2010 12:23 am

Try handling your exceptions (I.e. creating a Toast message).

Also, post up your logcat.
jonbonazza
Master Developer
Master Developer
 
Posts: 665
Joined: Thu Jul 15, 2010 2:58 pm
Location: WV USA

Re: Vibrator doesn't work - Htc Desire Froyo 2.2

Postby sodino » Tue Sep 07, 2010 7:04 am

Code: Select all
public class VibratorAct extends Activity {
   /** Called when the activity is first created. */
   Vibrator myvibrator;
   Context context;

   private void initVibrator() {
      try {
         // myvibrator = (Vibrator) context
         // .getSystemService(Context.VIBRATOR_SERVICE);
         //NOTE
         myvibrator = (Vibrator) getApplication().getSystemService(
               Service.VIBRATOR_SERVICE);
      } catch (Exception e) {
         // this is a test
         e.printStackTrace();
      }
   }

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      initVibrator();
      if (myvibrator != null) {
         try {
            this.myvibrator.vibrate(1000);
         } catch (Exception e) {
            e.printStackTrace();
         }
      }
   }
}


then add "<uses-permission android:name="android.permission.VIBRATE"></uses-permission>" at AndroidManifest.xml
sodino
Freshman
Freshman
 
Posts: 3
Joined: Tue Sep 07, 2010 5:23 am

Re: Vibrator doesn't work - Htc Desire Froyo 2.2

Postby sodino » Tue Sep 07, 2010 7:14 am

before you use a param, you should initialize it

add "context = getApplicationContext();" before run
"
myvibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
"

will be ok.
sodino
Freshman
Freshman
 
Posts: 3
Joined: Tue Sep 07, 2010 5:23 am

Re: Vibrator doesn't work - Htc Desire Froyo 2.2

Postby hamorabi » Wed Sep 08, 2010 9:46 pm

Thanks - initializing the parameter helped :-)
hamorabi
Freshman
Freshman
 
Posts: 9
Joined: Mon Sep 06, 2010 6:28 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 12 guests