Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.hzx.servuce_update_ui.MainActivity">
<Button
android:id="@+id/start_service"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="start service"/>
<Button
android:id="@+id/bind_service"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bing service"/>
<TextView
android:id="@+id/show_textView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Service
package com.example.hzx.servuce_update_ui;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
/**
* Created by hzx on 2018/11/5.
*/
public class MyService extends Service {
public static final int UPDATE_TEXT = 1;
private final String TAG = "123";
private Handler handler;
private MyBinder mBinder = new MyBinder();
private int ble_count;
private MyThread myThread;
class MyBinder extends Binder {
public void init_handler(Handler handler){
Log.d(TAG,"-->init_handler");
set_handler(handler);
}
public int start_Thread(){
if (get_handler() == null) {
Log.d(TAG,"-->get_handler() == null");
return -1;
}
Log.d(TAG,"-->start_Thread");
myThread = new MyThread ();
myThread.start();
return 0;
}
}
private void set_handler(Handler handler){
this.handler = handler;
}
private Handler get_handler(){
return this.handler;
}
private void setBle_count(int ble_count){
this.ble_count = ble_count;
}
private int getBle_count(){
return this.ble_count;
}
public MyService(){
this.handler = null;
//this.mBinder = null;
this.ble_count = 0;
}
class MyThread extends Thread {
public void run() {
while(true) {
Message message = new Message();
message.what = UPDATE_TEXT;
Bundle bundle = new Bundle();
int temp;
temp = getBle_count();
bundle.putString("ble", "" + temp);
temp++;
setBle_count(temp);
message.setData(bundle);
get_handler().sendMessage(message);
Log.d(TAG, "-->Threa send:" + temp);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Log.d(TAG, "-->InterruptedException:" + temp);
e.printStackTrace();
}
}
}
}
public IBinder onBind(Intent intent){
return mBinder;
}
public void onCreate(){
super.onCreate();
Log.d(TAG,"-->onCreate");
}
public int onStartCommand(Intent intent, int flags, int startId){
Log.d(TAG,"-->onStartCommand");
return super.onStartCommand(intent,flags,startId);
}
public void onDestroy(){
Log.d(TAG,"-->onDestroy");
super.onDestroy();
}
}
MaintActivity
package com.example.hzx.servuce_update_ui;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private final String TAG = "123";
public static final int UPDATE_TEXT = 1;
private MyService.MyBinder myBinder;
private TextView show_textView;
private Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button startService = (Button)findViewById(R.id.start_service);
startService.setOnClickListener(MainActivity.this);
Button bingService = (Button)findViewById(R.id.bind_service);
bingService.setOnClickListener(MainActivity.this);
show_textView = (TextView)findViewById(R.id.show_textView);
show_textView.setText("123");
handler = new FirstHandler();
}
private ServiceConnection connetction = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
Log.d(TAG,"activity onServiceConnected");
myBinder = (MyService.MyBinder)iBinder;
myBinder.init_handler(handler);
myBinder.start_Thread();
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
}
};
class FirstHandler extends Handler{
public void handleMessage(Message msg){
int what = msg.what;
Log.d(TAG,"activity handleMessage:"+what);
switch (what){
case UPDATE_TEXT:
String string = msg.getData().getString("ble");
show_textView.setText(string);
break;
}
}
}
public void onClick(View v){
switch (v.getId()){
case R.id.start_service:
Log.d(TAG,"activity start_service");
Intent startIntent = new Intent(this,MyService.class);
startService(startIntent);
break;
case R.id.bind_service:
Log.d(TAG,"activity bind_service");
Intent bindIntent = new Intent(this,MyService.class);
bindService(bindIntent,connetction,BIND_AUTO_CREATE);
break;
default:
break;
}
}
}
注意:
1、 需要在AndroidManifest.xml文件注册
<service android:name=".MyService"/>
效果:
网友评论