Android 开机自动启动程序详解

转载 http://blog.csdn.net/xwnxwn/article/details/43865343

略有改动的转载

首先是从BroadcastReceiver派生出一个新类,用来监听系统启动后发出的广播消息Android.intent.action.BOOT_COMPLETED

BootReceiver.java:

复制代码
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 " ); 
              Intent newIntent 
=   new  Intent(context, FirstRun. class );
         newIntent.setAction(
" android.intent.action.MAIN " );  // MyActivity action defined in AndroidManifest.xml

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

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

         context.startActivity(newIntent);

         
// if you want to start a service, follow below method:


         
/** *****************************************************

            Intent service = new Intent(yourService.ACTION_START);
            service.setClass(context, yourService.class);
            context.startService(service);

          //我改成:
          //Intent i = new Intent(AutoRun.class.getName());
          // i.setClass(context, AutoRun.class);
          //  context.startService(i);
         *****************************************************
*/
         }
     }
}
复制代码

 

接下来这个类就是监听到系统启动完毕后,我们要运行的activity.
FirstRun.java

 

复制代码
import  android.app.Activity;
import  android.os.Bundle;

public   class  FirstRun  extends  Activity {
    
    
public   void  onCreate(Bundle savedInstanceState) {
        
super .onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}
复制代码

 

当然,我们还要改配置文件,需要注意的是,在manifest.xml中需要加上
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>

Manifest.xml

复制代码
<? xml version="1.0" encoding="utf-8" ?>
< manifest  xmlns:android ="http://schemas.android.com/apk/res/android"
      package
="com.service.prac"
      android:versionCode
="1"
      android:versionName
="1.0" >
      
    
< application  android:icon ="@drawable/icon"  android:label ="@string/app_name" >
        
< 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 >
        
< activity  android:name =".FirstRun" >
            
< intent-filter >
                
< action  android:name ="android.intent.action.MAIN"   />
                
< category  android:name ="android.intent.category.LAUNCHER"   />
            
</ intent-filter >
        
</ activity >
    
</ application >
    
< uses-sdk  android:minSdkVersion ="3"   />
    
< uses-permission  android:name ="android.permission.RECEIVE_BOOT_COMPLETED" ></ uses-permission >
</ manifest >
复制代码

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值