Android Enables automatic conversion of mobile phone vibrations and ringtones based on conditions

Source: Internet
Author: User

Android content:

    • Why is the webpage view of Android displayed on a black screen?
    • In Android, how does one handle changes in the screen direction when the progress dialog box and background thread activity?
    • How does Android call oncreat () multiple times in an activity ()
    • How does Android create a textview with a folding effect?
    • Android app auto-start

In this example, the current mode of the mobile phone is changed based on the "status" of the mobile phone.
When the front of the phone is up, the phone is in the ring state, and when the back of the phone is up, the phone is in the mute state.
To implement this example, you need to use the built-in sensors of the mobile phone and implement corresponding methods to determine the status of the mobile phone. At the same time, you also need to use the audiomanager class of the mobile phone to change the Mobile Phone mode.
First, add the following content to the androidmanifest. xml file:Code:
<Uses-Permission Android: Name = "android. Permission. Vibrate"/>

The function of this line of code is to makeProgramYou can use the vibrate mode of the mobile phone.
The specific implementation code and corresponding comments are given below:
[Java] view plaincopy
Public class ex06_12 extends Activity
{
/* Create a sensormanager object */
Private sensormanager msensormanager01;
Private textview mtextview01;

/* Save the audiomanager mode as a private class member */
Private int strringermode;

/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate)
{
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );

Mtextview01 = (textview) findviewbyid (R. Id. mytextview1 );

/* Create a sensormanager object to obtain the sensor_service */

Try
{
Msensormanager01 = (sensormanager) getsystemservice (context. sensor_service );
}
Catch (exception E)
{
E. printstacktrace ();
}

/* Obtain the existing audiomanager mode */
Getaudiomanagermode ();

/* Display in textview based on the existing audiomanager mode */
Switch (strringermode)
{
/* Normal mode */
Case audiomanager. ringer_mode_normal:
Mtextview01.settext (R. String. str_normal_mode );
Break;
/* Quiet Mode */
Case audiomanager. ringer_mode_silent:
Mtextview01.settext (R. String. str_silent_mode );
Break;
/* Vibration mode */
Case audiomanager. ringer_mode_vibrate:
Mtextview01.settext (R. String. str_vibrate_mode );
Break;
}
}

/* Create a sensorlistener to capture the onsensorchanged event */
Private Final sensoreventlistener msensorlistener = new sensoreventlistener ()
{
Private float x, y, z;

@ Override
Public void onaccuracychanged (sensor, int accuracy)
{
// Todo auto-generated method stub
}

@ Override
Public void onsensorchanged (sensorevent event)
{
// Todo auto-generated method stub
Synchronized (msensormanager01)
{
Switch (event. sensor. GetType ())
{
Case sensor. type_accelerometer:
X = event. Values [0];
Y = event. Values [1];
Z = event. Values [2];
If (z> 9)
{
/* Positive */
// Mtextview01.settext ("face up ");
/* Change to normal mode */
Changetonormalmode ();

/* After calling the change mode, confirm again the mode of the mobile phone */
Switch (strringermode)
{
Case audiomanager. ringer_mode_normal:
Mtextview01.settext (R. String. str_normal_mode );
Break;
Case audiomanager. ringer_mode_silent:
Mtextview01.settext (R. String. str_silent_mode );
Break;
Case audiomanager. ringer_mode_vibrate:
Mtextview01.settext (R. String. str_vibrate_mode );
Break;
}
}
Else if (z <-9)
{
/* Front-down */
// Mtextview01.settext ("face down ");

/* Set to mute mode first */
Changetosilentmode ();

/* Set it to the vibration mode */
Changetovibratemode ();

/* determine the ringtone mode */
switch (strringermode)
{< br>/* normal mode */
case audiomanager. ringer_mode_normal:
mtextview01.settext (R. string. str_normal_mode);
break;
/* mute mode */
case audiomanager. ringer_mode_silent:
mtextview01.settext (R. string. str_silent_mode);
break;
/* vibration mode */
case audiomanager. ringer_mode_vibrate:
mtextview01.settext (R. string. str_vibrate_mode);
break;
}< br> else if (x> 9)
{< br>/* Forward to left */
// mtextview01.settext ("Left Side ");
}< br> else if (x <-9)
{< br>/* front to right */
// mtextview01.settext ("right side");
}< br> else if (Y> 9)
{
/* straighten out the palm of your hand */
// mtextview01.settext ("Standing up ");
}< br> else if (Y <-9)
{< br>/* palm Reverse flip reverse upright */
// mtextview01.settext ("On head ");
}< br> else
{< br>/* others cannot be determined */
// mtextview01.settext ("in ");
}< br> break;
}< BR >}

/* obtain the current audiomanager mode */
private void getaudiomanagermode ()
{< br> try
{< br>/* Create an audiomanager object and obtain audio_service */
audiomanager = (audiomanager) getsystemservice (context. audio_service);
If (audiomanager! = NULL)
{< br>/* ringer_mode_normal | ringer_mode_silent | ringer_mode_vibrate */
strringermode = audiomanager. getringermode ();
}< BR >}< br> catch (exception e)
{< br> mtextview01.settext (E. tostring ();
E. printstacktrace ();
}< BR >}

/* change to mute mode */
private void changetosilentmode ()
{< br> try
{< br> audiomanager = (audiomanager) getsystemservice (context. audio_service);
If (audiomanager! = NULL)
{< br>/* ringer_mode_normal | ringer_mode_silent | ringer_mode_vibrate */
audiomanager. setringermode (audiomanager. ringer_mode_silent);
strringermode = audiomanager. getringermode ();
}< BR >}< br> catch (exception e)
{< br> mtextview01.settext (E. tostring ();
E. printstacktrace ();
}< BR >}

/* change to vibrate mode */
private void changetovibratemode ()
{< br> try
{< br> audiomanager = (audiomanager) getsystemservice (context. audio_service);
If (audiomanager! = NULL)
{< br>/* call the setringermode method and set the mode */
audiomanager. setringermode (audiomanager. ringer_mode_vibrate);
/* ringer_mode_normal | ringer_mode_silent | ringer_mode_vibrate */
strringermode = audiomanager. getringermode ();
}< BR >}< br> catch (exception e)
{< br> mtextview01.settext (E. tostring ();
E. printstacktrace ();
}< BR >}

/* Change to normal mode */
Private void changetonormalmode ()
{
Try
{
Audiomanager = (audiomanager) getsystemservice (context. audio_service );
If (audiomanager! = NULL)
{
/* Ringer_mode_normal | ringer_mode_silent | ringer_mode_vibrate */
Audiomanager. setringermode (audiomanager. ringer_mode_normal );
Strringermode = audiomanager. getringermode ();
}
}
Catch (exception E)
{
Mtextview01.settext (E. tostring ());
E. printstacktrace ();
}
}

@ Override
Protected void onresume ()
{
// Todo auto-generated method stub
Msensormanager01.registerlistener
(
Msensorlistener,
Msensormanager01.getdefasensensor (sensor. type_accelerometer ),
Sensormanager. sensor_delay_normal
);
Super. onresume ();
}

@ Override
Protected void onpause ()
{
// Todo auto-generated method stub

/* rewrite the onpause event and cancel msensorlistener */
msensormanager01.unregisterlistener (msensorlistener);
super. onpause ();
}< BR >}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.