【Android】开机自启动

【Android】开机自启动

  1007人阅读  评论(6)  收藏  举报

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

1.定义广播类

2.Manifest.xml中注册广播类

3.添加权限

 

 

下面就是具体操作了。

 

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

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

重写一些必要的Java函数

[html]  view plain copy
  1. package cn.etzmico;  
  2.   
  3. import android.content.BroadcastReceiver;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6. import android.util.Log;  
  7.   
  8. public class BootReceiver extends BroadcastReceiver {  
  9.     public void onReceive(Context context, Intent intent) {  
  10.   
  11.         if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {  
  12.             Log.d("BootReceiver", "system boot completed");  
  13.   
  14.             // context, AutoRun.class  
  15.             Intent newIntent = new Intent(context, AutoRun.class);  
  16.   
  17.             /* MyActivity action defined in AndroidManifest.xml */  
  18.             newIntent.setAction("android.intent.action.MAIN");  
  19.   
  20.             /* MyActivity category defined in AndroidManifest.xml */  
  21.             newIntent.addCategory("android.intent.category.LAUNCHER");  
  22.   
  23.             /*  
  24.              * If activity is not launched in Activity environment, this flag is  
  25.              * mandatory to set  
  26.              */  
  27.             newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  28.   
  29.             /* if you want to start a service, follow below method */  
  30.             context.startActivity(newIntent);  
  31.   
  32.         }  
  33.     }  
  34. }  

 

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

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

[html]  view plain copy
  1. <receiver android:name=".BootReceiver" android:label="@string/app_name">  
  2.     <intent-filter>  
  3.         <action android:name="android.intent.action.BOOT_COMPLETED" />  
  4.         <category android:name="android.intent.category.LAUNCHER" />  
  5.     </intent-filter>  
  6. </receiver>  


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

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


 

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

 

 

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

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

有,则成功。

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

 

Demo资源1:http://download.csdn.net/detail/etzmico/3685322

Demo资源2:http://download.csdn.net/detail/etzmico/3685327


     转自:http://blog.csdn.net/etzmico/article/details/6848465

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值