Android alarm function, alarm bell sound, mobile phone starting to vibrate

Source: Internet
Author: User
Tags getcolor

Android alarm function, alarm bell sound, mobile phone starting to vibrate

To meet the company's project requirements, you must add the alarm function to the software,

Click the hand-drawn alarm icon to start shaking and send an alarm bell,

Layerlist, drawable, Vibrator, and soundpool are used.





Public class SosMainActivity extends BaseActivity {


Private TextView sosTv1, sosTv2, sosTv3;
Private int blueColor, whiteColor, transparent;

Private Vibrator vibrator;
Private SoundPool soundPool;
Int hit;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
// TODO Auto-generated method stub
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_sos );

SosTv1 = (TextView) findViewById (R. id. sostv1 );
SosTv2 = (TextView) findViewById (R. id. sostv2 );
SosTv3 = (TextView) findViewById (R. id. sostv3 );


Vibrator = (Vibrator) getSystemService (VIBRATOR_SERVICE );

SoundPool = new SoundPool (1, AudioManager. STREAM_MUSIC, 100 );
Hit = soundPool. load (context, R. raw. alertmp3, 0 );

BlueColor = getResources (). getColor (R. color. bluetext );
WhiteColor = getResources (). getColor (android. R. color. white );
Transparent = getResources (). getColor (android. R. color. transparent );

BtnSelected1 ();
SosTv1.setOnClickListener (new OnClickListener (){

@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
BtnSelected1 ();
}
});

SosTv2.setOnClickListener (new OnClickListener (){

@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
BtnSelected2 ();
}
});

SosTv3.setOnLongClickListener (new MyOnclick ());

}

Public class MyOnclick implements OnLongClickListener {
Public MyOnclick (){
}




@ Override
Public boolean onLongClick (View v ){
// TODO Auto-generated method stub
SoundPool. play (hit, 5, 5, 0, 5, (float) 1 );
Vibrator. vibrate (new long [] {,},-1 );

Return false;
}

}

Private void btnSelected1 (){
Drawable back1 = getResources (). getDrawable (R. drawable. btn_safety_right );
SosTv2.setBackgroundDrawable (back1 );
SosTv2.setTextColor (blueColor );


SosTv1.setTextColor (whiteColor );
SosTv1.setBackgroundResource (transparent );

}

Private void btnSelected2 (){
Drawable back1 = getResources (). getDrawable (R. drawable. btn_safety_leftshape );
SosTv1.setBackgroundDrawable (back1 );
SosTv1.setTextColor (blueColor );
SosTv2.setBackgroundResource (transparent );
SosTv2.setTextColor (whiteColor );

}

}


Activity_sos.xml


Xmlns: my = "http://schemas.android.com/apk/res/com.seable.withome"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: gravity = "center_horizontal"
Android: orientation = "vertical">


Android: id = "@ + id/TitleBar"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
My: titleName = "Smart Home"/>


Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: orientation = "horizontal"
Android: layout_marginTop = "50dp"
Android: padding = "10dp">


Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: layout_marginLeft = "10dp"
Android: layout_marginRight = "10dp"
Android: background = "@ drawable/btn_safety_back1"
Android: orientation = "horizontal">


Android: id = "@ + id/sostv1"
Android: layout_width = "0dp"
Android: layout_height = "wrap_content"
Android: layout_weight = "1"
Android: gravity = "center"
Android: padding = "10dp"
Android: text = "Emergency Rescue"
Android: textSize = "@ dimen/txt_20"
Android: textColor = "@ color/white"/>


Android: id = "@ + id/sostv2"
Android: layout_width = "0dp"
Android: layout_height = "wrap_content"
Android: layout_weight = "1"
Android: background = "@ drawable/btn_safety_right"
Android: gravity = "center"
Android: padding = "10dp"
Android: text = "intercom alarm"
Android: textSize = "@ dimen/txt_20"
Android: textColor = "@ color/bluetext"/>





Android: id = "@ + id/sostv3"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_centerInParent = "true"
Android: layout_marginTop = "100dp"
Android: background = "@ drawable/btn_sos_main"
Android: gravity = "center"
Android: paddingTop = "120dp"
Android: text = "Emergency Alert"
Android: textSize = "25sp"
Android: textColor = "@ android: color/white"/>






Csdn download http://download.csdn.net/detail/kan1kan5/8217051

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.