android 开机自启动程序2

来源:互联网 发布:知乎的海贼 王路飞 编辑:程序博客网 时间:2024/06/05 19:35

如果您在开发一个需要实时更新数据的应用程序,当有新的数据的时候提醒用户查看新的数据,那么您需要在后台开起一个Service,然后实时的去网络上获取数据,但是如果用户关机重启,您的Service可能就消失了!那么怎么样保证开机后你的Service还活跃的在用户的手机里偷偷的从网络上获取数据呢?

很简单,我们只要实现开机自启动即可,Android实现开机自启动可能是移动操作系统中最简单的了,我们只需要监听一个开机启动的Broadcast(广播)即可。首先写一个Receiver(即广播监听器),继承BroadcastReceiver,如下所示:

android开机启动service,适合闹钟程序 实例中一共三个类

1.public class YourReceiver extends BroadcastReceiver

2.public class ServiceTest extends Service

3.public class ShowActivity extends Activity

 

 

1.yourReceiver

 

 

[java] view plaincopyprint?
  1. package radar.com;   
  2.   
  3.    
  4.   
  5. import android.content.BroadcastReceiver;   
  6.   
  7. import android.content.Context;   
  8.   
  9. import android.content.Intent;   
  10.   
  11.    
  12.   
  13. public class YourReceiver extends BroadcastReceiver {   
  14.   
  15.    
  16.   
  17.        @Override   
  18.   
  19.        public void onReceive(Context context, Intent intent) {   
  20.   
  21.             Intent i = new Intent(context, ServiceTest.class);   
  22.   
  23.                 i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);   
  24.   
  25.        context.startService(i);   
  26.   
  27.        }   
  28.   
  29.          
  30.   
  31. }   

  

 

2.复制代码ServiceTest

 

 

 

[java] view plaincopyprint?
  1. package radar.com;   
  2.   
  3. import java.util.Calendar;   
  4.   
  5. import android.app.Service;   
  6.   
  7. import android.content.Intent;   
  8.   
  9. import android.os.Handler;   
  10.   
  11. import android.os.IBinder;   
  12.   
  13. import android.util.Log;   
  14.   
  15.    
  16.   
  17. public class ServiceTest extends Service{   
  18.   
  19.        Handler hd1=new Handler();   
  20.   
  21.        /**启动activity的开关*/   
  22.   
  23.        boolean b;   
  24.   
  25.        /**启动一次activity之后的一分钟内将不再重新启动*/   
  26.   
  27.        int time;   
  28.   
  29.        public static final Intent ACTION_START = null;   
  30.   
  31.        private static final String TAG = "TestService";   
  32.   
  33.        @Override   
  34.   
  35.        public IBinder onBind(Intent intent) {   
  36.   
  37.             return null;   
  38.   
  39.        }   
  40.   
  41.        @Override   
  42.   
  43.        public boolean onUnbind(Intent i) {   
  44.   
  45.             Log.e(TAG, "============> TestService.onUnbind");   
  46.   
  47.             return false;   
  48.   
  49.        }   
  50.   
  51.    
  52.   
  53.        @Override   
  54.   
  55.        public void onRebind(Intent i) {   
  56.   
  57.             Log.e(TAG, "============> TestService.onRebind");   
  58.   
  59.        }   
  60.   
  61.    
  62.   
  63.        @Override   
  64.   
  65.        public void onCreate() {   
  66.   
  67.               
  68.   
  69.             Log.e(TAG, "============> TestService.onCreate");   
  70.   
  71.                 hd1.postDelayed(mTasks, delay);   
  72.   
  73.        }   
  74.   
  75.    
  76.   
  77.        @Override   
  78.   
  79.        public void onStart(Intent intent, int startId) {   
  80.   
  81.             Log.e(TAG, "============> TestService.onStart");   
  82.   
  83.        }   
  84.   
  85.    
  86.   
  87.        @Override   
  88.   
  89.        public void onDestroy() {   
  90.   
  91.             Log.e(TAG, "============> TestService.onDestroy");   
  92.   
  93.        }   
  94.   
  95.        public void log(){   
  96.   
  97.             Calendar c= Calendar.getInstance();   
  98.   
  99.             int h=c.getTime().getHours();   
  100.   
  101.             int m=c.getTime().getMinutes();   
  102.   
  103.             Log.i("hour", ""+h);   
  104.   
  105.             Log.i("minute", ""+m);   
  106.   
  107.             /**这里的16和10可以自己定义一下   主要是提醒的时间设置,我不想做的太繁琐,所有没有些闹钟,只是用这个测试一下:)*/   
  108.   
  109.             if(h==16&&m==10)   
  110.   
  111.             {   
  112.   
  113.                      /**为防止持续调用,所以用boolean 变量b做了一个小开关*/   
  114.   
  115.                      if(!b){   
  116.   
  117.                                Intent i = new Intent();   
  118.   
  119.                                i.setClass(ServiceTest.this, ShowActivity.class);   
  120.   
  121.                                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);   
  122.   
  123.                                this.startActivity(i);   
  124.   
  125.                                this.stopSelf();   
  126.   
  127.                                b=true;   
  128.   
  129.                      }   
  130.   
  131.             }   
  132.   
  133.             /**开关开启后计时60秒,在这60秒之内就不再重新启动activity了,而60秒一过,上面的h和m条件肯定就不成立了*/   
  134.   
  135.             if(b){   
  136.   
  137.                      time+=5;   
  138.   
  139.                      if(time==60){   
  140.   
  141.                                time=0;   
  142.   
  143.                                b=false;   
  144.   
  145.                      }   
  146.   
  147.             }   
  148.   
  149.        }   
  150.   
  151.        /** 速度控制参数(单位豪秒)   */   
  152.   
  153.        private int delay = 5000;   
  154.   
  155.        /**  
  156.  
  157.       * 控制速度  
  158.  
  159.       * */   
  160.   
  161.        private Runnable mTasks = new Runnable()     
  162.   
  163.       {   
  164.   
  165.          public void run()   
  166.   
  167.          {   
  168.   
  169.                    log();   
  170.   
  171.                      
  172.   
  173.                      hd1.postDelayed(mTasks, delay);   
  174.   
  175.          }   
  176.   
  177.       };   
  178.   
  179. }   

 

 

 

 

