博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android---让你的APK程序开机自动运行(转)
阅读量:6942 次
发布时间:2019-06-27

本文共 2635 字,大约阅读时间需要 8 分钟。

转自: http://blog.sina.com.cn/s/blog_72f6e45701014l6t.html

有些时候,应用需要在开机时就自动运行,例如某个自动从网上更新内容的后台service。怎样实现开机自动运行的应用?在撰写本文时,联想到高焕堂先生以“Don't call me, I'll call you back!”总结Android框架,真是说到点子上了。理解这句话的含义,许多有关Android平台上实现某种功能的问题,都能迎刃而解。


使用场景:手机开机后,自动运行程序,在屏幕上显示"Hello. I started!"字样。
 
背景知识:当Android启动时,会发出一个系统广播,内容为ACTION_BOOT_COMPLETED,它的字符串常量表示为 android.intent.action.BOOT_COMPLETED。只要在程序中“捕捉”到这个消息,再启动之即可。记住,Android框架 说:Don't call me, I'll call you back。我们要做的是做好接收这个消息的准备,而实现的手段就是实现一个BroadcastReceiver。
 
代码解析:
 
1、界面Activity:SayHello.java
 
package com.ghstudio.BootStartDemo;  
  
import android.app.Activity;  
import android.os.Bundle;  
import android.widget.TextView;  
  
public class SayHello extends Activity {  
  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        TextView tv = new TextView(this);  
        tv.setText("Hello. I started!");  
        setContentView(tv);  
    }  
}  
 
 
这段代码很简单,当Activity启动时,创建一个TextView,用它显示"Hello. I started!"字样。
 
2、接收广播消息:BootBroadcastReceiver.java
 
package com.ghstudio.BootStartDemo;  
  
import android.content.BroadcastReceiver;  
import android.content.Context;  
import android.content.Intent;  
  
public class BootBroadcastReceiver extends BroadcastReceiver {  
  
 static final String ACTION = "android.intent.action.BOOT_COMPLETED";  
   
 @Override  
 public void onReceive(Context context, Intent intent) {  
    
  if (intent.getAction().equals(ACTION)){  
   Intent sayHelloIntent=new Intent(context,SayHello.class);  
   sayHelloIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
   context.startActivity(sayHelloIntent);  
  }  
 }  
}  
 
该类派生自BroadcastReceiver,覆载方法onReceive中,检测接收到的Intent是否符合BOOT_COMPLETED,如果符合,则启动SayHello那个Activity。
 
3、配置文件:AndroidManifest.xml
 
<?xml version="1.0" encoding="utf-8"?>  
<manifest xmlns:android="http://schemas.android.com/apk/res/android"  
      package="com.ghstudio.BootStartDemo"  
      android:versionCode="1"  
      android:versionName="1.0">  
    <application android:icon="@drawable/icon" android:label="@string/app_name">  
        <activity android:name=".SayHello"  
                  android:label="@string/app_name">  
            <intent-filter>  
                <action android:name="android.intent.action.MAIN" />  
                <category android:name="android.intent.category.LAUNCHER" />  
            </intent-filter>  
        </activity>  
  <receiver android:name=".BootBroadcastReceiver">  
  <intent-filter>  
    <action android:name="android.intent.action.BOOT_COMPLETED" />  
   </intent-filter>  
  </receiver>  
    </application>  
    <uses-sdk android:minSdkVersion="3" />  
  
   <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>  
  
</manifest>   
 
注意其中粗体字那一部分,该节点向系统注册了一个receiver,子节点intent-filter表示接收android.intent.action.BOOT_COMPLETED消息。不要忘记配置
android.permission.RECEIVE_BOOT_COMPLETED权限。
 
代码下载 http://pan.baidu.com/s/1hqgQhik

转载地址:http://vzinl.baihongyu.com/

你可能感兴趣的文章
golang: 常用数据类型底层结构分析
查看>>
How to Set Cores-Per-Socket Parameter for a Virtual Machine
查看>>
我的友情链接
查看>>
如何做出一个弹出窗口
查看>>
solidity 0.5.7简明教程
查看>>
你好啊中国
查看>>
冯柯:同步设计在高性能OLTP数据库中的实践
查看>>
iPhone史上最全的使用教程
查看>>
推广一款不错的应用“锁屏对对碰”
查看>>
Oracle IO问题解析(九)
查看>>
How to Properly Remove Datastore or LUN from ESXi 5.x hosts
查看>>
[读书]读《重构-改善既有代码的设计》
查看>>
我的友情链接
查看>>
一次owa登录exchange邮箱提示下载owaauth.dll的解决办法
查看>>
我的友情链接
查看>>
Log.cat 的使用
查看>>
使用myEclipse把java项目上传到码云
查看>>
Python爬虫框架Scrapy 学习笔记 5 ------- 使用pipelines过滤敏感词
查看>>
GTM+800的时间格式转成yyyy-mm-dd的格式
查看>>
QT下载地址
查看>>