Service Demo和总结

参考文档: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( )方法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值