Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add vibration and ringtone when launch application home page android

i want to add vibration and a play music in my application home page means when my application launch it vibrate and play a ring tone until i not press button for going to another activity means it stops the vibration and sound after when i go to other activity...my manifest file is given below..

 <?xml version="1.0" encoding="utf-8"?>
  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.androidpeople.tab"
   android:versionCode="1"
   android:versionName="1.0">
     <uses-permission android:name="android.permission.INTERNET" />


      <application android:icon="@drawable/icon" android:label="@string/app_name"
                        android:debuggable= "true">
            <uses-library android:name="com.google.android.maps" />
            <activity android:name=".SplashScreen"
              android:label="@string/app_name">
            <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            </activity>
             <activity android:name="currentlocmap"/>
             <activity android:name="filter"/>
             <activity android:name="keywordsearch"/>
             <activity android:name="keywordxmlparsing" />
             <activity android:name="Artistspacedetailview" />
             <activity android:name="loadingimage"/>
             <activity android:name="keywordpagetabs"/>
             <activity android:name="filterpagetabs"/>
             <activity android:name="artistspacedetailviewTab"/>


              </application>
              <supports-screens android:anyDensity="true"
                android:largeScreens="true"
                android:normalScreens="true"
                android:smallScreens="true" />

like image 375
SRam Avatar asked Dec 02 '22 02:12

SRam


1 Answers

So first, place your ringtone/sound clip in the raw folder under res/raw. For this example, I'm going to call it sound_clip.

import android.media.MediaPlayer;
import android.os.Vibrator;

private Vibrator vib;
private MediaPlayer mp;
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mp = MediaPlayer.create(this, R.raw.sound_clip);
    vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
    vib.vibrate(500);
    mp.start();
    Button button = (Button) findViewById(R.id.button);
    button.setOnClickListener(this);
}

public void onClick(View v){
    mp.stop();
    vib.cancel();
}
like image 83
Otra Avatar answered Dec 03 '22 15:12

Otra