How to release(gc) bluetoothgatt instance? - android

In my project, I called the mCmdBinder.gattConnect() and mCmdBinder.gattClose() method multiple times and it generated multiple BluetoothGatt instances. In the dumped file .hprof, I can see there are multiple instances of the BluetoothGatt object exist. Even when I run initiate gc command, these instances are not cleaned. Why these instances can not be released?
MyGattService.java
public class MyGattService extends Service {
private BluetoothAdapter mAdapt;
private BluetoothDevice mDevice;
private BluetoothGatt mGatt;
#Override
public void onCreate() {
super.onCreate();
Log.e("mLog", "service oncreate !");
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
#Override
public void onDestroy() {
super.onDestroy();
Log.e("mLog", "service ondestroy()!");
}
#Override
public IBinder onBind(Intent intent) {
return new CmdBinder();
}
//发送各种命令
public class CmdBinder extends Binder {
public void gattClose() {
if (mGatt != null) {
mAdapt = null;
mDevice = null;
mGatt.close();
mGatt = null;
Log.e("mLog", "gatt close! mGatt=" + mGatt);
}
}
public void gattConnect() {
mAdapt = BluetoothAdapter.getDefaultAdapter();
mDevice = mAdapt.getRemoteDevice("F4:04:4C:0C:81:1B");
Log.e("mLog", "device bind status:" + mDevice.getBondState());
mGatt = mDevice.connectGatt(MyGattService.this, true, new BluetoothGattCallback() {
#Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
Log.e("mlog", "status:" + status + "; newState:" + newState);
if (newState == BluetoothProfile.STATE_CONNECTED) {
Log.e("mLog", " go discover services!");
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
Log.e("mLog", "mGatt=" + mGatt);
}
}
});
Log.e("mLog", "gatt connect!");
}
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
/**
* 蓝牙处理放在service中
*/
private MyGattService.CmdBinder mCmdBinder;
private MyGattServiceConnection conn;
public class MyGattServiceConnection implements ServiceConnection {
public void onServiceConnected(ComponentName name, IBinder service) {
mCmdBinder = (MyGattService.CmdBinder) service;
Log.e("mLog", "service connected!");
}
public void onServiceDisconnected(ComponentName name) {
Log.e("mLog", "service disconnected!");
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
Intent gattService = new Intent(MainActivity.this, MyGattService.class);
conn = new MyGattServiceConnection();
bindService(gattService, conn, Context.BIND_AUTO_CREATE);
}
#Override
protected void onDestroy() {
super.onDestroy();
unbindService(conn);
}
public void initView() {
TextView retxt = (TextView) findViewById(R.id.txt_reconnect);
retxt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mCmdBinder.gattConnect();
}
});
TextView close = (TextView) findViewById(R.id.txtclose);
close.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mCmdBinder.gattClose();
}
});
}
}

In my project, I called the mCmdBinder.gattConnect() and mCmdBinder.gattClose() method multiple times and it generated multiple BluetoothGatt instances. In the dumped file .hprof, I can see there are multiple instances of the BluetoothGatt object exist. Even when I run initiate gc command, these instances are not cleaned. Why these instances can not be released?

Related

Why MainActivity is declared static inside other Class?

