国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

開機自動運行程序!

2019-11-09 16:21:53
字體:
來源:轉載
供稿:網友

使用場景:手機開機后,自動運行程序,在屏幕上顯示”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

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

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

<intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <receiver android:name=".BootBroadcastReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>

android.intent.action.BOOT_COMPLETED 消息。不要忘記配置 android.permission.RECEIVE_BOOT_COMPLETED 權限。 完成后,編譯出 apk 包,安裝到模擬器或手機中。關機,重新開機。 如果是系統進程的話,需要加入 init 配置文件來啟動,隨著 linux 啟動而啟動


上一篇:系統運行庫

下一篇:Eclipse SVN插件安裝

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 鄂伦春自治旗| 工布江达县| 石嘴山市| 潢川县| 荥经县| 泾阳县| 济源市| 鄂尔多斯市| 呼伦贝尔市| 肇庆市| 华阴市| 通江县| 澄江县| 禹城市| 灵川县| 莲花县| 额敏县| 永靖县| 常宁市| 七台河市| 繁峙县| 馆陶县| 潢川县| 寿阳县| 安化县| 松桃| 集安市| 凤冈县| 关岭| 家居| 庄河市| 武功县| 东兰县| 金堂县| 库尔勒市| 延边| 赤壁市| 余庆县| 晋宁县| 中卫市| 盈江县|