参考文档:https://www.jb51.net/article/90174.htm
1、在res目录中建立一个raw目录,并把一个音乐文件xxxx.mp3拷贝去。
2、ServiceTest/app/src/main/AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="example.com.servicetest">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".ServiceDemo"
android:enabled="true"
android:exported="true">
</service>
</application>
</manifest>
3、ServiceTest/app/src/main/res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="example.com.servicetest.MainActivity">
<Button
android:text="start Music Service"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginTop="11dp"
android:id="@+id/button"
android:onClick="startMusicService" />
<Button
android:text="stop Music Service"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/button"
android:layout_alignParentStart="true"
android:layout_marginTop="11dp"
android:id="@+id/button2"
android:onClick="stopMusicService"/>
<Button
android:text="bind Music Service"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/button2"
android:layout_alignParentStart="true"
android:layout_marginTop="15dp"
android:id="@+id/button3"
android:onClick="bindMusicService"/>
<Button
android:text="unBind Music Service"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/button3"
android:layout_alignParentStart="true"
android:layout_marginTop="18dp"
android:id="@+id/button4"
android:onClick="unBindMusicService"/>
</RelativeLayout>
4、ServiceTest/app/src/main/java/example/com/servicetest/ServiceDemo.java
package example.com.servicetest;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;
public class ServiceDemo extends Service {
private static final String TAG = "xxxxxx";
MediaPlayer mPlayer;
@Override
public void onCreate() {
mPlayer = MediaPlayer.create(getApplication(), R.raw.westlife);
mPlayer.setLooping(true);
Log.i(TAG, "Service.onCreate");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "Service.onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onStart(Intent intent, int startId) {
Log.i(TAG, "Service.onStart");
mPlayer.start();
}
@Override
public IBinder onBind(Intent intent) {
Log.e(TAG, "Service.onBind()!");
mPlayer.start();
return null;
}
@Override
public boolean onUnbind(Intent intent) {
// TODO: Return the communication channel to the service.
Log.e(TAG, "Service.onUnbind()!");
mPlayer.stop();
return false;
}
@Override
public void onDestroy() {
Log.i(TAG, "Service.onDestroy()");
mPlayer.stop();
}
}
5、ServiceTest/app/src/main/java/example/com/servicetest/MainActivity.java
package example.com.servicetest;
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.util.Log;
import android.view.View;
public class MainActivity extends Activity {
private static final String TAG = "xxxxxx";
Intent intent;
ServiceConnection conn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intent = new Intent(this, ServiceDemo.class);
conn = new ServiceConnection() {
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i(TAG, "onServiceConnected");
}
public void onServiceDisconnected(ComponentName name) {
Log.i(TAG, "onServiceDisconnected");
}
};
}
public void startMusicService(View v) {
Log.i(TAG, "startService");
startService(intent);
}
public void stopMusicService(View v) {
Log.i(TAG, "stopService");
stopService(intent);
}
public void bindMusicService(View v) {
Log.i(TAG, "bindService");
bindService(intent, conn, Context.BIND_AUTO_CREATE);
}
public void unBindMusicService(View v) {
Log.i(TAG, "unBindService");
unbindService(conn);
}
}
总结
方式1:startService(intent)
07-17 18:28:37.866 18449 18449 I xxxxxx : startService
07-17 18:28:37.929 18449 18449 I xxxxxx : Service.onCreate
07-17 18:28:37.930 18449 18449 I xxxxxx : Service.onStartCommand
07-17 18:28:37.930 18449 18449 I xxxxxx : Service.onStart
07-17 18:28:48.242 18449 18449 I xxxxxx : stopService
07-17 18:28:48.250 18449 18449 I xxxxxx : Service.onDestroy()
方式2:bindService()
07-17 18:29:36.782 18449 18449 I xxxxxx : bindService
07-17 18:29:36.866 18449 18449 I xxxxxx : Service.onCreate
07-17 18:29:36.867 18449 18449 E xxxxxx : Service.onBind()!
07-17 18:29:41.751 18449 18449 I xxxxxx : unBindService
07-17 18:29:41.758 18449 18449 E xxxxxx : Service.onUnbind()!
07-17 18:29:41.760 18449 18449 I xxxxxx : Service.onDestroy()
重写service方法,不许要super.onxxxx( )方法。
本文详细介绍了一种在Android应用中使用服务(Service)的方法,包括通过startService和bindService两种方式来启动服务。文中提供了从创建音乐文件到在AndroidManifest.xml中定义服务的具体步骤,以及MainActivity与ServiceDemo两个关键类的实现细节。
211

被折叠的 条评论
为什么被折叠?



