Android的Service

原来总感觉四大组建Service和Broadcaster Receiver用的少,其实不然,只是因为比较抽象,他们总是默默无闻的在后台进行服务

写了个demo测试了一下,一个是生命周期,一个是绑定服务,解绑服务,感觉这个用mp3播放比较形象

下面是工程列表,感觉在顺序上不只是Eclipse还是google调整了一下

技术分享

MainActivity.class

package cn.edu.sjzc.fanyafeng.activity;


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.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import cn.edu.sjzc.fanyafeng.service.MyService;
import cn.edu.sjzc.fanyafeng.servicedemo.R;


public class MainActivity extends Activity implements OnClickListener {


private Button start, end, bind, unbind;


private static String TAG = "生命周期";


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


Log.i(TAG, "onCreate方法执行");


initView();
}


private void initView() {
this.start = (Button) MainActivity.this.findViewById(R.id.start);
this.start.setOnClickListener(this);


this.end = (Button) MainActivity.this.findViewById(R.id.end);
this.end.setOnClickListener(this);


this.bind = (Button) MainActivity.this.findViewById(R.id.bind);
this.bind.setOnClickListener(this);


this.unbind = (Button) MainActivity.this.findViewById(R.id.unbind);
this.unbind.setOnClickListener(this);


}


@Override
public void onClick(View v) {
// TODO Auto-generated method stub


Intent intent = new Intent(MainActivity.this,MyService.class);

switch (v.getId()) {
case R.id.start:


startService(intent);

break;
case R.id.end:

stopService(intent);


break;
case R.id.bind:


bindService(intent, conn, BIND_AUTO_CREATE);

break;
case R.id.unbind:


unbindService(conn);

break;


default:
break;
}


}

final ServiceConnection conn = 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

}
};
}


MyService.java

package cn.edu.sjzc.fanyafeng.service;


import cn.edu.sjzc.fanyafeng.servicedemo.R;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;


public class MyService extends Service {
// 定义输入日志
private static String TAG = "生命周期";
// 定义音乐播放变量
private MediaPlayer mPlayer;

private MyBinder mBinder = new MyBinder();


@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
Log.i(TAG, "onBind()执行");
// mPlayer.start();
return mBinder;
}


@Override
public void onCreate() {
// TODO Auto-generated method stub
Log.i(TAG, "onCreate()执行");//第一个执行
//R.raw.xxxx其中raw不能换成其他的名称,不然Android识别不出来,囧
mPlayer = MediaPlayer.create(getApplicationContext(), R.raw.qimiaozhongdejiyi);
//设置音乐循环播放,这个大家可以想一下Handler其中的Looper
mPlayer.setLooping(true);
super.onCreate();
}




@Override
public void onDestroy() {
// TODO Auto-generated method stub
Log.i(TAG, "onDestroy()执行");//停止播放时执行
mPlayer.stop();
super.onDestroy();
}




@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Log.i(TAG, "onStartCommand()执行");//第二个执行
mPlayer.start();
return super.onStartCommand(intent, flags, startId);
}





@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
Log.i(TAG, "onUnbind()执行");
return super.onUnbind(intent);
}


@SuppressWarnings("deprecation")
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
//onStart方法被谷歌打上了横杠,不建议大家使用
Log.i(TAG, "onStart()执行");//第三个执行
//start()方法放在onStartCommand
// mPlayer.start();
super.onStart(intent, startId);
}

public class MyBinder extends Binder{
MyService getService(){
return MyService.this;
}
}


}


xml文件

<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="${relativePackage}.${activityClass}" >


    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />


    <Button
        android:id="@+id/unbind"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/bind"
        android:layout_alignBottom="@+id/bind"
        android:layout_alignParentRight="true"
        android:layout_marginRight="88dp"
        android:text="解绑" />


    <Button
        android:id="@+id/start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_toLeftOf="@+id/unbind"
        android:text="开启音乐播放" />


    <Button
        android:id="@+id/bind"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/start"
        android:layout_below="@+id/end"
        android:layout_marginTop="22dp"
        android:text="绑定Service" />


    <Button
        android:id="@+id/end"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/start"
        android:layout_below="@+id/start"
        android:layout_marginTop="16dp"
        android:text="停止音乐播放服务" />


</RelativeLayout>


最重要的不要忘记注册,Android的四大组建都需要注册

以下是demo的运行技术分享

技术分享


这样程序就可以后台运行了


下面是程序日志帮助大家理解生命周期技术分享


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。