android shutdown 和 reboot 操作可能有些朋友也和自己一樣有這種需求,開機啟動的網上資料非常多。
貼出自己原來寫的大家交流下。功能實現的源碼非常簡單,但牽涉到許可權的問題。
實現的思路:調用android的系統源碼,在源碼中編譯實現關機和啟動功能。
NOTE:必須在源碼中進行compile , 如果沒有源碼包使用eclipse ,在編輯器當中會直接提示錯誤。
file 1 :(採用BroadcastReceiver 發送廣播可以在每次開機時跑到你指定的class)<br />public class Boot_Receiver extends BroadcastReceiver {<br /> @Override<br /> public void onReceive(Context context,Intent intent){<br /> Intent i=new Intent(Intent.ACTION_RUN);<br /> i.setClass(context,C.class);<br /> i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);<br /> context.startActivity(i);<br /> }<br />}<br />file 2 :(shutdown 功能的實現)<br /> case R.id.Coldboot : {<br /> mPowerCycleType="Coldboot";<br /> Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);<br /> intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);<br /> intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);<br /> startActivity(intent);<br /> finish();<br /> }break;<br /> (restart功能的實現)<br /> case R.id.Warmboot : {<br /> mPowerCycleType="Warmboot";<br /> //Get the current system time<br /> mTime=GetSystemDate().toString();<br /> boot_SharedPreferences_put();<br /> Intent i=new Intent();<br /> i.setClass(B.this,C.class);<br /> startActivity(i);<br /> }break;<br />AndroidManifest.xml :(聲明許可權)<br /><manifest xmlns:android="http://schemas.android.com/apk/res/android"<br /> package="wistron.mojo"<br /> android:sharedUserId="android.uid.system"<br /> ><br /> <receiver android:name=".boot.Boot_Receiver"><br /> <intent-filter><br /> <action android:name="android.intent.action.BOOT_COMPLETED" /><br /> </intent-filter><br /> </receiver><br />。。。。。。。。<br />。。。。。。。。<br /> <uses-permission android:name="android.permission.HARDWARE_TEST"/--><br /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/><br /> <uses-permission android:name="android.permission.SHUTDOWN"/>