开机启动service小DEMO

yourReceiver 类:

package radar.com;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class yourReceiver extends BroadcastReceiver { 

        @Override
        public void onReceive(Context context, Intent intent) {
                Intent i = new Intent(context, ServiceTest.class);
                 i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startService(i); 
        }
        
} 

ServiceTest 类:

package radar.com;

import java.util.Calendar;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;

public class ServiceTest extends Service{
        Handler hd1=new Handler();
        /**启动activity的开关*/
        boolean b;
        /**启动一次activity之后的一分钟内将不再重新启动*/
        int time;
        public static final Intent ACTION_START = null;
        private static final String TAG = "TestService";
        @Override
        public IBinder onBind(Intent intent) {
                return null;
        }
        @Override
        public boolean onUnbind(Intent i) {
                Log.e(TAG, "============> TestService.onUnbind");
                return false;   
        }

        @Override
        public void onRebind(Intent i) {
                Log.e(TAG, "============> TestService.onRebind");
        }

        @Override
        public void onCreate() {
                
                Log.e(TAG, "============> TestService.onCreate");
                 hd1.postDelayed(mTasks, delay);
        }

        @Override
        public void onStart(Intent intent, int startId) {
                Log.e(TAG, "============> TestService.onStart");
        }

        @Override
        public void onDestroy() {
                Log.e(TAG, "============> TestService.onDestroy");
        }
        public void log(){
                Calendar c= Calendar.getInstance();
                int h=c.getTime().getHours();
                int m=c.getTime().getMinutes();
                Log.i("hour", ""+h);
                Log.i("minute", ""+m);
                /**这里的16和10可以自己定义一下  主要是提醒的时间设置,我不想做的太繁琐,所有没有些闹钟,只是用这个测试一下:)*/
                if(h==16&&m==10)
                {
                        /**为防止持续调用,所以用boolean 变量b做了一个小开关*/
                        if(!b){
                                Intent i = new Intent();
                                i.setClass(ServiceTest.this, showActivity.class);
                                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                this.startActivity(i);
                                this.stopSelf();
                                b=true;
                        }
                }
                /**开关开启后计时60秒,在这60秒之内就不再重新启动activity了,而60秒一过,上面的h和m条件肯定就不成立了*/
                if(b){
                        time+=5;
                        if(time==60){
                                time=0;
                                b=false;
                        }
                }
        }
        /** 速度控制参数(单位豪秒)  */
        private int delay = 5000;
        /**
         * 控制速度
         * */
        private Runnable mTasks = new Runnable()  
          {
            public void run() 
            {
                    log();
                    
                        hd1.postDelayed(mTasks, delay);
            } 
          };
}

AndroidManifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="radar.com"
      android:versionCode="1"
      android:versionName="1.0">
    <application 
            android:icon="@drawable/icon" 
            android:label="@string/app_name">
            <service 
            android:name=".ServiceTest"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            </service>
           
                        <receiver android:name=".yourReceiver" >
                            <intent-filter>
                            <action android:name="android.intent.action.BOOT_COMPLETED" />
                            <category android:name="android.intent.category.HOME"/>
                           </intent-filter>
          </receiver> 
          <activity  android:name=".showActivity"
                                   android:label="@string/app_name"
                  android:configChanges="orientation|keyboardHidden|navigation"
                              android:screenOrientation="portrait">
                               <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            </activity>
    </application>
    <uses-sdk android:minSdkVersion="4" />
    
</manifest> 

showActivity 类:证明程序启动

package radar.com;

import radar.com.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

public class showActivity extends Activity{
        @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.main);
    }
}
原文地址:https://www.cnblogs.com/weixiao870428/p/3571547.html