Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 高級開發 >> Android 開機自啟動service

Android 開機自啟動service

編輯:高級開發

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);

  }

  };

  }

  復制代碼showActivity 類:(次類中啥都沒有,就是演示一下activity可以被啟動)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);

  }

  }

  復制代碼下面是很重要的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>

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved