package com.my.android.test;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
public class MainService extends Service {
private Timer timer;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
super.onCreate();
Log.d("TAG", "onCreate");
}
@Override
//onStartは2.0以降は非推奨でonStartCommandを使うことが推奨されている
//public void onStart(Intent intent, int startId) {
public int onStartCommand(Intent intent, int flags, int startId) {
//現在時刻の取得
long currentTimeMillis = System.currentTimeMillis();
Date date = new Date(currentTimeMillis + 10000);//現在時刻+10秒
// タイマの設定
timer = new Timer(true);
final Handler handler = new Handler();
//service = Executors.newSingleThreadScheduledExecutor();
timer.scheduleAtFixedRate( new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
public void run() {
Log.d("TAG", "onStartCommand");
}
});
}
}, date, 3000);//スタート時間、3秒間隔
//super.onStart(intent, startId);
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
if(timer != null){
timer.cancel();
}
Log.d("TAG", "onDestroy");
}
}
2011年12月24日土曜日
端末起動時にサービスを実行:タイマーで指定時間から繰り返し
端末起動時にサービスを実行:タイマーで繰り返し の記事の一部を修正
Executorsクラスで繰り返し
端末起動時にサービスを実行:タイマーで繰り返しの記事の一部を修正
package com.my.android.test;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
public class MainService extends Service {
private Timer timer;
private ScheduledExecutorService service;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
super.onCreate();
Log.d("TAG", "onCreate");
}
@Override
//onStartは2.0以降は非推奨でonStartCommandを使うことが推奨されている
//public void onStart(Intent intent, int startId) {
public int onStartCommand(Intent intent, int flags, int startId) {
final Handler handler = new Handler();
service = Executors.newSingleThreadScheduledExecutor();
service.scheduleAtFixedRate( new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
public void run() {
Log.d("TAG", "onStartCommand");
}
});
}
}, 1000, 3000, TimeUnit.MILLISECONDS);//遅延、3秒間隔、単位時間
//super.onStart(intent, startId);
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
if(timer != null){
timer.cancel();
}
Log.d("TAG", "onDestroy");
}
}
2011年12月22日木曜日
端末起動時にサービスを実行:タイマーで繰り返し
package com.my.android.test;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MainReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.v("タグ", "onReceive");
// サービスを起動する(BOOT_COMPLETED時)
context.startService(new Intent(context, MainService.class));
}
}
package com.my.android.test;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
public class MainService extends Service {
private Timer timer;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
super.onCreate();
Log.d("TAG", "onCreate");
}
@Override
//onStartは2.0以降は非推奨でonStartCommandを使うことが推奨されている
//public void onStart(Intent intent, int startId) {
public int onStartCommand(Intent intent, int flags, int startId) {
// タイマの設定
timer = new Timer(true);
final Handler handler = new Handler();
timer.schedule(new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
public void run() {
Log.d("TAG", "onStartCommand");
}
});
}
}, 1000, 3000);//3秒間隔
//super.onStart(intent, startId);
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
if(timer != null){
timer.cancel();
}
Log.d("TAG", "onDestroy");
}
}
package com.my.android.test;
import android.app.Activity;
import android.os.Bundle;
public class MyAndroidTestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.my.android.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="14" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".MyAndroidTestActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="MainReceiver" >
<intent-filter >
<!-- デバイスブート時のインテントを受け取るレシーバ -->
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name="MainService" >
</service>
</application>
</manifest>
2011年12月21日水曜日
指定時間に実行
package com.android.test;
import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
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 interval = 3 * 1000;
// 初回開始時間
Calendar calendar = Calendar.getInstance(); // Calendar取得
calendar.setTimeInMillis(System.currentTimeMillis()); // 現在時刻を取得
calendar.add(Calendar.SECOND, 10); // 現時刻より10秒後を設定
alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), 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>
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>
AlarmManagerを使って定期処理
このプログラムは動かない。
アンドロイドプロジェクトを作成して、
修正を加えないでインストールした後、
修正を加えてインストールしないと動かない。
アンドロイドプロジェクトを作成して、
修正を加えないでインストールした後、
修正を加えてインストールしないと動かない。
package com.android.test;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.SystemClock;
import android.util.Log;
public class AndroidReceiverTestActivity extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO 自動生成されたメソッド・スタブ
Log.d("タグ", "onReceive");
//レシーバーセット
Intent my_intent = new Intent(context, MyReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 0,my_intent, 0);
// アラームマネージャの用意(初回は5秒後,そのあとは3秒毎に実行
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
// 初回開始時間
long firstTime = SystemClock.elapsedRealtime();
firstTime += 5 * 1000;
// 実行間隔設定
long interval = 3 * 1000;
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, interval, 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 context, Intent intent) {
// TODO 自動生成されたメソッド・スタブ
Log.d("タグ", "MyReceiver:onReceive");
}
}
<?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" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name">
<receiver android:name="AndroidReceiverTestActivity">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>
<receiver android:name="MyReceiver"></receiver>
</application>
</manifest>
登録:
投稿 (Atom)