• 服务的最佳实践——后台执行的定时任务


    创建一个ServiceBestPractice项目,然后新增一个LongRunningService类,代码如下所示:

    package com.example.servicebestpractice;
    
    
    import java.util.Date;
    
    import android.app.AlarmManager;
    import android.app.PendingIntent;
    import android.app.Service;
    import android.content.Intent;
    import android.os.IBinder;
    import android.os.SystemClock;
    import android.util.Log;
    
    public class LongRunningService extends Service{
    
        @Override
        public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
            return null;
        }
        
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    Log.d("LongRunningService", "executed at " + new Date().toString());
                }
            }).start();
            AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);
            int anHour = 60 * 60 * 1000;  //这是一小时的毫秒数
            long triggerAtTime = SystemClock.elapsedRealtime() + anHour;
            Intent i = new Intent(this, AlarmReceiver.class);
            PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
            manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime, pi);
            return super.onStartCommand(intent, flags, startId);
        }
    
    }

    新建一个AlarmReceiver类,并让它继承自BroadcastReceiver,代码如下所示:

    package com.example.servicebestpractice;
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    
    public class AlarmReceiver extends BroadcastReceiver {
    
        @Override
        public void onReceive(Context context, Intent intent) {
            Intent i = new Intent(context, LongRunningService.class);
            context.startService(i);
        }
    
    }

    接下来我们需要在打开程序的时候启动一次LongRunningService,修改mainActivity中的代码,如下所示:

    package com.example.servicebestpractice;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    
    
    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Intent i = new Intent(this, LongRunningService.class);
            startService(i);
        }
    
    }

    最后别忘了,我们用到的广播接收器和服务都要在AndroidManifest.xml中注册才行,代码如下所示:

            <service android:name=".LongRunningService">
            </service>
            <receiver android:name=".AlarmReceiver">
            </receiver>
  • 相关阅读:
    [bzoj4893]项链分赃
    [Spoj]Counting Divisors (cube)
    [Noi2016]国王饮水记
    [Noi2016]网格
    [Noi2016]优秀的拆分
    [Noi2016]区间
    [Noi2015]寿司晚宴
    Codeforces Round #411 (Div. 2)
    VK-Cup2017 Wild Card Round 2
    [Noi2015]小园丁和老司机
  • 原文地址:https://www.cnblogs.com/happyhacking/p/4220154.html
Copyright © 2020-2023  润新知