DBMNG数据库管理与应用

独立思考能力,对于从事科学研究或其他任何工作,都是十分必要的。
当前位置:首页 > 移动应用 > Android

Android开机自动启动服务

 当Android启动时,会发出一个系统广播,内容为ACTION_BOOT_COMPLETED, 只要在程序中“捕捉”到这个消息,再启动之即可, 所以我们在自己应用里面添加 一个广播接收就可以达到开机启动的目的.

  在 AndroidManifest.xml 里面注册服务,还有 广播接收,于开机启动!!!!!

 


1.  BootBroadcastReceiver.java  用于接收到广播后的处理,通过Intent启动服务或者其activity 等操作


package com.tianfeng.bootstartdemo;




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


public class BootBroadcastReceiver extends BroadcastReceiver{
private static final String ACTION = "android.intent.action.BOOT_COMPLETED";
  private final static String TAG = "BootBroadcastReceiver";


@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub

if(intent.getAction().equals(ACTION))
{
        Log.e(TAG, "try to start demo servers");
Intent mDemoService=new Intent();//启动服务
mDemoService.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mDemoService.setClass(context, StartBootDemoServer.class);
context.startService(mDemoService);  



}


}


2.  StartBootDemoServer.java 


package com.tianfeng.bootstartdemo;


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


public class StartBootDemoServer extends Service{

private final static String TAG = "StartBootDemoServer";


@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}

@Override
public void onStart(Intent intent, int startId){
while(true){
Log.i(TAG, "==============tianfeng I am demo Run===============");
SystemClock.sleep(1000);
}
}


}


3. AndroidManifest.xml


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tianfeng.bootstartdemo"
    android:versionCode="1"
    android:versionName="1.0" >


    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />


    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".StartDemo"
            android:label="@string/title_activity_start_demo" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />


                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
<!-- 在这里注册服务 用于启动-->
        <service android:name=".StartBootDemoServer" />
<!--在这里注册广播接收,用于接收系统开机广播-->
        <receiver android:name=".BootBroadcastReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED">
    </action>
    </intent-filter>
</receiver>
    </application>


</manifest>


4. 开机后通过logcat 可以看见没1秒输出: ==============tianfeng I am demo Run===============


本站文章内容,部分来自于互联网,若侵犯了您的权益,请致邮件chuanghui423#sohu.com(请将#换为@)联系,我们会尽快核实后删除。
Copyright © 2006-2023 DBMNG.COM All Rights Reserved. Powered by DEVSOARTECH            豫ICP备11002312号-2

豫公网安备 41010502002439号