編輯:關於Android編程
原理,在收到系統開機廣播後,啟動一個透明的activity,在activity裡面啟動一個服務。
關鍵代碼如下:
1、開機廣播接受者
public class BootReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(android.intent.action.BOOT_COMPLETED)) {
Log.d(BootReceiver, system boot completed);
// context, AutoRun.class
Intent newIntent = new Intent(context, AutoRun.class);
/* MyActivity action defined in AndroidManifest.xml */
newIntent.setAction(android.intent.action.MAIN);
/* MyActivity category defined in AndroidManifest.xml */
newIntent.addCategory(android.intent.category.LAUNCHER);
/*
* If activity is not launched in Activity environment, this flag is
* mandatory to set
*/
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
/* if you want to start a service, follow below method */
context.startActivity(newIntent);
}
}
}
2、開機啟動的activty透明
3、
public class AutoRun extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//去除title
requestWindowFeature(Window.FEATURE_NO_TITLE);
//去掉Activity上面的狀態欄
getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN , WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
Log.i(開機啟動,開機啟動);
startService(new Intent(this,EndClientService.class));
finish();
}
}
3、開機啟動的服務
public class EndClientService extends Service {
private Intent intent2=null;
public EndClientService() {
// TODO Auto-generated constructor stub
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Log.i(開機服務,服務開啟);
IntentFilter filter=new IntentFilter();
filter.addAction(android.provider.Telephony.SMS_RECEIVED);
filter.setPriority(Integer.MAX_VALUE);
registerReceiver(mReceiver, filter);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(服務,服務運行中);
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
unregisterReceiver(mReceiver);
mReceiver=null;
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
Android教你如何一步步打造通用適配器
前言在Android開發中ListView是最為常用的控件之一,基本每個應用都會涉及到它,要使用ListView列表展示,就不可避免地涉及到另外一個東西—&m
Android Studio安裝更新終極解決方式
Android Studio安裝更新終極解決方式之前寫過一篇Android SDK無法更新的博文,其實該方式對Android Studio同樣有效,大伙可以下載網盤中分享
通過android XML 創建圖行,減少對美工的依賴
在開發中總會需要自定義各種View的樣式,如果總是依賴美工作圖弄出需要的UI樣式圖片(比如:一個按鈕要選中和默認兩張圖片),不但時間上會浪費、往往也會有適配問題。雖然可以
Android網絡收音機--使用Vitamio解碼(二)
在前面提到Android網絡收音機--使用Vitamio解碼(一)實現了播放流媒體文件,但只能播放一次,在這裡可以隨機點擊左邊的menu來播放,但Vitamio還是不是很
【React Native開發】React Native控件之DrawerLayoutAndroid抽屜導航切換組件講解(13)
(一)前言今天我們一起來看一下抽屜DrawerLayoutAndroid