カテゴリー
SugiBlog Webエンジニアのためのお役立ちTips

端末の起動時に処理を実行する [Android]

この記事は最終更新日から1年以上経過しています。

Androidでは、端末の起動時に発行されるandroid.intent.action.BOOT_COMPLETEDというブロードキャストインテントを受け取り、処理を実行させることができます。

これを利用し、バックグラウンドで常駐するアプリを作成することが可能です。
ここでは例として位置情報を取得し、位置情報が更新されたらトーストでメッセージを表示するサービスをバックグラウンドで常駐させるように作成しています。

AndroidManifest.xmlapplicationタグ内に以下を記述

1<service android:name="ExampleService"></service>
2<receiver
3    android:name=".BootReceiver"
4    android:enabled="true">
5 
6    <intent-filter>
7        <action android:name="android.intent.action.BOOT_COMPLETED" />
8        <category android:name="android.intent.category.DEFAULT" />
9    </intent-filter>
10</receiver>

起動時にブロードキャストインテントを受け取れるようにパーミッションの設定を記述します。

1<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

ブロードキャストインテントを受け取るブロードキャストレシーバーを作成します。
onReceiveにて、常駐させたいサービスを起動させます。
BootReceiver.java

1import android.content.BroadcastReceiver;
2import android.content.Context;
3import android.content.Intent;
4import android.widget.Toast;
5 
6public class BootReceiver extends BroadcastReceiver {
7 
8    @Override
9    public void onReceive(Context context, Intent intent) {
10        String action = intent.getAction();
11        if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
12            Toast.makeText(context, "BOOT_COMPLETED", Toast.LENGTH_LONG).show();
13            context.startService(new Intent(context, ExampleService.class));
14        }
15    }
16 
17}

実際に常駐するサービスを作成します。
ExampleService.java

1import android.app.Service;
2import android.content.Intent;
3import android.location.Location;
4import android.location.LocationListener;
5import android.location.LocationManager;
6import android.os.Bundle;
7import android.os.IBinder;
8import android.util.Log;
9import android.widget.Toast;
10 
11public class ExampleService extends Service implements LocationListener {
12 
13    static final String TAG = "ExampleService";
14    private LocationManager mLocationManager;
15 
16    @Override
17    public void onCreate() {
18        Toast.makeText(this, "バックグラウンドサービスを開始しました。", Toast.LENGTH_SHORT).show();
19        mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
20    }
21 
22    @Override
23    public int onStartCommand(Intent intent, int flags, int startId) {
24        Log.i(TAG, "onStartCommand Received start id " + startId + ": " + intent);
25        mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, this);
26        //明示的にサービスの起動、停止が決められる場合の返り値
27        return START_STICKY;
28    }
29  
30    @Override
31    public void onDestroy() {
32        Log.i(TAG, "onDestroy");
33          mLocationManager.removeUpdates(this);
34    }
35 
36    @Override
37    public IBinder onBind(Intent intent) {
38        return null;
39    }
40 
41    @Override
42    public void onLocationChanged(Location location) {
43        Toast.makeText(this, "位置情報を更新", Toast.LENGTH_LONG).show();
44    }
45 
46    @Override
47    public void onProviderDisabled(String provider) {
48         
49    }
50 
51    @Override
52    public void onProviderEnabled(String provider) {
53    }
54 
55    @Override
56    public void onStatusChanged(String provider, int status, Bundle extras) {
57    }
58 
59}

作成したアプリをインストールし、一度再起動した端末にて、
[設定]-[アプリケーション]-[実行中のサービス]で作成したアプリのところを確認します。
「1件のプロセスと1件のサービス」と表示されていれば成功です。
位置情報が更新された時に、正常にメッセージが表示されるか確認しましょう。

この記事がお役に立ちましたらシェアお願いします
10,087 views

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です