I followed a tutorial on Bluetooth Low Energy, and I have started to add my own menus, etc.
The question I have is, why would MainActivity be static and put inside another class.
The full tutorial is available on Github: https://github.com/kaviles/BLE_Tutorials
Below is the Scanner_BTLE class that contains static MainActivity.
public class Scanner_BTLE {
private MainActivity ma;
private BluetoothAdapter mBluetoothAdapter;
private boolean mScanning;
private Handler mHandler;
private long scanPeriod;
private int signalStrength;
private UUID[] uuids;
// ToDO add Runtime Permissions to activate it momentarily once user asks for scanning
// ToDo this is currently done in App permitions while in dedug mode, add the software in the code so //permisiions does not have to be done on phone
// ToDo see "https://andela.com/insights/how-to-scan-for-android-bluetooth-low-energy-devices-successfully/"
// ToDO Look at "B- or add Runtime Permissions..... " on how to add it in code
#RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
public Scanner_BTLE(Scanner_BTLE.MainActivity mainActivity, long scanPeriod, int signalStrength) {
ma = mainActivity;
mHandler = new Handler();
this.scanPeriod = scanPeriod;
this.signalStrength = signalStrength;
final BluetoothManager bluetoothManager =
(BluetoothManager) ma.getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
}
public boolean isScanning() {
return mScanning;
}
#RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
public void start() {
if (!Utils.checkBluetooth(mBluetoothAdapter)) {
Utils.requestUserBluetooth(ma);
ma.stopScan();
}
else {
scanLeDevice(true);
}
}
#RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
public void stop() {
scanLeDevice(false);
}
//ToDo Use the Callback to find only the sps UUID of Relay
// If you want to scan for only specific types of peripherals,
// you can instead call startLeScan(UUID[], BluetoothAdapter.LeScanCallback),
// providing an array of UUID objects that specify the GATT services your app supports.
#RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
private void scanLeDevice(final boolean enable) {
if (enable && !mScanning) {
Utils.toast(ma.getApplicationContext(), "Starting BLE scan...");
// Stops scanning after a pre-defined scan period.
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
Utils.toast(ma.getApplicationContext(), "Stopping BLE scan...");
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
ma.stopScan();
}
}, scanPeriod);
mScanning = true;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
mBluetoothAdapter.startLeScan(mLeScanCallback);
}
}
else {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
}
private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
#Override
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
final int new_rssi = rssi;
if (rssi > signalStrength) {
mHandler.post(new Runnable() {
#Override
public void run() {
ma.addDevice(device, new_rssi);
}
});
}
}
};
public static class MainActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemClickListener {
private final static String TAG = MainActivity.class.getSimpleName();
public static final int REQUEST_ENABLE_BT = 1;
public static final int BTLE_SERVICES = 2;
private HashMap<String, BTLE_Device> mBTDevicesHashMap;
private ArrayList<BTLE_Device> mBTDevicesArrayList;
private ListAdapter_BTLE_Devices adapter;
private ListView listView;
private Button btn_Scan;
private BroadcastReceiver_BTState mBTStateUpdateReceiver;
private Scanner_BTLE mBTLeScanner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
Utils.toast(getApplicationContext(), "BLE not supported");
finish();
}
mBTStateUpdateReceiver = new BroadcastReceiver_BTState(getApplicationContext());
mBTLeScanner = new Scanner_BTLE(this, 5000, -75);
mBTDevicesHashMap = new HashMap<>();
mBTDevicesArrayList = new ArrayList<>();
adapter = new ListAdapter_BTLE_Devices(this, R.layout.btle_device_list_item, mBTDevicesArrayList);
listView = new ListView(this);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
btn_Scan = (Button) findViewById(R.id.btn_scan);
((ScrollView) findViewById(R.id.scrollView)).addView(listView);
findViewById(R.id.btn_scan).setOnClickListener(this);
}
#Override
protected void onStart() {
super.onStart();
registerReceiver(mBTStateUpdateReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
}
#Override
protected void onResume() {
super.onResume();
// registerReceiver(mBTStateUpdateReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
}
#Override
protected void onPause() {
super.onPause();
// unregisterReceiver(mBTStateUpdateReceiver);
stopScan();
}
#Override
protected void onStop() {
super.onStop();
unregisterReceiver(mBTStateUpdateReceiver);
stopScan();
}
#Override
public void onDestroy() {
super.onDestroy();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == REQUEST_ENABLE_BT) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
// Utils.toast(getApplicationContext(), "Thank you for turning on Bluetooth");
}
else if (resultCode == RESULT_CANCELED) {
Utils.toast(getApplicationContext(), "Please turn on Bluetooth");
}
}
else if (requestCode == BTLE_SERVICES) {
// Do something
}
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Context context = view.getContext();
// Utils.toast(context, "List Item clicked");
// do something with the text views and start the next activity.
stopScan();
String name = mBTDevicesArrayList.get(position).getName();
String address = mBTDevicesArrayList.get(position).getAddress();
Intent intent = new Intent(this, Activity_BTLE_Services.class);
intent.putExtra(Activity_BTLE_Services.EXTRA_NAME, name);
intent.putExtra(Activity_BTLE_Services.EXTRA_ADDRESS, address);
startActivityForResult(intent, BTLE_SERVICES);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_scan:
Utils.toast(getApplicationContext(), "Scan Button Pressed");
if (!mBTLeScanner.isScanning()) {
startScan();
}
else {
stopScan();
}
break;
default:
break;
}
}
public void addDevice(BluetoothDevice device, int rssi) {
String address = device.getAddress();
if (!mBTDevicesHashMap.containsKey(address)) {
BTLE_Device btleDevice = new BTLE_Device(device);
btleDevice.setRSSI(rssi);
mBTDevicesHashMap.put(address, btleDevice);
mBTDevicesArrayList.add(btleDevice);
}
else {
mBTDevicesHashMap.get(address).setRSSI(rssi);
}
adapter.notifyDataSetChanged();
}
public void startScan(){
btn_Scan.setText("Scanning...");
mBTDevicesArrayList.clear();
mBTDevicesHashMap.clear();
mBTLeScanner.start();
}
public void stopScan() {
btn_Scan.setText("Scan Again");
mBTLeScanner.stop();
}
}
}

