ブロードキャストとサービスを介したAndroid開発オープンのスケジュールされた更新タスク

はじめに:

気象情報の更新、投稿の更新、情報の更新などの一部のフロントエンドアプリケーション

この記事では、サービスおよび放送受信機を通してそれを紹介します。3秒ごとに更新されますが、携帯電話の性能上、更新が数秒遅れる場合があります。

 

ステップ1:MainActivity.javaがサービスを開く

パッケージcom.huwan.broadcastwithservicedemo;

 

android.app.Activityをインポートします。

import android.content.Intent;

android.os.Bundleをインポートします。

 

パブリッククラスMainActivityはActivity {

         @オーバーライド

         protected void onCreate(Bundle savedInstanceState){

                   super.onCreate(savedInstanceState);

                   setContentView(R.layout.activity_main);

                   //サービスを開始します

                   Intent intent = new Intent(this、LongRunningService.class);

                   startService(intent);

         }

}

 

ステップ2:LongRunningService.javaサービスコード

パッケージcom.huwan.broadcastwithservicedemo;

 

import java.util.Date;

android.app.AlarmManagerをインポートします。

import android.app.PendingIntent;

android.app.Serviceをインポートします。

import android.content.Intent;

android.os.IBinderをインポートします。

android.os.SystemClockをインポートします。

android.util.Logをインポートします。

 

パブリッククラスLongRunningServiceはService {

         @オーバーライド

         public IBinder onBind(Intent intent){

                   nullを返します。

         }

 

         @オーバーライド

         public int onStartCommand(Intent intent、int flags、int startId){

                   new Thread(new Runnable(){

                            @オーバーライド

                            public void run(){

                                     Log.e( "LongRunningService"、 "executed at" + new Date()。toString());

                            }

                   })。開始();

                   AlarmManager manager =(AlarmManager)getSystemService(ALARM_SERVICE);

                   // int anHour = 60 * 60 * 1000;

                   int anTime = 3 * 1000; // 3秒ごとに記録

                   長いtriggerAtTime = SystemClock.elapsedRealtime()+ anTime;

                   Intent i = new Intent(this、AlarmReceiver.class);

                   PendingIntent pi = PendingIntent.getBroadcast(this、0、i、0);

                   manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP、triggerAtTime、pi);

                   super.onStartCommand(intent、flags、startId);を返します。

         }

 

}

 

ステップ3:AlarmReceiver.javaサービスレシーバー、サービスの受信後にサービスを再度リクエストする

パッケージcom.huwan.broadcastwithservicedemo;

 

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

 

パブリッククラスAlarmReceiverはBroadcastReceiver {

 

         @オーバーライド

         public void onReceive(Context context、Intent intent){

                   Intent i = new Intent(context、LongRunningService.class);

                   context.startService(i);

         }

 

}

メインインターフェースはサービスを開始し、サービスは時間指定されたタスクを送信し、ブロードキャストレシーバーがタスクを受信した後、サービスを再度再起動します。これはデーモンに相当します。

 

デモサンプルのダウンロード:http : //www.huwan.xin/Source/AppPackage/ZIP/Android/BroadcastWithServiceDemo.rar

参考資料:Android開発-データを更新するか、オブザーバーデザインモードでインターフェイスを閉じます

参考資料:Android開発-静的ブロードキャスト(アクティビティに値を渡すためのブロードキャストを含む)、動的ブロードキャスト(順序付き/順序なし)の紹介

参考資料:Android開発-Androidは複数のアクティビティプロセスを終了する/複数のインターフェースを同時に閉じる/以前のインターフェースを閉じる

上記のドキュメントは、定期的に更新した後にインターフェイスを更新する方法を解決できます。

詳細については、www.huwan.xinをご覧ください。

おすすめ

転載: www.cnblogs.com/huwan/p/12758263.html