歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 一個好玩的Android應用程序/開機自啟動

一個好玩的Android應用程序/開機自啟動

日期:2017/3/1 10:01:59   编辑:Linux編程

Android這個例子實現了一個小程序,這個程序的特殊之處在於只要運行一次,然後它就會伴隨著手機的啟動而自己運行。

首先,為了捕捉手機啟動的事件,我們需要在AndroidManifest.xml文件中添加如下的代碼:

<!-- 委派receiver名稱為類別名稱 -->
<receiver android:name="HippoStartupIntentReceiver" >
<!-- 在filter裡設定BOOT_COMPLETED為要捕捉的訊息 -->
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>

其中:

android.intent.action.BOOT_COMPLETED

這行代碼是接收系統發送的廣播事件。

下面給出這個程序的完整代碼:

1.主程序的代碼

這段代碼其實就是提供一個Activity UI界面,不負責接收系統廣播

public class EX06_16 extends Activity
{
/* 本程序只需運行一次,就會?日後開機時自動運行 */
private TextView mTextView01;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

/* 為了快速示意,程序僅一歡迎的TextView文字作為演示 */
mTextView01 = (TextView)findViewById(R.id.myTextView1);
mTextView01.setText(R.string.str_welcome);
}
}

Copyright © Linux教程網 All Rights Reserved