Run Android Service every 30 seconds

I am creating a Notification using Android Service independent from UI. This works perfectly fine. Below is the code.
public class SendNotificationService extends Service {
Context context;
String test_heading;
String test_body;
final class notifThread implements Runnable {
int service_id;
notifThread(int service_id) {
this.service_id = service_id;
}
#Override
public void run() {
String requested_method = "LoadBU";
String bu_status = "1";
CheckNewEntry checkNewEntry = new CheckNewEntry(SendNotificationService.this);
checkNewEntry.execute(requested_method, bu_status);
stopSelf(this.service_id);
}
}
#Override
public void onCreate() {
super.onCreate();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Thread thread = new Thread(new notifThread(startId));
thread.start();
return START_STICKY;
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Notifications Stopped...", Toast.LENGTH_LONG).show();
}
}
This service also starts automatically on system boot. CheckNewEntry is my AsyncTask that checks the database and sends notification if there is any change. I have not added CheckNewEntry as it is beyond the scope of this question.
Now what I want to do is, run CheckNewEntry every 30 seconds or 1 minute.
Can anyone help?
After going through different Stackoverflow questions/answers, I managed to come up with my own solution.
Below is the code that I have created and is working now.
public class SendNotificationService extends Service {
public Context context = this;
public Handler handler = null;
public static Runnable runnable = null;
#Override
public void onCreate() {
handler = new Handler();
runnable = new Runnable() {
public void run() {
String requested_method = "LoadBU";
String bu_status = "1";
CheckNewEntry checkNewEntry = new CheckNewEntry(SendNotificationService.this);
checkNewEntry.execute(requested_method, bu_status);
handler.postDelayed(runnable, 10000);
}
};
handler.postDelayed(runnable, 15000);
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onDestroy() {
handler.removeCallbacks(runnable);
Toast.makeText(this, "Notifications Stopped...", Toast.LENGTH_LONG).show();
}
}
If anyone of you can provide a better solution, please do post.
you can use handler like this.
public class SendNotificationService extends Service {
Context context;
String test_heading;
String test_body;
public static Runnable runn;
public static Handler hand =new Handler();
final class notifThread implements Runnable {
int service_id;
notifThread(int service_id) {
this.service_id = service_id;
}
#Override
public void run() {
String requested_method = "LoadBU";
String bu_status = "1";
CheckNewEntry checkNewEntry = new CheckNewEntry(SendNotificationService.this);
runn = new Runnable() {
#Override
public void run() {
checkNewEntry.execute(requested_method, bu_status);
hand.postDelayed(runn, 30000);
}
};
runn.run();
stopSelf(this.service_id);
}
}
#Override
public void onCreate() {
super.onCreate();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Thread thread = new Thread(new notifThread(startId));
thread.start();
return START_STICKY;
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Notifications Stopped...", Toast.LENGTH_LONG).show();
}
}

Altbeacon library with android version 6

I'm updating the altbeacon library to the latest 2.9.1 but I don't get any beacon when I range for it, this is using android 6.0.1.
public class BeaconService extends IntentService implements BeaconConsumer {
private BeaconManager mBeaconManager;
private static ArrayList<Beacon> beaconsList=new ArrayList<Beacon>();
private Region region=new Region("rid", null, null, null);
private static final String LOGTAG = "BeaconService";
/**
* Creates an IntentService. Invoked by your subclass's constructor.
*/
public BeaconService() {
super(Constants.BEACON_SERVICE);
}
#Override
public void onBeaconServiceConnect() {
try {
mBeaconManager.startRangingBeaconsInRegion(region);
mBeaconManager.setRangeNotifier(new RangeNotifier() {
#Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
Intent localIntent =new Intent(Constants.BEACON_ACTION);
beaconsList.clear();
beaconsList.addAll(beacons);
Collections.sort(beaconsList,new Comparator<Beacon>() {
#Override
public int compare(Beacon lhs, Beacon rhs) {
return Double.compare(lhs.getDistance(), rhs.getDistance());
}
});
localIntent.putParcelableArrayListExtra(Constants.BEACON_LIST,beaconsList);
LocalBroadcastManager.getInstance(BeaconService.this).sendBroadcast(localIntent);
}
});
} catch (RemoteException e) {
Log.e(LOGTAG,"Error BeaconService",e);
}
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return Service.START_NOT_STICKY;
}
#Override
public void onCreate() {
super.onCreate();
mBeaconManager = BeaconManager.getInstanceForApplication(this);
mBeaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
mBeaconManager.bind(this);
}
#Override
public void onDestroy() {
super.onDestroy();
mBeaconManager.unbind(this);
}
/**
* Stop Scanning
*/
public void stopRanging(){
try {
mBeaconManager.stopRangingBeaconsInRegion(region);
} catch (RemoteException e) {
Log.e(LOGTAG,"Error BeaconService - StopRanging",e);
}
}
/**
* Start Scanning
*/
public void startRanging(){
try {
mBeaconManager.startRangingBeaconsInRegion(region);
} catch (RemoteException e) {
Log.e(LOGTAG,"Error BeaconService - StartRanging",e);
}
}
#Override
public IBinder onBind(Intent intent) {
return new LocalBinder();
}
#Override
protected void onHandleIntent(Intent intent) {
}
public class LocalBinder extends Binder {
public BeaconService getService() {
return BeaconService.this;
}
}
I did try changing to monitoring and the same result, also I did try adding more layouts but I don't get any beacons on the list
looks like you need to add the permission at runtime, I did fix it by doing this
#Override
public void initialize(final CordovaInterface cordova, CordovaWebView webView) {
Log.i(LOGTAG, "initialize");
context = webView.getContext();
beaconServiceIntent = new Intent(context, BeaconService.class);
context.bindService(beaconServiceIntent, serviceBeaconConnection, Service.BIND_AUTO_CREATE);
BeaconReceiver beaconReciever = new BeaconReceiver();
IntentFilter intentFilter = new IntentFilter(Constants.BEACON_ACTION);
LocalBroadcastManager.getInstance(context).registerReceiver(beaconReciever, intentFilter);
mainActiviy = (Activity) context;
checkPermission();
}
#TargetApi(Build.VERSION_CODES.M)
private void checkPermission() {
if(this.mainActiviy.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED){
AlertDialog.Builder builder = new AlertDialog.Builder(((Activity)context));
builder.setTitle("This app needs location access");
builder.setMessage("Please grant location access so this app can detect beacons.");
builder.setPositiveButton(android.R.string.ok, null);
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
mainActiviy.requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},PERMISSION_REQUEST_COARSE_LOCATION);
}
});
builder.show();
}
}

