package com.example.testservice;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class MyService extends Service {
//创建自己的绑定服务业务逻辑
class MusicBinder extends Binder{
public void ready(){
Log.d("MyService", "----ready Method---");
}
public void play(){
Log.d("MyService", "----play Method---");
}
}
private MusicBinder musicBinder = new MusicBinder();
@Override
public IBinder onBind(Intent arg0) {
Toast.makeText(this, "服务的onBind方法被调用", Toast.LENGTH_SHORT).show();
return musicBinder;
}
/**
* 服务第一次创建的时候调用
*/
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "服务的onCreate方法被调用", Toast.LENGTH_SHORT).show();
}
/**
* 服务每一次启动的时候调用
*/
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "服务的onStartCommand方法被调用", Toast.LENGTH_SHORT).show();
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
Toast.makeText(this, "服务的onDestroy方法被调用", Toast.LENGTH_SHORT).show();
super.onDestroy();
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="启动服务" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="停止服务" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="绑定服务" />
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="解绑服务" />
</LinearLayout>
package com.example.testservice;
import com.example.testservice.MyService.MusicBinder;
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.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener{
private Button startService_Button;
private Button stopService_Button;
private Button bindService_Button;
private Button unbindService_Button;
private MyService.MusicBinder musicBinder;
//创建ServiceConnection,在绑定服务的时候会用到。
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName service) {
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//类型转换
musicBinder = (MyService.MusicBinder) service;
//指挥服务需要做的工作
musicBinder.ready();
musicBinder.play();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取开启服务按钮
startService_Button = (Button) findViewById(R.id.button1);
//获取停止服务按钮
stopService_Button = (Button) findViewById(R.id.button2);
//获取绑定服务按钮
bindService_Button = (Button) findViewById(R.id.button3);
//获取解绑服务按钮
unbindService_Button = (Button) findViewById(R.id.button4);
//调用点击事件
startService_Button.setOnClickListener(this);
stopService_Button.setOnClickListener(this);
bindService_Button.setOnClickListener(this);
unbindService_Button.setOnClickListener(this);
}
/**
* 点击事件
*/
@Override
public void onClick(View view) {
switch(view.getId()){
case R.id.button1:
//“开启服务”按钮
Intent startIntent = new Intent(this,MyService.class);
//开启服务
startService(startIntent);
break;
case R.id.button2:
//“停止服务”按钮
Intent stopIntent = new Intent(this,MyService.class);
//停止服务
stopService(stopIntent);
break;
case R.id.button3:
//“绑定服务”按钮
Intent bindIntent = new Intent(this,MyService.class);
bindService(bindIntent, connection, BIND_AUTO_CREATE);
break;
case R.id.button4:
//“解绑服务”按钮
Intent unbindIntent = new Intent(this,MyService.class);
unbindService(connection);
break;
default:
break;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有