首页 / 操作系统 / Linux / 一个好玩的Android应用程序/开机自启动
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); } }
收藏该网址