Android中定时关机的代码实现

最近客户要求实现平板的定时关机功能。经过在网上查找,结合自己的研究,总算实现了。下面把源码贡献上来。

首先,建立一个工程,名为ShutDown,由于定时开关机需要在关屏的时候计时,达到一定时间之后关机,所以需要一个后台服务,在后台监听屏幕的状态,如果屏幕关掉,则开启计时关机的方法,如果屏幕开启,则取消掉定时关机的方法。而为了实现实时监控,我们需要收听开机广播,当开机时,启动后台服务,这样在监听屏幕状态的情况下,才能实现定时关机。下面的实现步骤。

1.实现接收开机启动广播,在广播中开启服务:

public class BootCompleteReciver extends BroadcastReceiver{
	@Override
	public void onReceive(Context context, Intent intent) {
		if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
			Intent intent2 = new Intent() ;
			intent2.setClass(context, StatusService.class) ;
			context.startService(intent2) ;
			Log.i("TAG", "start service......") ;
		}
	}
}

2.实现后台服务:

package com.cartion.shutdown;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;

public class StatusService extends Service{
	Timer timer = new Timer() ;
	@Override
	public void onCreate() {
		super.onCreate();
		IntentFilter filter = new IntentFilter() ;
		filter.addAction(Intent.ACTION_SCREEN_OFF) ;
		filter.addAction(Intent.ACTION_SCREEN_ON) ;
		registerReceiver(screenOffReceiver, filter) ;
			
	}
	
	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		unregisterReceiver(screenOffReceiver) ;
	}
	
	BroadcastReceiver screenOffReceiver = new BroadcastReceiver() {
		int what = 2 ;
		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
				Log.i("TAG", "Receiver screen off broadcast......") ;
				shutDownAlarm(1 * 60 * 1000) ;
			} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
				Log.i("TAG", "Receiver screen on......") ;

				if (am != null && pendingIntent != null) {
					am.cancel(pendingIntent) ;
				}
			}
			
		}
	};
	
	public int onStartCommand(Intent intent, int flags, int startId) {
		
		return START_STICKY ;
};

	
	AlarmManager am ;
	PendingIntent pendingIntent ;
	public void shutDownAlarm(int time){
	    	am = (AlarmManager) this
                .getSystemService(Context.ALARM_SERVICE);
	    Intent intent = new Intent("com.cartion.abc") ;
	    pendingIntent = PendingIntent.getBroadcast(this, 0,
                intent, PendingIntent.FLAG_CANCEL_CURRENT);
        am = (AlarmManager) this
                .getSystemService(Context.ALARM_SERVICE);
        am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + time, pendingIntent);
        
	}
	public void cancel(PendingIntent pendingIntent){
		if (am != null) {
			am.cancel(pendingIntent) ;			
		}
	}
}
3.实现关机广播


public class DeReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("TAG", ": " + intent.getAction() ) ;
        if (intent.getAction().equals("com.cartion.abc")) {
            shutDown(context) ;
        }
    }
    private void shutDown(Context context) {
        Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);

        intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);

        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        context.startActivity(intent);
    }
}


4.Android的manifest文件




    

    
    
    
        
            
                
            
        
        
        
        
            
                
            
        
    



5.Android.mk文件,由于需要放到源码中编译,所以需要Android.mk文件

        LOCAL_PATH:= $(call my-dir)

        include $(CLEAR_VARS)


        LOCAL_MODULE_TAGS := optional


        LOCAL_SRC_FILES := $(call all-java-files-under, src)


        LOCAL_PACKAGE_NAME := ShutDown       //注意,这里配置的是你自己的工程名字

        LOCAL_CERTIFICATE := platform


        include $(BUILD_PACKAGE) 

源码下载地址:http://download.csdn.net/detail/h452478045/8077237

你可能感兴趣的:(android学习笔记)