import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;public class Activity01 extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//从main.xml布局中获得Button对象
Button button_start = (Button)findViewById(R.id.start);
Button button_stop = (Button)findViewById(R.id.stop);
//设置按钮(Button)监听
button_start.setOnClickListener(start);
button_stop.setOnClickListener(stop); }
//开始按钮
private OnClickListener start = new OnClickListener()
{
public void onClick(View v)
{
//开启Service
startService(new Intent("com.yarin.Android.MUSIC"));
}
};
//停止按钮
private OnClickListener stop = new OnClickListener()
{
public void onClick(View v)
{
//停止Service
stopService(new Intent("com.yarin.Android.MUSIC"));
}
};}import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;public class MusicService extends Service{
private MediaPlayer player;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
} public void onStart(Intent intent, int startId){
super.onStart(intent, startId);
player = MediaPlayer.create(this, R.raw.test);
player.start();
}
public void onDestory(){
Log.d("aaa", "bbb");
super.onDestroy();
player.stop();
}
}主要代码就是这样,我是用的2.2,就是通过服务来启动一个音频文件的播放/停止,现在是播放可以,但是停止不了,点了“停止”按钮没反应,不知为何啊,我打了Log发现也没进onDestory方法
改为startService(new Intent(this,MusicService.class)); stopService(new Intent("com.yarin.Android.MUSIC"));
改为 stopService(new Intent(this,MusicService.class));
component = new ComponentName(this, MusicService.class);
Intent mIntent;
mIntent = new Intent();
mIntent.setAction("com.yarin.Android.MUSIC_STOP");
mIntent.setComponent(component);
startService(mIntent);
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gejing.Chapter3_2"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Activity01"
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=".MusicService">
<intent-filter>
<action android:name="com.yarin.Android.MUSIC" />
<category android:name="android.intent.category.default" />
</intent-filter>
</service>
</application>
<uses-sdk android:minSdkVersion="5" />
</manifest>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/start"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="开始播放"/>
<Button
android:id="@+id/stop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="停止播放"
/>
</LinearLayout>
string.xml<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">使用Service播放音乐例子,谢谢使用!</string>
<string name="app_name">Examples_03_03</string>
</resources>再就是raw中放一个音频文件,就这些文件了
上面是SDK的内容,如果要用stopService(..)方法,必须通过bindService方法而不是startService!
还有重写系统的方法最好加上@Override,还有你的onDestory拼写错鸟下面是我改的代码,运行通过
先把<uses-sdk android:minSdkVersion="5" />中的5改成8,与2.2对应。package com.gejing.Chapter3_2;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 Activity01 extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//从main.xml布局中获得Button对象
Button button_start = (Button)findViewById(R.id.start);
Button button_stop = (Button)findViewById(R.id.stop);
//设置按钮(Button)监听
button_start.setOnClickListener(start);
button_stop.setOnClickListener(stop); }
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
} public void onServiceDisconnected(ComponentName className) {
}
}; protected void doBindService() {
bindService(new Intent(this, MusicService.class), mConnection,
Context.BIND_AUTO_CREATE); }
//开始按钮
private OnClickListener start = new OnClickListener()
{
public void onClick(View v)
{
//开启Service
doBindService();
}
};
//停止按钮
private OnClickListener stop = new OnClickListener()
{
public void onClick(View v)
{
//停止Service
unbindService(mConnection);
}
};}package com.gejing.Chapter3_2;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;public class MusicService extends Service{
private MediaPlayer player;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
} @Override
public void onCreate(){
player = MediaPlayer.create(this, R.raw.test);
player.start();
}
@Override
public void onStart(Intent intent, int startId){
super.onStart(intent, startId);
}
@Override
public void onDestroy(){
Log.d("aaa", "bbb");
super.onDestroy();
player.stop();
}
}
其实我2了,又想得太多。
只要把onDestroy()拼对就解决问题了。
原因是onDestroy写成了onDestory。注意,应该是onDestroy!!