onReadyForSpeech calling multiple times while continuous Speech Recognition

I am creating an application in that i need continuous speech recognition. But onReadyForSpeech calling two times.
I am also attaching my code. Please help me to find out the problem.
Thanks in advance.
private SpeechRecognizer mSpeechRecognizer = null;
public static VoiceRecognizeService sVoiceRecognizeService;
private ITelephony mListener;
private boolean isListening;
private Intent mSaverController;
public VoiceRecognizeService() {
super();
}
#Override
public void onCreate() {
super.onCreate();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
sVoiceRecognizeService = this;
startListening();
return START_NOT_STICKY;
}
public void setTelephonyListener(ITelephony mListener) {
this.mListener = mListener;
}
public static VoiceRecognizeService getInstance() {
return sVoiceRecognizeService;
}
// starts the service
public void startListening() {
if (!isListening) {
mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
mSpeechRecognizer.setRecognitionListener(this);
Intent mRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
mRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
mRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en_IN");
mRecognizerIntent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS, 2000);
mRecognizerIntent.putExtra("android.speech.extra.DICTATION_MODE", true);
mRecognizerIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, false);
mRecognizerIntent.putExtra("android.speech.extra.PREFER_OFFLINE", true);
mRecognizerIntent.putExtra("calling_package", this.getPackageName());
mSpeechRecognizer.startListening(mRecognizerIntent);
isListening = true;
}
}
public void processVoiceCommands(final ArrayList<String> partialData) {
}
public void restartListeningService() {
cancelSpeechRecognition();
startListening();
}
public void cancelSpeechRecognition() {
if (mSpeechRecognizer != null) {
mSpeechRecognizer.stopListening();
mSpeechRecognizer.cancel();
mSpeechRecognizer.destroy();
mSpeechRecognizer = null;
isListening = false;
}
}
#Override
public void onReadyForSpeech(Bundle bundle) {
Log.e("VoiceError", "speechReady");
}
#Override
public void onBeginningOfSpeech() {
}
#Override
public void onRmsChanged(float scale) {
if (mListener != null) {
mListener.onRmsChanged(scale);
}
}
#Override
public void onBufferReceived(byte[] bytes) {
}
#Override
public void onEndOfSpeech() {
}
#Override
public void onError(int i) {
if (i == SpeechRecognizer.ERROR_RECOGNIZER_BUSY) {
} else {
restartListeningService();
}
}
#Override
public void onResults(Bundle bundle) {
final ArrayList<String> data = bundle.getStringArrayList(
SpeechRecognizer.RESULTS_RECOGNITION);
if (data != null) {
processVoiceCommands(data);
}
restartListeningService();
}
#Override
public void onPartialResults(Bundle bundle) {
final ArrayList<String> data = bundle.getStringArrayList(
SpeechRecognizer.RESULTS_RECOGNITION);
Log.e("VoiceError", "partialResults " + data);
}
#Override
public void onEvent(int i, Bundle bundle) {
}
#Override
public void onDestroy() {
if (mSpeechRecognizer != null) {
mSpeechRecognizer.setRecognitionListener(null);
cancelSpeechRecognition();
}
super.onDestroy();
}
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}