3复制代码showActivity:(.类中啥都没有就是演示一下activity可以被启动

 

 

[java] view plaincopyprint?
  1. package radar.com;   
  2.   
  3. import radar.com.R;   
  4.   
  5. import android.app.Activity;   
  6.   
  7. import android.os.Bundle;   
  8.   
  9. import android.view.Window;   
  10.   
  11. import android.view.WindowManager;   
  12.   
  13.    
  14.   
  15. public class ShowActivity extends Activity{   
  16.   
  17.        @Override   
  18.   
  19. public void onCreate(Bundle savedInstanceState) {   
  20.   
  21.        super.onCreate(savedInstanceState);   
  22.   
  23.        this.requestWindowFeature(Window.FEATURE_NO_TITLE);   
  24.   
  25.        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,   
  26.   
  27.                                WindowManager.LayoutParams.FLAG_FULLSCREEN);   
  28.   
  29.        setContentView(R.layout.main);   
  30.   
  31. }   
  32.   
  33. }   
  34.   
  35.    

 

 

 

4.复制代码下面是很重要的AndroidManifest

 

 

[xhtml] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>   
  2.   
  3. <manifest xmlns:android="http://schemas.android.com/apk/res/android"   
  4.   
  5.    package="radar.com"   
  6.   
  7.    android:versionCode="1"   
  8.   
  9.    android:versionName="1.0">   
  10.   
  11. <application   
  12.   
  13.          android:icon="@drawable/icon"   
  14.   
  15.          android:label="@string/app_name">   
  16.   
  17.          <service   
  18.   
  19.          android:name=".ServiceTest"   
  20.   
  21.          android:label="@string/app_name">   
  22.   
  23.          <intent-filter>   
  24.   
  25.             <action android:name="android.intent.action.MAIN" />   
  26.   
  27.             <category android:name="android.intent.category.LAUNCHER" />   
  28.   
  29.          </intent-filter>   
  30.   
  31.          </service>   
  32.   
  33.             
  34.   
  35.                      <receiver android:name=".yourReceiver" >   
  36.   
  37.                         <intent-filter>   
  38.   
  39.                         <action android:name="android.intent.action.BOOT_COMPLETED" />   
  40.   
  41.                         <category android:name="android.intent.category.HOME"/>   
  42.   
  43.                         </intent-filter>   
  44.   
  45.       </receiver>   
  46.   
  47.       <activity   android:name=".showActivity"   
  48.   
  49.                                   android:label="@string/app_name"   
  50.   
  51.                android:configChanges="orientation|keyboardHidden|navigation"   
  52.   
  53.                            android:screenOrientation="portrait">   
  54.   
  55.                            <intent-filter>   
  56.   
  57.             <action android:name="android.intent.action.MAIN" />   
  58.   
  59.             <category android:name="android.intent.category.LAUNCHER" />   
  60.   
  61.          </intent-filter>   
  62.   
  63.          </activity>   
  64.   
  65. </application>   
  66.   
  67. <uses-sdk android:minSdkVersion="4" />   
  68.   
  69. </manifest>   

 

 

 

 

既可以作为开机启动并隐藏到后台的service也可以当做activity打开

 

原创粉丝点击