在后台长时间运行,没有界面UI, 在后台下载文件和获取用户GPS信息
// 需要在XML文件中配准Service Bound
<service
android:name=".MusicService"
android:enabled="true"
android:exported="true"></service>
public boolean isRunning() {
ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
ArrayList<ActivityManager.RunningServiceInfo> runningServiceInfoArrayList
= (ArrayList< ActivityManager.RunningServiceInfo>) activityManager.getRunningServices(10);
for(int i=0;i <runningServiceInfoArrayList.size();i++){
if(runningServiceInfoArrayList.get(i).service.getClassName().toString().equals("com.example.myapplication.MyService")) {
return true;
}
}
return false;
}
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageButton;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//设置全屏显示
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
ImageButton btn_play = (ImageButton) findViewById(R.id.btn_play);//获取“播放/停止”按钮
//启动服务与停止服务,实现播放背景音乐与停止播放背景音乐
btn_play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (MusicService.isPlay == false) { //判断音乐播放的状态
startService(new Intent(MainActivity.this, MusicService.class)); //启动服务,从而实现播放背景音乐
//更换播放背景音乐图标
((ImageButton) v).setImageDrawable(getResources().getDrawable(R.drawable.play, null));
} else {
stopService(new Intent(MainActivity.this, MusicService.class)); //停止服务,从而实现停止播放背景音乐
//更换停止背景音乐图标
((ImageButton) v).setImageDrawable(getResources().getDrawable(R.drawable.stop, null));
}
}
});
}
@Override
protected void onStart() { //实现进入界面时,启动背景音乐服务
startService(new Intent(MainActivity.this, MusicService.class)); //启动服务,从而实现播放背景音乐
super.onStart();
}
}
package com.example.myapplication;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
public class MusicService extends Service {
public MusicService() {
}
static boolean isPlay =false;
private MediaPlayer player;
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// 判断播放状态
if(player.isPlaying()) {
player.start();
isPlay = player.isPlaying();
}
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onCreate() {
// 创建MediaPlay对象,加载播放音频对象
player = MediaPlayer.create(this,R.raw.music);
super.onCreate();
}
@Override
public void onDestroy() {
player.stop();
isPlay = player.isPlaying();
player.release();
super.onDestroy();
}
}
注意 在Android 8.0 后不允许在后台创建Service 上面有问题 需要调整
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.List;
public class MainActivity extends AppCompatActivity {
BinderService binderService; //声明BinderService
//文本框组件ID
int[] tvid = {R.id.textView1, R.id.textView2, R.id.textView3, R.id.textView4, R.id.textView5,
R.id.textView6, R.id.textView7};
// 创建ServiceConnection 对象
private ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
binderService = ((BinderService.MyBinder) iBinder).getService();
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn_random = (Button) findViewById(R.id.btn); //获取随机选号按钮
btn_random.setOnClickListener(new View.OnClickListener() { //单击按钮,获取随机彩票号码
@Override
public void onClick(View v) {
List number = binderService.getRandomNumber(); //获取BinderService类中的随机数数组
for (int i = 0; i < number.size(); i++) { //遍历数组并显示
TextView tv = (TextView) findViewById(tvid[i]); //获取文本框组件对象
String strNumber = number.get(i).toString(); //将获取的号码转为String类型
tv.setText(strNumber); //显示生成的随机号码
}
}
});
}
@Override
protected void onStart() { //设置启动Activity时与后台Service进行绑定
super.onStart();
Intent intent = new Intent(this, BinderService.class); //创建启动Service的Intent
bindService(intent, serviceConnection, BIND_AUTO_CREATE); //绑定指定Service
}
@Override
protected void onStop() { //设置关闭Activity时解除与后台Service的绑定
super.onStop();
unbindService(serviceConnection); //解除绑定Service
}
}
package com.example.myapplication;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class BinderService extends Service {
public BinderService() {
}
// 创建MyBinder 内部类
public class MyBinder extends Binder {
public BinderService getService() {
return BinderService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
return new MyBinder();
}
public List getRandomNumber() { //创建获取随机号码的方法
List resArr = new ArrayList(); //创建ArrayList数组
String strNumber="";
for (int i = 0; i < 7; i++) { //将随机获取的数字转换为字符串添加到ArrayList数组中
int number = new Random().nextInt(33) + 1;
//把生成的随机数格式化为两位的字符串
if (number<10) { //在数字1~9前加0
strNumber = "0" + String.valueOf(number);
} else {
strNumber=String.valueOf(number);
}
resArr.add(strNumber);
}
return resArr; //将数组返回
}
@Override
public void onDestroy() { //销毁该Service
super.onDestroy();
}
}
·普通的Service 不能自动开启线程 和 自动停止服务