Service的学习代码

1. startService(new Intent(MainActivity.this, MyService.class))------->stopService(new Intent(MainActivity.this, MyService.class))

2.bindService(new Intent(MainActivity.this, MyService.class), mServiceConnection,Context.BIND_AUTO_CREATE)---->

if(mServiceConnection != null){unbindService(mServiceConnection);mServiceConnection = null;
}----->一般放在onDestory()函数里面

MainActivity.java

private MyService myService;

private ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
Log.i("jxy",this.getClass().getName() + "--->onServiceConnected");
myService = ((MyService.MyBinder)iBinder).getService();
}

@Override
public void onServiceDisconnected(ComponentName componentName) {
Log.i("jxy",this.getClass().getName() + "--->onServiceDisconnected");
}
};

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

@Override
protected void onDestroy() {
super.onDestroy();
if(mServiceConnection != null){
unbindService(mServiceConnection);
mServiceConnection = null;
}
}

public void startService(View view) {
startService(new Intent(MainActivity.this, MyService.class));
}

public void stopService(View view) {
stopService(new Intent(MainActivity.this, MyService.class));
}

public void bindService(View view) {
bindService(new Intent(MainActivity.this, MyService.class), mServiceConnection,
Context.BIND_AUTO_CREATE);
}

public void mp3player(View view){
myService.mp3Play((String) view.getTag());
}

MyService.java
class MyBinder extends Binder{
public MyService getService(){
return MyService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
// Log.i("jxy",this.getClass().getName() + "--->onBind");
// Binder binder = new Binder();
// Log.i("jxy",this.getClass().getName() + "--->binder:" + binder);
return new MyBinder();
}

@Override
public boolean onUnbind(Intent intent) {
Log.i("jxy",this.getClass().getName() + "--->onUnbind");
return super.onUnbind(intent);
}

@Override
public void onCreate() {
super.onCreate();
Log.i("jxy",Thread.currentThread().getName() + ":MyService --> onCreate");
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("jxy",Thread.currentThread().getName() + "MyService --> onStartCommand");
return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy() {
super.onDestroy();
Log.i("jxy",Thread.currentThread().getName() + "MyService --> onDestroy");
}

// 此方法完成MP3播放的功能
public void mp3Play(String name){
Log.i("jxy",this.getClass().getName() + "--->正在播放的歌曲名称为:" + name);
}

activity.xml
<LinearLayout 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"
android:orientation="vertical"
android:padding="16dp">

<Button
android:onClick="startService"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="启动服务" />

<Button
android:onClick="stopService"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="停止服务" />
<Button
android:onClick="bindService"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="绑定服务" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="解绑服务" />

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="调用系统服务挂断电话" />

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="测试黑名单拦截(调用系统服务)" />

<Button
android:tag="相信自己"
android:onClick="mp3player"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="音乐播放测试" />

</LinearLayout>

Manifest.xml文件的service的注册
<service android:name=".MyService" />




原文地址:https://www.cnblogs.com/liunx1109/p/9901326.html