2011年12月21日水曜日

AlarmManagerを使って定期処理2


package com.android.test;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;

public class AndroidTestActivity extends Activity {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
 }

 public void onClickStart(View view) {
  Log.d("タグ", "スタート");
  Intent intent = new Intent(this, MyReceiver.class);
  PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, 0);

  // アラームマネージャの用意(初回は5秒後,そのあとは3秒毎に実行
    //AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

    // 初回開始時間
    long firstTime = SystemClock.elapsedRealtime();
    firstTime += 5 * 1000;

    // 実行間隔設定
    long interval = 3 * 1000;

    alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, interval, sender);
 }
 public void onClickStop(View view) {
  Log.d("タグ", "ストップ");
  Intent intent = new Intent(this, MyReceiver.class);
  PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, 0);

  AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
  am.cancel(sender);
 }
}


package com.android.test;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class MyReceiver extends BroadcastReceiver {

 @Override
 public void onReceive(Context arg0, Intent arg1) {
  // TODO 自動生成されたメソッド・スタブ
  Log.d("タグ", "MyReceiver:onReceive");
 }

}

<?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" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start" android:onClick="onClickStart"/>

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Stop" android:onClick="onClickStop"/>

</LinearLayout>


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.test"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="14" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".AndroidTestActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name="MyReceiver"></receiver>
    </application>

</manifest>

関連記事

0 件のコメント:

コメントを投稿