当前位置:操作系统 > 安卓/Android >>

Android 开机自启动

要想在Android系统中实现开机启动,很简单,只需要几个步骤就可以了。

1.定义广播类

2.Manifest.xml中注册广播类

3.添加权限

 

 

下面就是具体操作了。

 

首先,我们来定义广播类。

创建一个类BootReceiver,使其继承BroadcastReceiver。

重写一些必要的Java函数

[html] package cn.etzmico; 
 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.util.Log; 
 
public class BootReceiver extends BroadcastReceiver { 
    public void onReceive(Context context, Intent intent) { 
 
        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) { 
            Log.d("BootReceiver", "system boot completed"); 
 
            // context, AutoRun.class 
            Intent newnewIntent = new Intent(context, AutoRun.class); 
 
            /* MyActivity action defined in AndroidManifest.xml */ 
            newIntent.setAction("android.intent.action.MAIN"); 
 
            /* MyActivity category defined in AndroidManifest.xml */ 
            newIntent.addCategory("android.intent.category.LAUNCHER"); 
 
            /* 
             * If activity is not launched in Activity environment, this flag is 
             * mandatory to set 
             */ 
            newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
 
            /* if you want to start a service, follow below method */ 
            context.startActivity(newIntent); 
 
        } 
    } 

package cn.etzmico;

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

public class BootReceiver extends BroadcastReceiver {
 public void onReceive(Context context, Intent intent) {

  if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
   Log.d("BootReceiver", "system boot completed");

   // context, AutoRun.class
   Intent newIntent = new Intent(context, AutoRun.class);

   /* MyActivity action defined in AndroidManifest.xml */
   newIntent.setAction("android.intent.action.MAIN");

   /* MyActivity category defined in AndroidManifest.xml */
   newIntent.addCategory("android.intent.category.LAUNCHER");

   /*
    * If activity is not launched in Activity environment, this flag is
    * mandatory to set
    */
   newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

   /* if you want to start a service, follow below method */
   context.startActivity(newIntent);

  }
 }
}
 

AutoRun.class就是程序运行的Activity。


其次,在Manifest.xml中注册广播类

[html] <receiver android:name=".BootReceiver" android:label="@string/app_name"> 
    <intent-filter> 
        <action android:name="android.intent.action.BOOT_COMPLETED" /> 
        <category android:name="android.intent.category.LAUNCHER" /> 
    </intent-filter> 
</receiver> 
<receiver android:name=".BootReceiver" android:label="@string/app_name">
 <intent-filter>
  <action android:name="android.intent.action.BOOT_COMPLETED" />
  <category android:name="android.intent.category.LAUNCHER" />
 </intent-filter>
</receiver>

最后,再添加上权限就可以了

[html] <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission> 
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>

 

这样,我们就实现了Android系统的开机自启动,切勿忘记Manifest.xml中的操作!

 

 

资源有两个,他们本质上是一样的。程序运行后,把虚拟机或手机之类的移动设备关闭,之后重新启动;当系统程序加载外币后,我们的程序就会运行。

如果想隐藏,那么只需要加上finish();就可以了。我在程序中已经添加了一个System.out,所以finish也不用担心是否成功,只要看LogCat中是否有Successful就可以了。

有,则成功。

  \


 

切记要看清时间是否吻合,以免造成不必要的麻烦!

 

Demo资源1:/2012/0308/20120308012933938.zip
 

Demo资源2:/2012/0308/20120308013159656.zip
 

 

摘自 Etzmico
 
补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,