本來打算周更的。。過年那段時間盡情的放縱去了。
我作為一個菜鳥,我來介紹一點關于Service的用法。
Service 是 Android 四大基本組件之一,是無界面的應用程序,可以長期在后臺運行,在實際工作中非常重要。比如我們在聽歌的時候突然來一條信息,那么當我點開信息的時候,我的音樂也要運行,這個時候就要服務了。
首先我介紹如何啟動服務。
先創建一個Activity.我這里就寫MainActivyt。創建完成之后在創建一個服務,

創建完成之后,我們先將主布局改為LinearLayout
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.liqipeter.startservice.MainActivity"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tvout" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Hello World!" android:id="@+id/etData" /> <Button android:text="啟動服務" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnstartservice" /> <Button android:text="停止服務" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnstopservice" /> <Button android:text="綁定服務" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnbingdservice" /> <Button android:text="解除綁定" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnendservice" /> <Button android:text="同步數據" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/updata" /></LinearLayout>然后在Activity中初始化控件,并且添加點擊事件findViewById(R.id.btnstartservice).setOnClickListener(this); findViewById(R.id.btnstopservice).setOnClickListener(this); findViewById(R.id.btnbingdservice).setOnClickListener(this); findViewById(R.id.btnendservice).setOnClickListener(this); findViewById(R.id.updata).setOnClickListener(this);然后在點擊事件中開啟服務switch (view.getId()){ case R.id.btnstartservice: Intent i = new Intent(MainActivity.this,MyService.class); i.putExtra("data",etData.getText().toString()); startService(i); break; case R.id.btnstopservice: stopService(new Intent(MainActivity.this,MyService.class)); break; case R.id.btnbingdservice: bindService(new Intent(this,MyService.class),this, Context.BIND_AUTO_CREATE); break; case R.id.btnendservice: unbindService(this); break; case R.id.updata: if (binder!=null){ binder.setData(etData.getText().toString()); } break; }然后在Service中寫一個方法running = true; new Thread(){ @Override public void run() { super.run(); int i = 0; while (running){ i++; String str =i+":"+data; System.out.PRintln(str); if (callback!=null){ callback.onDataChange(str); } try { sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }.start();當我們點擊綁定服務的時候,我們會將data值傳到服務中,服務中接收到之后,把他顯示在TextView上。但是不能直接顯示,因為不能在子線程更新UI,所以我們要在寫一個消息機制。private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); tvout.setText(msg.getData().getString("data")); } };這是效果圖:下面我附上我的GitHub地址源代碼在這里:https://github.com/FoxconnPeter/StartService(ps:本人現在想找份工作,在深圳。至于薪資我目前在深圳工廠做Android,薪資5000左右。我想學更多東西和提升自己的價值郵箱:597058061@QQ.com)
新聞熱點
疑難解答