Chinaunix首页 | 论坛 | 博客
  • 博客访问: 9153863
  • 博文数量: 1727
  • 博客积分: 12961
  • 博客等级: 上将
  • 技术积分: 19860
  • 用 户 组: 普通用户
  • 注册时间: 2009-01-09 11:25
个人简介

偷得浮生半桶水(半日闲), 好记性不如抄下来(烂笔头). 信息爆炸的时代, 学习是一项持续的工作.

文章分类

全部博文(1727)

文章存档

2024年(3)

2023年(26)

2022年(112)

2021年(217)

2020年(157)

2019年(192)

2018年(81)

2017年(78)

2016年(70)

2015年(52)

2014年(40)

2013年(51)

2012年(85)

2011年(45)

2010年(231)

2009年(287)

分类: LINUX

2009-12-08 16:20:20

其实参考一下mms 一类的程序就明白了。仿造一份就OK。但是关键是要知道原理。


怎样让一个Service开机自动启动

1.首先开机启动后系统会发出一个Standard Broadcast Action,名字叫android.intent.action.BOOT_COMPLETED,这个Action只会发出一次。

2.构造一个BroadcastReceiver类,重构其抽象方法onReceive(Context context, Intent intent),在其中启动你想要启动的Service。

3.AndroidManifest.xml中,首先加入来获得BOOT_COMPLETED的使用许可,然后注册前面重构的IntentReceiver类,在其中加入 ,以使其能捕捉到这个Action。

一个例子


      package="com.xinocomm.device.ps2"
      android:versionCode="1"
      android:versionName="1.0">
   
   

   
       
 
       
           
               
               
           

       

   


   





receiver 部分

package com.xinocomm.device.ps2;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;


public class XinoDeviceReceiver extends BroadcastReceiver {
    /*要接收的intent源*/
    static final String ACTION = "android.intent.action.BOOT_COMPLETED";
    
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(ACTION)) {
            context.startService(new Intent(context, Ps2Dev.class));//启动PS2服务

            Toast.makeText(context, "PS2 device monitor service has started!", Toast.LENGTH_LONG).show();    
        }        
    }
}


阅读(1867) | 评论(0) | 转发(0) |
0

上一篇:个人简历

下一篇:android 手动配置 emulator

给主人留下些什么吧!~~