Android中的Service服务绑定
来源:互联网 发布:情定三生知夏的床戏 编辑:程序博客网 时间:2024/05/17 22:33
第一,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.IBinder;import android.support.v7.app.ActionBarActivity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.EditText;public class MainActivity extends ActionBarActivity implements View.OnClickListener, ServiceConnection { private EditText etData; private MyService.Binder binder = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); etData = (EditText) findViewById(R.id.etData); 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; } @Override public void onServiceDisconnected(ComponentName name) { }}
第二,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; } } @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(); while (running) { System.out.println(data); try { sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }.start(); } @Override public void onDestroy() { super.onDestroy(); running = false; }}
第三步,在MainXmal中的代码如下:
<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"> <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>
最后,配置Mainfest文件:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.yangjian.connectservice" > <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".MyService" android:enabled="true" android:exported="true" > </service> </application></manifest>
0 0
- Android中的Service服务绑定
- Service中的绑定服务总结
- Android中的Service的用法之绑定服务
- Android service(二)绑定服务
- Android -- service之绑定服务
- Android---服务(Service)的绑定服务
- android中的服务---Service
- Android中的Service的用法 开启服务,下一篇讲绑定服务
- Android Service---创建绑定类型服务
- Android Service---创建绑定类型服务
- 【Android】Activity与服务Service绑定
- Android Service之绑定本地服务
- Android--Service之绑定服务交互
- Android Service 之绑定服务和AIDL
- Android Service(服务)绑定远程服务一个小demo
- Android中的服务(service)--AIDL远程服务
- service(服务绑定)
- service(服务绑定)
- mysql编码设置
- spring中@value注解需要注意
- jQuery判断浏览器是移动端还是电脑端自动跳转
- 安卓自定义View实现图片上传进度显示(仿QQ)
- 黑马程序员---c语言字符串输入总结
- Android中的Service服务绑定
- oracle数据库在ibatis的批处理
- 初学iOS UI 用于交流与学习
- 内联成员函数与重载成员函数
- java入门相关
- 剑指Offer面试题7(Java版):用两个栈实现队列与用两个队列实现栈
- 【剑指Offer面试题】 九度OJ1386:旋转数组的最小数字
- Android中Application类用法
- ajax的接受之页面,一定要有echo,html页面的返回函数success才能正常运行