灯火互联
管理员
管理员
  • 注册日期2011-07-27
  • 发帖数41778
  • QQ
  • 火币41290枚
  • 粉丝1086
  • 关注100
  • 终身成就奖
  • 最爱沙发
  • 忠实会员
  • 灌水天才奖
  • 贴图大师奖
  • 原创先锋奖
  • 特殊贡献奖
  • 宣传大使奖
  • 优秀斑竹奖
  • 社区明星
阅读:2501回复:0

Android 开机自启动

楼主#
更多 发布于:2012-09-06 13:44

要想在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就可以了。
有,则成功。
  




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


喜欢0 评分0
游客

返回顶部