Company project needs, need to add alarm function to the software,
Click on the hand-drawn alarm icon to start shaking and beep,
Using the Layerlist,drawable,vibrator,soundpool
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 (2, 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[]{100,2000,500,2500},-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
<?xml version= "1.0" encoding= "Utf-8"?>
<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
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" >
<com.seable.withome.myview.titlebar
Android:id= "@+id/titlebar"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
My:titlename= "Smart home"/>
<linearlayout
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:orientation= "Horizontal"
android:layout_margintop= "50DP"
android:padding= "10DP" >
<linearlayout
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" >
<textview
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"/>
<textview
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"/>
</LinearLayout>
</LinearLayout>
<textview
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 Alarm"
Android:textsize= "25SP"
Android:textcolor= "@android: Color/white"/>
</RelativeLayout>
CSDN Download http://download.csdn.net/detail/kan1kan5/8217051
Android alarm function, alarm ring tone, mobile phone starts shaking