Android警示功能,警示鈴音,手機開始震動

來源:互聯網
上載者:User

Android警示功能,警示鈴音,手機開始震動

公司項目需求,需要給軟體加入警示功能,

點擊手繪的警示表徵圖,開始震動,並且發出警示鈴音,

使用了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



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="智慧家園" />



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="緊急救援"
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="對講警示"
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="緊急警示"
android:textSize="25sp"
android:textColor="@android:color/white" />






csdn下載 http://download.csdn.net/detail/kan1kan5/8217051

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.