service类:
package com.example.service;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
/**
* service也是运行在主线程,如果处理比较耗时的操作一样要另起新线程,
* 没有startservice的情况下stopservice不会报错,没有onbindservice的情况下,onunbindservice会报错。
* bindservice需要serviceconnection对象关联。start不需要。
* 需要在配置文件中声明service。
*
* @author Administrator
*
*/
public class servicedemo extends Service {
private MediaPlayer mediaPlayer = null;
private static final String TAG = "MusicService";
//oncreate-onstart-ondestroy
//oncreate-onbind-onunbind-ondestroy
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(servicedemo.this, "onBind()", Toast.LENGTH_LONG).show();
Log.i(TAG, "onBind()");
mediaPlayer.start();
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
Toast.makeText(servicedemo.this, "onCreate()", Toast.LENGTH_LONG).show();
Log.i(TAG, "onCreate()");
mediaPlayer=MediaPlayer.create(servicedemo.this, R.raw.huranzhijian);
//在res文件夹下新建raw文件夹,放入一个mp3文件作为播放文件。
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
Toast.makeText(servicedemo.this, "onDestroy()", Toast.LENGTH_LONG).show();
Log.i(TAG, "onDestroy()");
mediaPlayer.stop();
}
@Override
public void onRebind(Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(servicedemo.this, "onRebind()", Toast.LENGTH_LONG).show();
Log.i(TAG, "onRebind()");
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
Toast.makeText(servicedemo.this, "onStart()", Toast.LENGTH_LONG).show();
Log.i(TAG, "onStart()");
mediaPlayer.start();
}
@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(servicedemo.this, "onUnbind()", Toast.LENGTH_LONG).show();
Log.i(TAG, "onUnbind()");
return false;
}
}
主Activity:
package com.example.service;
import android.app.Activity;
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.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button start = null;
private Button stop = null;
private Button onbind = null;
private Button unbind = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start = (Button) findViewById(R.id.service_start);
stop = (Button) findViewById(R.id.service_stop);
onbind = (Button) findViewById(R.id.service_onbind);
unbind = (Button) findViewById(R.id.service_unbind);
start.setOnClickListener(clickListener);
stop.setOnClickListener(clickListener);
onbind.setOnClickListener(clickListener);
unbind.setOnClickListener(clickListener);
}
/**
* ServiceConnection
*/
final ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
// 解除链接时调用
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
// 绑定链接时调用
}
};
android.view.View.OnClickListener clickListener = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,
com.example.service.servicedemo.class);
switch (v.getId()) {
case R.id.service_start:
startService(intent);
break;
case R.id.service_stop:
stopService(intent);
break;
case R.id.service_onbind:
bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
break;
case R.id.service_unbind:
unbindService(serviceConnection);
}
}
};
}
布局文件:
<RelativeLayout 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"
tools:context=".MainActivity" >
<Button
android:id="@+id/service_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="22dp"
android:layout_marginTop="35dp"
android:text="start" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/service_start"
android:text="service使用"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/service_stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/service_start"
android:layout_alignBottom="@+id/service_start"
android:layout_alignLeft="@+id/textView1"
android:layout_marginLeft="20dp"
android:text="stop" />
<Button
android:id="@+id/service_onbind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/service_stop"
android:layout_alignBottom="@+id/service_stop"
android:layout_toRightOf="@+id/textView1"
android:text="onbind" />
<Button
android:id="@+id/service_unbind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/service_onbind"
android:layout_alignBottom="@+id/service_onbind"
android:layout_alignParentRight="true"
android:text="unbind" />
</RelativeLayout>
配置文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.service"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<service android:name="com.example.service.servicedemo" >
</service>
<activity
android:name="com.example.service.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>
</application>
</manifest>