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

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

開機自動運行程序!

2019-11-09 17:26:06
字體:
來源:轉載
供稿:網友

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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 封开县| 竹溪县| 乌兰察布市| 大埔县| 桂阳县| 惠东县| 莱西市| 朝阳县| 金溪县| 临漳县| 东兴市| 遂平县| 临沭县| 琼海市| 青海省| 安康市| 西充县| 青龙| 于田县| 南阳市| 曲周县| 华阴市| 嵊州市| 乌拉特后旗| 大余县| 定陶县| 迁西县| 包头市| 武安市| 阜康市| 察哈| 新田县| 土默特右旗| 霍州市| 汝阳县| 京山县| 通江县| 衡阳市| 阳城县| 固原市| 黑水县|