AndroidでSHARP製の端末で歩数計から1時間毎とクリア時の更新を受け取ります。
【準備】
1.「SHARP SDK AddOn Android 2.2版のインストール方法」で「SHARP SDK AddOn Android 2.2版」をインストールしておきます。
【手順1】
1.「Androidプロジェクトの作成・実行方法(バージョン別一覧)」のAndroidプロジェクトの作成手順で、「AndroidSample21-011-SharpPedometerReceiverMeasureHourAndClear」といプロジェクトを作成。
※プロジェクトの設定は以下の通り。
※他の項目は任意。
| 項目名 | 項目に設定する値 |
| アプリケーション名(Application Name) | com.example.androidsample21_011 |
| プロジェクト名(Project Name) | AndroidSample21-011-SharpPedometerReceiverMeasureHourAndClear |
| パッケージ名(Package Name) | com.example.androidsample21_011 |
| Minimum Required SDK | API 8 |
| Target SDK | API 8 |
| Compile With | sharp_addon (SHARP Corporation) (API 8) |
| Theme | None |
【手順2】
1.「AndroidManifest.xml」は以下の通り。
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidsample21_011"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<uses-library android:name="jp.co.sharp.android.hardware" />
<activity
android:name="com.example.androidsample21_011.MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
2.「Ctrl+Shift+F」を押し、コードをフォーマッティング。3.「Ctrl+S」を押し、ファイルを保存。
【手順3】
1.「res/layout/activity_main.xml」は以下の通り。
<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/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClickButtonStart"
android:text="開始" />
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClickButtonStop"
android:text="停止" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
2.「Ctrl+Shift+F」を押し、コードをフォーマッティング。3.「Ctrl+S」を押し、ファイルを保存。
【手順4】
1.「src/com/example/androidsample21_011/MainActivity.java」は以下の通り。
package com.example.androidsample21_011;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import jp.co.sharp.android.hardware.Pedometer;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private Pedometer pedometer;
private boolean pedometerStartFlag;
private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
String[] keys = { Pedometer.STEPS };
Bundle data = pedometer.getParameters(keys);
TextView text = (TextView) findViewById(R.id.text);
int steps = data.getInt(Pedometer.STEPS);
if (Pedometer.ACTION_MEASURE_HOURLY.equals(action)) {
text.setText("HOURLY " + getTime() + " " + steps + "歩\n" + text.getText());
} else if (Pedometer.ACTION_MEASURE_CLEAR.equals(action)) {
text.setText("CLEAR " + getTime() + " " + steps + "歩\n" + text.getText());
}
}
};
private String getTime() {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");
return sdf.format(Calendar.getInstance().getTime());
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pedometer = Pedometer.createInstance(this);
pedometerStartFlag = false;
setButtonState();
}
private void setButtonState() {
Button startButton = (Button) findViewById(R.id.start);
Button stopButton = (Button) findViewById(R.id.stop);
if (pedometerStartFlag) {
startButton.setEnabled(false);
stopButton.setEnabled(true);
} else {
startButton.setEnabled(true);
stopButton.setEnabled(false);
}
}
public void onClickButtonStart(View view) {
IntentFilter filter = new IntentFilter();
filter.addAction(Pedometer.ACTION_MEASURE_HOURLY);
filter.addAction(Pedometer.ACTION_MEASURE_CLEAR);
registerReceiver(receiver, filter);
pedometer.startBroadcastIntent();
pedometerStartFlag = true;
setButtonState();
}
public void onClickButtonStop(View view) {
pedometer.stopBroadcastIntent();
unregisterReceiver(receiver);
pedometerStartFlag = false;
setButtonState();
}
}
2.「Ctrl+Shift+F」を押し、コードをフォーマッティング。3.「Ctrl+S」を押し、ファイルを保存。
【手順5】
1.「Androidプロジェクトの作成・実行方法(バージョン別一覧)」のAndroidプロジェクトの実行手順で、実行。
【手順6】
1.以下の様に表示されれば成功です。
以上です。

0 件のコメント:
コメントを投稿
注: コメントを投稿できるのは、このブログのメンバーだけです。