Android Service的两种方式

时间:2022-02-18 16:47:17

Android Service提供了两种启动方式供我们选择。

第一种是startService方式启动,这种方式就只是Activity中的任意控件用启动了Service,Activity与Service是没有什么关系的,这是仅仅的Activity通知Service你该启动了的意思。

第二种是bindService方式启动,这种方式表示Activity与Service中建议起了练习,Activity可以从Service中拿到需要的消息。

两种方式启动需要经历一下的阶段。Android Service的两种方式

  要启动服务首先要创建一个类继承Service

<span style="font-size:18px;">public class MyService extends Service {
public static final String TAG = "MyService";
private int i;
private boolean is_flag = false;
@Override
public void onCreate() {
super.onCreate();
new Thread(new Runnable() {
@Override
public void run() {
while (!is_flag){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
i++;
Log.d(TAG, "i="+i);
}
}
}).start();
Log.d(TAG, "onCreate方法被调用");
}
@Override
public void onStart(Intent intent, int startId) {
Log.d(TAG, "Onstart方法被调用");
super.onStart(intent, startId);
}
@Override
public void onDestroy() {
Log.d(TAG, "onDestroy方法被调用");
is_flag = true;
super.onDestroy();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.d(TAG, "onBind方法执行");
return null;
}
}
</span>
从上面的图片可以看出来,startService启动只要经过 onCreat-->onStart-->onDestroy 三个步骤就可以了,放了观察方便,我在里面加入了一个变量i表示服务在运行,我让这些在打印台输出。Android Service的两种方式

第二种启动方式要创建一个内部类,继承Binder,代码如下

package com.daizhihao.demo_1;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

/**
* 创建一个MyService的类继承Service
* Created by Administrator on 2016/3/13.
*/
public class MyService extends Service {
public static final String TAG = "MyService";
private MyBinder myBinder = new MyBinder();
private int i;
private boolean is_flag = false;
@Override
public void onDestroy() {
Log.d(TAG, "onDestroy方法被调用");
is_flag = true;
super.onDestroy();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.d(TAG, "onBind方法执行");
return null;
}
@Override
public boolean onUnbind(Intent intent) {
Log.d(TAG, "onUnbind方法被执行");
return super.onUnbind(intent);
}

class MyBinder extends Binder{
MyBinder myBinder() {
Log.d(TAG, "myBinder方法执行");
return myBinder;
}
}
}
这种启动方式是要经过onCreat-->onBind-->onUnbind-->onDestroy就可以了,在控制台的打印如下:

Android Service的两种方式

Activity的代码只要用4个按钮一次启动各个服务就可以了

package com.daizhihao.demo_1;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
NotificationManager notificationManager = null;
private Button startService, stopService, bindService, unBindeService;

private void assignViews() {
startService = (Button) findViewById(R.id.startService);
stopService = (Button) findViewById(R.id.stopService);
bindService = (Button) findViewById(R.id.bindService);
unBindeService = (Button) findViewById(R.id.unBindService);
}

private ServiceConnection connection;

{
connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d("MainActivity", "onServiceConnected方法执行");
}

@Override
public void onServiceDisconnected(ComponentName name) {
Log.d("MainActivity", "onServiceDisconnected方法执行");
}
};
}

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

private void start() {
startService.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, MyService.class);
startService(intent);
Intent i = new Intent(MainActivity.this, Notification.class);
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(MainActivity.this);
mBuilder.setTicker("提示");
mBuilder.setContentTitle("标题");
mBuilder.setContentText("我是通过点击Notification得到的,点击查看");
mBuilder.setContentIntent(pendingIntent);
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, mBuilder.build());
Log.d("MainActivity", "notification方法启动");
}
});
stopService.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent stopIntent = new Intent(MainActivity.this, MyService.class);
stopService(stopIntent);
notificationManager.cancelAll();
}
});
bindService.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent bindIntent = new Intent(MainActivity.this, MyService.class);
bindService(bindIntent, connection, BIND_AUTO_CREATE);
}
});
unBindeService.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
unbindService(connection);
}
});
}
}