android - how can I stop the thread inside the service?

I have a checked button in my MainActivity. If that button is checked it should start the service but if a user unchecked the button I want to stop the service.
So in uncheck condition I have written this stopService(intentname); but the problem is the service is not stopping. Here is my code snippet:
Service Class
public class SimpleService extends Service
{
String selectedAudioPath = "";
private MyThread myythread;
public Intent intent;
public boolean isRunning = false;
long interval=30000;
#Override
public IBinder onBind(Intent arg0)
{
return null;
}
#Override
public void onCreate()
{
super.onCreate();
myythread = new MyThread(interval);
}
#Override
public synchronized void onDestroy()
{
super.onDestroy();
if(!isRunning)
{
myythread.interrupt();
myythread.stop();
isRunning = false;
}
}
#Override
public synchronized void onStart(Intent intent, int startId)
{
super.onStart(intent, startId);
if(!isRunning)
{
//this.intent = intent;
//System.out.println("the intent is" + intent);
myythread.start();
isRunning = true;
}
}
class MyThread extends Thread
{
long interval;
public MyThread(long interval)
{
this.interval=interval;
}
#Override
public void run()
{
while(isRunning)
{
System.out.println("Service running");
try
{
String myString = intent.getStringExtra("name");
if(myString == null)
Log.d("Service","null");
else
{
Log.d("Service","not null");
if(myString.equalsIgnoreCase("image"))
{
uploadImages();
Thread.sleep(interval);
}
else if(myString.equalsIgnoreCase("audio"))
{
uploadAudio();
Thread.sleep(interval);
}
}
}
catch (InterruptedException e)
{
isRunning = false;
e.printStackTrace();
}
}
}
You can't stop a thread that has a running unstoppable loop like this
while(true)
{
}
To stop that thread, declare a boolean variable and use it in while-loop condition.
public class MyService extends Service {
...
private Thread mythread;
private boolean running;
#Override
public void onDestroy()
{
running = false;
super.onDestroy();
}
#Override
public void onStart(Intent intent, int startid) {
running = true;
mythread = new Thread() {
#Override
public void run() {
while(running) {
MY CODE TO RUN;
}
}
};
};
mythread.start();
}
Source: Stopping a thread inside a service
Don't use Threads. Use AsyncTask instead.
public class MyService extends Service {
private AsyncTask<Void,Void,Void> myTask;
#Override
public void onDestroy(){
super.onDestroy();
myTask.cancel(true);
}
#Override
public void onStart(Intent intent, int startid) {
myTask = new AsyncTask<Void,Void,Void>(){
#Override
public void doInBackground(Void aVoid[]){
doYourWorkHere();
}
}
myTask.execute();
}
}

Categories

Resources