在Android中如何侦听Service内部的状态
来源:互联网 发布:电脑程序编程培训 编辑:程序博客网 时间:2024/06/05 15:45
第一步,在MainActivity中:
package com.example.yangjian.connectservice;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.os.Message;import android.support.v7.app.ActionBarActivity;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.EditText;import android.widget.TextView;public class MainActivity extends ActionBarActivity implements View.OnClickListener, ServiceConnection { private EditText etData; private MyService.Binder binder = null; private TextView tvOut; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); etData = (EditText) findViewById(R.id.etData); tvOut = (TextView) findViewById(R.id.tvOut); findViewById(R.id.btnStartService).setOnClickListener(this); findViewById(R.id.btnStopService).setOnClickListener(this); findViewById(R.id.btnBindService).setOnClickListener(this); findViewById(R.id.btnUnbindService).setOnClickListener(this); findViewById(R.id.btnSyncData).setOnClickListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btnStartService: Intent i = new Intent(this, MyService.class); i.putExtra("data", etData.getText().toString()); startService(i); break; case R.id.btnStopService: stopService(new Intent(this, MyService.class)); break; case R.id.btnBindService: bindService(new Intent(this, MyService.class), this, Context.BIND_AUTO_CREATE); break; case R.id.btnUnbindService: unbindService(this); break; case R.id.btnSyncData: if (binder != null) { binder.setData(etData.getText().toString()); } break; } } @Override public void onServiceConnected(ComponentName name, IBinder service) { binder = (MyService.Binder)service; binder.getService().setCallback(new MyService.Callback() { @Override public void onDataChange(String data) { Message msg = new Message(); Bundle b = new Bundle(); b.putString("data", data); msg.setData(b); handler.sendMessage(msg); } }); } @Override public void onServiceDisconnected(ComponentName name) { } private android.os.Handler handler = new android.os.Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); tvOut.setText(msg.getData().getString("data")); } };}
第二步,在MyService中:
package com.example.yangjian.connectservice;import android.app.Service;import android.content.Intent;import android.os.IBinder;public class MyService extends Service { private boolean running = false; private String data = "information"; public MyService() { } @Override public IBinder onBind(Intent intent) { return new Binder(); } public class Binder extends android.os.Binder{ public void setData(String data) { MyService.this.data = data; } public MyService getService() { return MyService.this; } } @Override public int onStartCommand(Intent intent, int flags, int startId) { data = intent.getStringExtra("data"); return super.onStartCommand(intent, flags, startId); } @Override public void onCreate() { super.onCreate(); 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(); } @Override public void onDestroy() { super.onDestroy(); running = false; } private Callback callback = null; public static interface Callback { void onDataChange(String data); } public void setCallback(Callback callback) { this.callback = callback; } public Callback getCallback() { return callback; }}
在MainXml中:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context=".MainActivity"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/tvOut"/> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="默认信息" android:id="@+id/etData"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="启动服务" android:id="@+id/btnStartService"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="停止服务" android:id="@+id/btnStopService"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="绑定服务" android:id="@+id/btnBindService" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="解除绑定服务" android:id="@+id/btnUnbindService" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="同步数据" android:id="@+id/btnSyncData" /></LinearLayout>
0 0
- 在Android中如何侦听Service内部的状态
- 举例说明如何在android中使用Service
- Android如何在service中显示Dialog
- Android 在Service中监听软键盘显示状态
- Android中关于短信侦听的一个类
- 如何查看Android 中native的Service
- android中如何提高service的优先级
- 如何查看Android 中native的Service
- Erlang如何查看gen_server内部的状态
- 在一个应用程序,如何监测一个service内部的数据的变化
- android 系统的电池侦听
- android 在service中如何向activity传递数据
- 如何在Android中利用AIDL添加service
- Android中如何让Service不被一键清理且在息屏后保活
- android 如何在service中定时请求网络
- 如何在Service方法中抛出异常并返回状态或校验结果?
- 在android中如何判断当前的apk是否处于运行状态
- eclipse中如何查看一个android模拟器的内部文件
- S3C2440 解决LCD图像偏移问题
- Python 从 mat 文件读写 到 h5py 文件包
- java文本输入
- Spring MVC之@RequestBody, @ResponseBody 详解
- 如何更好的建设网站外链
- 在Android中如何侦听Service内部的状态
- 【HDU 5294】Tricks Device(最短路+最大流)
- 解压多个压缩包时,解压后文件被放置在一个文件下的处理
- ContentObserver——窃听用户发出的短信
- 【图文】细说日本动画的发展史
- 动态规划求最大子段和
- mysql进阶(九)多表查询
- POJ 2777 Count Color(区间更新 + 状压)
- The summary of competition season