码迷,mamicode.com
首页 > 移动开发 > 详细

Android 服务消息侦听

时间:2016-06-15 09:13:12      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:

技术分享技术分享技术分享技术分享技术分享

 1 package com.example.metrox.l15;
 2 
 3 import android.app.Service;
 4 import android.content.Intent;
 5 import android.os.Binder;
 6 import android.os.IBinder;
 7 
 8 public class MyService extends Service {
 9     private boolean isRun = false;
10     private String data = "服务默认信息";
11     public MyService() {
12     }
13 
14     @Override
15     public IBinder onBind(Intent intent) {
16         return  new Binder();
17     }
18 
19     public class Binder extends android.os.Binder{
20         public void setData(String data){
21             MyService.this.data = data;
22         }
23         public MyService getService(){
24             return MyService.this;
25         }
26     }
27 
28     @Override
29     public int onStartCommand(Intent intent, int flags, int startId) {
30         System.out.println("服务已启动...");
31         data = intent.getStringExtra("data");
32 
33         return super.onStartCommand(intent, flags, startId);
34     }
35 
36     @Override
37     public void onCreate() {
38         super.onCreate();
39         System.out.println("服务已创建...");
40         isRun = true;
41         new Thread(){
42             @Override
43             public void run() {
44                 super.run();
45                 int i = 0;
46                 while (isRun){
47                     i++;
48                     String str = i+ data;
49                     System.out.println(str);
50                     if(callback != null){
51                          callback.OnDataChange(str);
52                     }
53                     try {
54                         sleep(1000);
55                     } catch (InterruptedException e) {
56                         e.printStackTrace();
57                     }
58                 }
59             }
60         }.start();
61     }
62 
63     @Override
64     public void onDestroy() {
65         super.onDestroy();
66         isRun = false;
67         System.out.println("服务已消毁...");
68     }
69 
70     public Callback callback = null;
71 
72     public void setCallback(Callback callback) {
73         this.callback = callback;
74     }
75 
76     public Callback getCallback() {
77         return callback;
78     }
79 
80     public static interface Callback{
81         void OnDataChange(String data);
82     }
83 }
 1 package com.example.metrox.l15;
 2 
 3 import android.content.ComponentName;
 4 import android.content.Intent;
 5 import android.content.ServiceConnection;
 6 import android.os.Handler;
 7 import android.os.IBinder;
 8 import android.os.Message;
 9 import android.provider.Settings;
