公司项目需求,需要给软件加入报警功能,
点击手绘的报警图标,开始震动,并且发出报警铃音,
使用了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="智慧家园" />
    <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="紧急救援"
                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="对讲报警"
                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="紧急报警"
        android:textSize="25sp"
        android:textColor="@android:color/white" />
</RelativeLayout>
csdn下载 http://download.csdn.net/detail/kan1kan5/8217051
原文:http://blog.csdn.net/kan1kan5/article/details/41680041