I made a service called Watcher run through startService when the main activity of the application ends through onDestroy. This service has a Bluetooth connection code and an infinite loop, such as the following code: The app also has a code that connects Bluetooth and receives data like a service. But it's okay when you don't run this service, but if you run the app while the service is running in the background, only a white screen appears and ANR occurs. Although the stopService command was written on the first line of the app's onCreate, the service does not shut down and ANR occurs. How can I shut down the service when the app is running?
// Some code for the service
public int onStartCommand(Intent intent, int flags, int startId) {
//region Bluetooth connection
bt = intent.getStringExtra("command");
Log.d("bt", bt);
try{
Log.i("bt", "Start connection! The value to connect is " + bt + "";
if(mSocket == null || !isBtConnected){
mBluetooth = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice btDev = mBluetooth.getRemoteDevice(bt);
mSocket = btDev.createInsecureRfcommSocketToServiceRecord(mUUID);
BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
mSocket.connect();
Toast.makeText(getApplicationContext(), "Connect succeeded", Toast.LENGTH_LONG).show();
Log.i ("bt", "Success");
}
}
catch(IOException e){
// FIXME: 2017-08-26 Displays a dialog when a Bluetooth connection fails.
Toast.makeText(getApplicationContext(), "ERR004, Bluetooth connection failed," Toast.LENGTH_LONG).show();
final AlertDialog.Builder builder = new AlertDialog.Builder(this, AlertDialog.THEME_HOLO_LIGHT);
builder.setTitle ("Do you want to start setting up?")
.setMessage("Bluetooth connection failed successfully. Will you shut down the app and restart it later?" +
"Or choose whether you want to re-establish the connection now. " +
"If you exit the app, you won't be notified until you turn it back on."))
.setNegativeButton("setup", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent settingIntent = new Intent(getApplicationContext(), SettingActivity.class);
startActivity(settingIntent);
}
})
.setPositiveButton("End App", newDialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// // TODO: 2017-08-27
}
}).show();
}
/////////////
//endregion
for(;;){
receiveNotify();
if(false){
break;
}
}
return super.onStartCommand(intent, flags, startId);
}
Android's services are components that run on the main (primary) thread. Therefore, running an infinite loop here will inevitably result in ANR. And even if stopService() is called, stopService() also does not work when the main thread is occupied by that loop.
Use IntentService as an alternative or write code so that you do not block main threads by creating new threads from existing code. When the operation is complete, be sure to include the process of closing the loop.
Please refer to the following link for the description and sample code related to the Intent Service.
© 2024 OneMinuteCode. All rights reserved.