バイブレーションを使う(1)
今回NotificationManagerクラスを使用してのバイブレーションです
Notification Managerは今後詳しく扱いますが
基本的にはユーザーに通知を与える機能です
今回は通知にバイブレーションのみを割り当てて動作させています
ほかにもステータスバーに通知やサウンドの割り当てなど
様々なことが可能です
NotificationManagerはNotificationを管理するのマネージャークラスです
つまり、Notificationクラスを生成しNotification.vibrateにバイブレーションパターンを設定します
それを管理しているNotificationManagerメソッドを使って バイブレーションを動作させます
まずはレイアウトにボタンを配置します
今回はスタートボタンのみです
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="指定時間ON"/>
</LinearLayout>次にMainActivityです
スタートボタンの動作を設定しています
MainActivity.java
package blog.test;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity implements View.OnClickListener{
private Button btn01;
private NotificationManager notifManager;
private Notification notif;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn01 = (Button)findViewById(R.id.button1);
btn01.setOnClickListener(this);
notifManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notif = new Notification();
//時間の指定
notif.vibrate = new long[]{1000, 200, 700, 200, 400, 200};
}
//ボタンを押したときの動作
public void onClick(View v) {
if(v == btn01) {
notifManager.notify(R.string.app_name, notif);
}
}
}
なお今回はハードウェアのバイブレーションを使う権限を追加しなくても動作します
今回からgalaxyではなくxperiaでの動作確認になっています
ちゃんと動作しましたww
スポンサードリンク


