mirror of
https://gitea.winboll.cc/ZhanGSKen/MyWinBoLL.git
synced 2026-08-14 05:27:11 +08:00
更正服务类的属性和方法命名,明确服务分类。
This commit is contained in:
@@ -16,18 +16,18 @@ public class SettingsActivity extends Activity {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_settings);
|
||||
|
||||
Switch swGpsService = findViewById(R.id.sw_gps_service);
|
||||
swGpsService.setChecked(GpsReceiveService.isServiceRunning());
|
||||
swGpsService.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
|
||||
Switch swGpsReceiveService = findViewById(R.id.sw_gps_receive_service);
|
||||
swGpsReceiveService.setChecked(GpsReceiveService.isServiceRunning());
|
||||
swGpsReceiveService.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
|
||||
@Override
|
||||
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
|
||||
if (isChecked) {
|
||||
GpsReceiveService.startGpsService(SettingsActivity.this);
|
||||
GpsReceiveService.startGpsReceiveService(SettingsActivity.this);
|
||||
} else {
|
||||
GpsReceiveService.stopGpsService(SettingsActivity.this);
|
||||
GpsReceiveService.stopGpsReceiveService(SettingsActivity.this);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,11 +24,11 @@ public class GpsReceiveService extends Service {
|
||||
|
||||
public static final String TAG = "GpsReceiveService";
|
||||
private static final int NOTIFICATION_ID = 1001;
|
||||
private static final String CHANNEL_ID = "gps_service_channel";
|
||||
private static final String CHANNEL_ID = "gps_receive_service_channel";
|
||||
|
||||
private static boolean serviceRunning = false;
|
||||
private GpsDbManager dbManager;
|
||||
private BroadcastReceiver gpsBroadcastReceiver;
|
||||
private BroadcastReceiver gpsServiceBroadcastReceiver;
|
||||
|
||||
/**
|
||||
* 获取GPS定位服务运行状态
|
||||
@@ -41,27 +41,27 @@ public class GpsReceiveService extends Service {
|
||||
/**
|
||||
* 外部统一静态启动服务
|
||||
*/
|
||||
public static void startGpsService(final Context context) {
|
||||
public static void startGpsReceiveService(final Context context) {
|
||||
if (serviceRunning) {
|
||||
LogUtils.i("GpsReceiveService", "服务已运行,无需重复启动");
|
||||
return;
|
||||
}
|
||||
final Intent intent = new Intent(context, GpsReceiveService.class);
|
||||
context.startService(intent);
|
||||
LogUtils.i("GpsReceiveService", "GPS接收服务启动请求发送");
|
||||
LogUtils.i("GpsReceiveService", "GPS定位消息接收服务启动请求发送");
|
||||
}
|
||||
|
||||
/**
|
||||
* 外部统一静态关闭服务
|
||||
*/
|
||||
public static void stopGpsService(final Context context) {
|
||||
public static void stopGpsReceiveService(final Context context) {
|
||||
if (!serviceRunning) {
|
||||
LogUtils.i("GpsReceiveService", "服务未运行,无需关闭");
|
||||
return;
|
||||
}
|
||||
final Intent intent = new Intent(context, GpsReceiveService.class);
|
||||
context.stopService(intent);
|
||||
LogUtils.i("GpsReceiveService", "GPS接收服务停止请求发送");
|
||||
LogUtils.i("GpsReceiveService", "GPS定位消息接收服务停止请求发送");
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -69,35 +69,35 @@ public class GpsReceiveService extends Service {
|
||||
super.onCreate();
|
||||
serviceRunning = true;
|
||||
dbManager = new GpsDbManager(getApplicationContext());
|
||||
registerGpsBroadcast();
|
||||
registerGpsServiceBroadcast();
|
||||
startForeground(NOTIFICATION_ID, createNotification());
|
||||
LogUtils.i("GpsReceiveService", "GPS接收服务创建完成");
|
||||
Toast.makeText(this, "GPS定位服务已开启", Toast.LENGTH_SHORT).show();
|
||||
LogUtils.i("GpsReceiveService", "GPS定位消息接收服务创建完成");
|
||||
Toast.makeText(this, "GPS定位消息接收服务已开启", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
private Notification createNotification() {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
NotificationChannel channel = new NotificationChannel(
|
||||
CHANNEL_ID,
|
||||
"GPS定位服务",
|
||||
NotificationManager.IMPORTANCE_LOW
|
||||
CHANNEL_ID,
|
||||
"GPS定位消息接收服务",
|
||||
NotificationManager.IMPORTANCE_LOW
|
||||
);
|
||||
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
|
||||
manager.createNotificationChannel(channel);
|
||||
}
|
||||
return new Notification.Builder(this, CHANNEL_ID)
|
||||
.setContentTitle("ByTheWay")
|
||||
.setContentText("GPS定位服务运行中")
|
||||
.setSmallIcon(android.R.drawable.ic_menu_mylocation)
|
||||
.setOngoing(true)
|
||||
.build();
|
||||
.setContentTitle("ByTheWay")
|
||||
.setContentText("GPS定位消息接收服务运行中")
|
||||
.setSmallIcon(android.R.drawable.ic_menu_mylocation)
|
||||
.setOngoing(true)
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 动态注册GPS定位广播接收器
|
||||
*/
|
||||
private void registerGpsBroadcast() {
|
||||
gpsBroadcastReceiver = new BroadcastReceiver() {
|
||||
private void registerGpsServiceBroadcast() {
|
||||
gpsServiceBroadcastReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(final Context context, final Intent intent) {
|
||||
parseAndSaveGpsData(intent);
|
||||
@@ -106,8 +106,8 @@ public class GpsReceiveService extends Service {
|
||||
final IntentFilter filter = new IntentFilter();
|
||||
//GPS定位广播动作,与你定位发送广播对应
|
||||
filter.addAction("android.location.GPS_LOCATION_BROADCAST");
|
||||
registerReceiver(gpsBroadcastReceiver, filter);
|
||||
LogUtils.i("GpsReceiveService", "GPS广播动态注册成功");
|
||||
registerReceiver(gpsServiceBroadcastReceiver, filter);
|
||||
LogUtils.i("GpsServiceBroadcastReceiver", "GPS广播接收动态注册成功");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -149,12 +149,12 @@ public class GpsReceiveService extends Service {
|
||||
stopForeground(true);
|
||||
serviceRunning = false;
|
||||
//反注册广播,防止内存泄漏
|
||||
if (gpsBroadcastReceiver != null) {
|
||||
unregisterReceiver(gpsBroadcastReceiver);
|
||||
if (gpsServiceBroadcastReceiver != null) {
|
||||
unregisterReceiver(gpsServiceBroadcastReceiver);
|
||||
}
|
||||
LogUtils.i("GpsReceiveService", "GPS接收服务已销毁,广播已解绑");
|
||||
Toast.makeText(this, "GPS定位服务已关闭", Toast.LENGTH_SHORT).show();
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -7,10 +7,10 @@
|
||||
android:padding="16dp">
|
||||
|
||||
<Switch
|
||||
android:id="@+id/sw_gps_service"
|
||||
android:id="@+id/sw_gps_receive_service"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="GPS定位服务"/>
|
||||
android:text="GPS定位消息接收服务"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user