zoukankan      html  css  js  c++  java
  • Android初级教程启动定时器详解

    本案例知识是:后台执行定时任务。

    Alarm机制:

    一、创建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;
    
    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() {
    				// 打印日志模拟耗时操作。
    				System.out.println("服务启动时间:" + new Date().toString());
    
    			}
    		}).start();
    
    		AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);
    		int times = 1000 * 60;// 设置相隔多久启动一次广播,我设置为1分钟启动一次服务,去执行定时任务(虽然我写的是打印一条日志,看起来很无趣)
    		long triggerAtime = SystemClock.elapsedRealtime() + times;// 设置触发时间点
    		Intent i = new Intent(this, AlarmReceiver.class);// 服务启动广播的intent意图
    		PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, i, 0);// 封装pendingIntent,启动广播接收者意图
    		manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtime,
    				pendingIntent);// 设置精确定时时间,定时到了触发,广播启动。
    		return super.onStartCommand(intent, flags, startId);
    	}
    
    }
    

    二、创建要接收上述要启动的广播。

    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 intent2 = new Intent(context, LongRunningService.class);
    		context.startService(intent2);//启动广播做启动服务操作,服务又一次启动。
    		//由于服务不再前台,因此不需要设置addFlags();方法。因为服务不再借助任务栈去创建了。
    	}
    
    }
    

    三、我们要有一个启动服务的入口,那就选择在主活动作为入口:

    package com.example.servicebestpractice;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Intent;
    import android.view.Menu;
    
    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            //主活动要首先有一次启动服务的操作
            Intent intent3 = new Intent(this, LongRunningService.class);
            startService(intent3);//启动服务
        }
        
    }
    

    四、广播、活动、服务三大组件记得去清单文件配置一下:

    <activity
                android:name="com.example.servicebestpractice.MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    
            <service android:name="com.example.servicebestpractice.LongRunningService" >
            </service>
    
            <receiver android:name="com.example.servicebestpractice.AlarmReceiver" >
            </receiver>

    写完这篇博客后,看了一下我的log后台输出如下截图:

    每隔一分钟,定时任务完成,启动一次服务。



  • 相关阅读:
    git push 小结
    在GitHub上创建代码仓库
    本人在CSDN上的技术博客访问量突破了10万次,特此截图留念
    和菜鸟一起学linux总线驱动之i2c死锁问题
    《万能数据库查询分析器》实现使用SQL语句直接高效地访问文本文件
    Access text files using SQL statements by DB Query Analyzer
    git分享:Git_MinaPro
    git分享:Git_DataPro
    vimgrep 搜索总结
    HTML+CSS+JavaScript网页制作从新手到高手
  • 原文地址:https://www.cnblogs.com/wanghang/p/6299671.html
Copyright © 2011-2022 走看看