Thursday, March 1, 2012

Turn on your mobile in vibrate mode.

This code is for turn on vibrate mode in android.


Step 1) Your main java code is here.

 

package image.switcher;

import android.app.Activity;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class ImageswitcherActivity extends Activity {
       AudioManager mode;
      
    /** Called when the activity is first created.
     * @return */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        Button normal=(Button)findViewById(R.id.button2);
        Button silent=(Button)findViewById(R.id.button1);
        final TextView tv=(TextView)findViewById(R.id.textView1);
       
        normal.setOnClickListener(new OnClickListener() {
                    
                     public void onClick(View arg0) {
                           // TODO Auto-generated method stub
                           tv.setText("The Mobile in Silent Mode");
                //mode.setRingerMode(AudioManager.RINGER_MODE_SILENT);
                           
                           ((AudioManager) getSystemService(AUDIO_SERVICE))
                             .setRingerMode(AudioManager.RINGER_MODE_VIBRATE);

                                                                    
                     }
              });
     
        silent.setOnClickListener(new OnClickListener() {
                    
                     public void onClick(View arg0) {
                           // TODO Auto-generated method stub
                    
                           tv.setText("The Mobile in Normal Mode");
                         //mode.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
                           ((AudioManager) getSystemService(AUDIO_SERVICE))
                             .setRingerMode(AudioManager.RINGER_MODE_NORMAL);
                    
                           
                     }
              });
       
       
       
    }

}



Step 2) Put below code in main.xml file


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weightSum="1">
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
/>

<Button
 android:id="@+id/button1"
 android:layout_height="wrap_content"
 android:text="Normal"
 android:layout_width="97dp"></Button>
<Button
android:id="@+id/button2"
android:layout_height="wrap_content"
android:text="Vibrate"
android:layout_width="96dp"></Button>
<TextView android:id="@+id/textView1" android:text="TextView" android:layout_height="wrap_content" android:layout_width="206dp" android:layout_weight="0.06"></TextView>

</LinearLayout>


1 comment:

  1. It's working and thanks for giving this code on your website.

    ReplyDelete