/**
* 链接到service时触发。
* name 链接到service组件的名称
* service 在service中调用onBund时返回的IBinder,主要用来进行信息的交流
*/
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i("通知", "链接成功!");
MyBinder binder = (MyBinder)service;
MyService myService = binder.getMyService();
int count = myService.getCount();
Log.i("通知", "count="+count);
}
<!-- service配置开始 --> <service android:name="MyService"></service> <!-- service配置结束 -->
package cn.com.chenzheng_java;
import cn.com.chenzheng_java.MyService.MyBinder;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* @description 对service进行简单的应用
*/
public class ServiceActivity extends Activity implements OnClickListener{
private Button button_start ;
private Button button_bind ;
private Button button_destroy ;
private Button button_unbind;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.service);
button_start = (Button) findViewById(R.id.button1_service);
button_bind = (Button) findViewById(R.id.button2_service);
button_destroy = (Button) findViewById(R.id.button3_service);
button_unbind = (Button) findViewById(R.id.button4_service);
button_start.setOnClickListener(this);
button_bind.setOnClickListener(this);
button_destroy.setOnClickListener(this);
button_unbind.setOnClickListener(this);
}
private class MyServiceConnection implements ServiceConnection{
/**
* 链接到service时触发。
* name 链接到service组件的名称
* service 在service中调用onBund时返回的IBinder,主要用来进行信息的交流
*/
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i("通知", "链接成功!");
MyBinder binder = (MyBinder)service;
MyService myService = binder.getMyService();
int count = myService.getCount();
Log.i("通知", "count="+count);
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.i("通知", "链接未成功!");
}
}
private MyServiceConnection serviceConnection = new MyServiceConnection();
@Override
public void onClick(View v) {
if(v == button_start){
Intent intent = new Intent();
intent.setClass(getApplicationContext(), MyService.class);
startService(intent);
}
if(v == button_bind){
Intent intent = new Intent();
intent.setClass(getApplicationContext(), MyService.class);
bindService(intent,serviceConnection , BIND_AUTO_CREATE);
}
if(v==button_destroy){
Intent intent = new Intent();
intent.setClass(getApplicationContext(), MyService.class);
stopService(intent);
}
if(v==button_unbind){
unbindService(serviceConnection);
}
}
}
package cn.com.chenzheng_java;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
/**
* @description 实现自己的service
* @author chenzheng_java
* @since 2011/03/18
*/
public class MyService extends Service {
MediaPlayer mediaPlayer;
/**
* 当用户调用bindService方法时会触发该方法 返回一个IBinder对象,我们可以通过该对象,对service中 的某些数据进行访问
*/
@Override
public IBinder onBind(Intent intent) {
Log.i("通知", "service绑定成功!");
return new MyBinder();
}
@Override
public void onCreate() {
Log.i("通知", "service创建成功!");
mediaPlayer = MediaPlayer.create(this, R.raw.aiweier);
mediaPlayer.setLooping(false);
super.onCreate();
}
@Override
public void onDestroy() {
mediaPlayer.stop();
Log.i("通知", "service销毁成功!");
super.onDestroy();
}
@Override
public void onRebind(Intent intent) {
Log.i("通知", "service重新绑定成功!");
super.onRebind(intent);
}
@Override
public void onStart(Intent intent, int startId) {
mediaPlayer.start();
Log.i("通知", "service start成功!");
super.onStart(intent, startId);
}
@Override
public boolean onUnbind(Intent intent) {
mediaPlayer.stop();
Log.i("通知", "service解绑成功!");
return super.onUnbind(intent);
}
private int count = 100;
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public class MyBinder extends Binder {
/**
* @return 返回一个个人的service对象
*/
MyService getMyService() {
return MyService.this;
}
}
}
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:text="启动" android:id="@+id/button1_service" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> <Button android:text="绑定" android:id="@+id/button2_service" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> <Button android:text="销毁" android:id="@+id/button3_service" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> <Button android:text="解绑" android:id="@+id/button4_service" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.com.chenzheng_java"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name="ServiceActivity"
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配置开始
-->
<service android:name="MyService"></service>
<!--
service配置结束
-->
</application>
</manifest>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有