10 import android.support.v7.app.AppCompatActivity;
11 import android.os.Bundle;
12 import android.view.View;
13 import android.widget.EditText;
14 import android.widget.Switch;
15 import android.widget.TextView;
16 
17 public class MainActivity extends AppCompatActivity implements View.OnClickListener, ServiceConnection {
18     Intent intent;
19     EditText et;
20     TextView tv;
21     MyService.Binder binder = null;
22     private boolean  isBind = false;
23     private  boolean isRuning = false;
24     @Override
25     public void onClick(View view) {
26         switch(view.getId()){
27             case R.id.btnStartService:
28                 et = (EditText) findViewById(R.id.editText);
29                 intent.putExtra("data",et.getText().toString());
30                 startService(intent);
31                 isRuning = true;
32                 break;
33             case R.id.btnStopService:
34                 stopService(intent);
35                 break;
36             case R.id.btnBindService:
37                 bindService(intent,this,BIND_AUTO_CREATE);
38                 break;
39             case R.id.btnUnBindService:
40                 unbindService(this);
41                 break;
42             case R.id.btnSyncData:
43                 if(binder != null){
44                    binder.setData(et.getText().toString());
45                 }
46                 break;
47         }
48     }
49 
50     @Override
51     protected void onCreate(Bundle savedInstanceState) {
52         super.onCreate(savedInstanceState);
53         setContentView(R.layout.activity_main);
54         tv = (TextView) findViewById(R.id.textView);
55         intent = new Intent(MainActivity.this,MyService.class);
56         findViewById(R.id.btnStartService).setOnClickListener(this);
57         findViewById(R.id.btnStopService).setOnClickListener(this);
58         findViewById(R.id.btnBindService).setOnClickListener(this);
59         findViewById(R.id.btnUnBindService).setOnClickListener(this);
60         findViewById(R.id.btnSyncData).setOnClickListener(this);
61     }
62 
63     @Override
64     public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
65         System.out.println("服务已连接...");
66         binder = (MyService.Binder) iBinder;
67         binder.getService().setCallback(new MyService.Callback() {
68             @Override
69             public void OnDataChange(String data) {
70                   Message msg = new Message();
71                   Bundle b = new Bundle();
72                   b.putString("data",data);
73                   msg.setData(b);
74                   handler.sendMessage(msg);
75             }
76         });
77     }
78 
79     @Override
80     public void onServiceDisconnected(ComponentName componentName) {
81         System.out.println("服务已断开...");
82     }
83 
84     private Handler handler = new Handler(){
85         @Override
86         public void handleMessage(Message msg) {
87             super.handleMessage(msg);
88             tv.setText(msg.getData().getString("data"));
89         }
90     };
91 }
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:paddingBottom="@dimen/activity_vertical_margin"
 7     android:paddingLeft="@dimen/activity_horizontal_margin"
 8     android:paddingRight="@dimen/activity_horizontal_margin"
 9     android:paddingTop="@dimen/activity_vertical_margin"
10     tools:context="com.example.metrox.l15.MainActivity">
11 
12     <TextView
13         android:layout_width="wrap_content"
14         android:layout_height="wrap_content"
15         android:text="Hello World!"
16         android:id="@+id/textView" />
17 
18     <Button
19         android:layout_width="wrap_content"
20         android:layout_height="wrap_content"
21         android:text="停止服务"
22         android:id="@+id/btnStopService"
23         android:layout_marginTop="48dp"
24         android:longClickable="false"
25         android:layout_below="@+id/textView"
26         android:layout_centerHorizontal="true" />
27 
28     <Button
29         android:layout_width="wrap_content"
30         android:layout_height="wrap_content"
31         android:text="启动服务"
32         android:id="@+id/btnStartService"
33         android:layout_alignTop="@+id/btnStopService"
34         android:layout_alignParentLeft="true"
35         android:layout_alignParentStart="true" />
36     <Button
37         android:layout_width="wrap_content"
38         android:layout_height="wrap_content"
39         android:text="解绑服务"
40         android:id="@+id/btnUnBindService"
41         android:longClickable="false"
42         android:layout_alignTop="@+id/btnBindService"
43         android:layout_alignLeft="@+id/btnStopService"
44         android:layout_alignStart="@+id/btnStopService" />
45 
46     <Button
47         android:layout_width="wrap_content"
48         android:layout_height="wrap_content"
49         android:text="绑定服务"
50         android:id="@+id/btnBindService"
51         android:layout_centerVertical="true"
52         android:layout_alignParentLeft="true"
53         android:layout_alignParentStart="true" />
54 
55     <EditText
56         android:layout_width="match_parent"
57         android:layout_height="wrap_content"
58         android:id="@+id/editText"
59         android:layout_above="@+id/btnStartService"
60         android:layout_alignParentLeft="true"
61         android:layout_alignParentStart="true"
62         android:text="默认数据" />
63 
64     <Button
65         android:layout_width="wrap_content"
66         android:layout_height="wrap_content"
67         android:text="同步数据"
68         android:id="@+id/btnSyncData"
69         android:layout_alignParentBottom="true"
70         android:layout_alignParentLeft="true"
71         android:layout_alignParentStart="true"
72         android:layout_marginBottom="74dp" />
73 </RelativeLayout>

 

Android 服务消息侦听

标签:

原文地址:http://www.cnblogs.com/linhongquan/p/5586135.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!