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();
}
}
Related
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?
I have to perform some task continuously in MyService.java class. For this I use Handler but mHandler.postDelayed(this, 40000); is not getting fired after given time. It is getting fired after every second. Please help me out
public class SendMessageService extends Service {
private Handler mHandler = new Handler();
private Runnable task;
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
registerReceiver(stopReceiver, new IntentFilter("com.android.STOP_HANDLER"));
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
task = new Runnable() {
#Override
public void run() {
dosomething();
mHandler.postDelayed(this, 40000);
}
};
try {
mHandler.postDelayed(task, 40000);
} catch (Exception e) {
e.printStackTrace();
}
return START_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(stopReceiver);
}
private void dosomething() {
//perform my task
}
private BroadcastReceiver stopReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("receive","STOP");
mHandler.removeCallbacks(task);
}
};
}
I change my code to this and it is working
public class SendMessageService extends Service {
private Handler mHandler = new Handler();
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
registerReceiver(stopReceiver, new IntentFilter("com.android.STOP_HANDLER"));
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
try {
mHandler.postDelayed(task, 40000);
} catch (Exception e) {
e.printStackTrace();
}
return START_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(stopReceiver);
}
private void dosomething() {
//perform my task
}
private BroadcastReceiver stopReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("receive","STOP");
mHandler.removeCallbacks(task);
}
};
private Runnable task = new Runnable() {
#Override
public void run() {
dosomething();
mHandler.postDelayed(this, 40000);
}
};
}
As before, I work with Android Beacon Library,
It already worked and I can found out beacon via BLE - Bluetooth low energy,
But now, after updated to latest version of library, now method onBeaconServiceConnect() not run anymore.
Please tell me what I need to do to make it works,
Thank you,
p/s : Code :
Manifest.xml
<uses-feature
android:name="android.hardware.bluetooth_le"
android:required="true" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.INTERNET" />
<service
android:name="org.altbeacon.beacon.service.BeaconService"
android:enabled="true"
android:isolatedProcess="false"
android:label="beacon" />
<service
android:name="org.altbeacon.beacon.BeaconIntentProcessor"
android:enabled="true" />
Java
public class FoundBeaconFragment extends Fragment
implements BeaconConsumer {
#Override
public boolean bindService(Intent intent, ServiceConnection serviceConnection, int i) {
return false;
}
#Override
public Context getApplicationContext() {
return getActivity();
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_found_beacon, null);
// Set adapter
foundBeaconAdapter = new FoundBeaconAdapter(
getActivity(),
R.layout.simple_list_item_found_beacon,
mAlFoundBeacon);
mLvFoundBeacon.setAdapter(foundBeaconAdapter);
// Register Scan beacons feature
register();
return v;
}
#Override
public void onDestroy() {
super.onDestroy();
try {
// Unbind scan beacon progress
if (beaconManager != null)
beaconManager.unbind(this);
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public void unbindService(ServiceConnection serviceConnection) {
}
// CUSTOM METHODS
private void register() {
beaconManager = BeaconManager.getInstanceForApplication(getActivity());
// To detect proprietary beacons, you must add a line like below corresponding to your beacon
// type. Do a web search for "setBeaconLayout" to get the proper expression.
try {
// todo
beaconManager.getBeaconParsers().add(new BeaconParser().
setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
} catch (Exception e) {
e.printStackTrace();
}
// CAN SEE THIS LOG CAT, NO EXCEPTION
Log.i("", "Register Service");
beaconManager.bind(this);
}
#Override
public void onBeaconServiceConnect() {
beaconManager.setRangeNotifier(new RangeNotifier() {
#Override
public void didRangeBeaconsInRegion(
Collection<org.altbeacon.beacon.Beacon> beacons, Region region) {
Log.i("", "IS_SCAN_BEACON " + FoundBeaconFragment.IS_SCAN_BEACON);
if (FoundBeaconFragment.IS_SCAN_BEACON) {
Log.i("", "Found " + beacons.size() + " beacon!");
if (beacons.size() > 0) {
/**
* Begin transfer data
*/
for (final org.altbeacon.beacon.Beacon beacon : beacons) {
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
getDataViaBLE(getActivity(), beacon.getId1() + "",
beacon.getId2() + "", beacon.getId3() + "");
}
});
}
}
}
}
});
try {
beaconManager.startRangingBeaconsInRegion(
new Region(Constant.UUID, null, null, null));
} catch (RemoteException e) {
e.printStackTrace();
}
}
public static String UUID = "01122334-4556-6778-899a-abbccddeeff0";
ANSWER
Since I used Fragment not Activity as sample codes from the library.
So I need do these changes :
#Override
public Context getApplicationContext() {
return getActivity().getApplicationContext();
}
#Override
public void unbindService(ServiceConnection serviceConnection) {
getActivity().unbindService(serviceConnection);
}
#Override
public boolean bindService(Intent intent, ServiceConnection serviceConnection, int i) {
return getActivity().bindService(intent, serviceConnection, i);
}
If you are implementing the BeaconConsumer interface in a Fragment (and not an Activity, Service or Application instance), you need to chain all of the methods. Like this:
#Override
public Context getApplicationContext() {
return getActivity().getApplicationContext();
}
#Override
public void unbindService(ServiceConnection serviceConnection) {
getActivity().unbindService(serviceConnection);
}
#Override
public boolean bindService(Intent intent, ServiceConnection serviceConnection, int i) {
return getActivity().bindService(intent, serviceConnection, i);
}
I am not sure.before few days ,this beacon code is working for me.please check.if any issue ,i will send whole my code.
try this one:
beaconManager.startRangingBeaconsInRegion(new Region("myBeaons", Identifier.parse(UUID), null, null));
instead of this line in your code.
beaconManager.startRangingBeaconsInRegion(
new Region(Constant.UUID, null, null, null));
this Code is working for me.please try this code:
Create Application Class:
public class BeaconReferenceApplication extends Application implements BootstrapNotifier {
private BackgroundPowerSaver backgroundPowerSaver;
private boolean haveDetectedBeaconsSinceBoot = false;
private MonitoringActivity monitoringActivity = null;
private String UUID = "23542266-18D1-4FE4-B4A1-23F8195B9D39";
private static final String TAG = ".MyApplicationName";
private RegionBootstrap regionBootstrap;
#Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "App started up");
BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);
// To detect proprietary beacons, you must add a line like below corresponding to your beacon
// type. Do a web search for "setBeaconLayout" to get the proper expression.
// beaconManager.getBeaconParsers().add(new BeaconParser().
// setBeaconLayout("m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));
// wake up the app when any beacon is seen (you can specify specific id filers in the parameters below)
Region region = new Region("com.example.myapp.boostrapRegion", null, null, null);
regionBootstrap = new RegionBootstrap(this, region);
}
#Override
public void didDetermineStateForRegion(int arg0, Region arg1) {
// Don't care
}
#Override
public void didEnterRegion(Region arg0) {
Log.d(TAG, "Got a didEnterRegion call");
// This call to disable will make it so the activity below only gets launched the first time a beacon is seen (until the next time the app is launched)
// if you want the Activity to launch every single time beacons come into view, remove this call.
regionBootstrap.disable();
Intent intent = new Intent(this, MainActivity.class);
// IMPORTANT: in the AndroidManifest.xml definition of this activity, you must set android:launchMode="singleInstance" or you will get two instances
// created when a user launches the activity manually and it gets launched from here.
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(intent);
}
#Override
public void didExitRegion(Region arg0) {
// Don't care
}
}
Activity Class
public class MainActivity extends Activity implements BeaconConsumer {
public static final String TAG = "BeaconsEverywhere";
private BeaconManager beaconManager;
private String UUID = "23542266-18D1-4FE4-B4A1-23F8195B9D39";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
beaconManager = BeaconManager.getInstanceForApplication(this);
beaconManager.getBeaconParsers().add(new BeaconParser()
.setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));
beaconManager.bind(this);
}
#Override
protected void onDestroy() {
super.onDestroy();
beaconManager.unbind(this);
}
#Override
public void onBeaconServiceConnect() {
final Region region = new Region("myBeaons", Identifier.parse(UUID), null, null);
beaconManager.setMonitorNotifier(new MonitorNotifier() {
#Override
public void didEnterRegion(Region region) {
try {
Log.d(TAG, "didEnterRegion");
beaconManager.startRangingBeaconsInRegion(region);
} catch (RemoteException e) {
e.printStackTrace();
}
}
#Override
public void didExitRegion(Region region) {
try {
Log.d(TAG, "didExitRegion");
beaconManager.stopRangingBeaconsInRegion(region);
} catch (RemoteException e) {
e.printStackTrace();
}
}
#Override
public void didDetermineStateForRegion(int i, Region region) {
}
});
beaconManager.setRangeNotifier(new RangeNotifier() {
#Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
for (Beacon oneBeacon : beacons) {
Log.d(TAG, "distance: " + oneBeacon.getDistance() + " id:" + oneBeacon.getId1() + "/" + oneBeacon.getId2() + "/" + oneBeacon.getId3());
}
}
});
try {
beaconManager.startMonitoringBeaconsInRegion(region);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
I'm developing a tracking app. and i have problem with GPS module. The app must record a route. App work fine, but sometimes when the device is not moving, GPS still receive
continuous coordinate that don't indicate my position, error is within a radius of 20 meter, and when I'm moving again work fine.
Please give me some tips that can help me to fix this problem. Thanks a lot.
I have 3 calsses
1 - GPSReceiver here is method for get location
public void getMyLoction(){
_locationManager = (LocationManager) _context.getSystemService(LOCATION_SERVICE);
_isGPSEnabled =_locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (_isGPSEnabled) {
if (_location == null) {
_locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0, this);
if (_locationManager != null) {
_location = _locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
setLocation(_location);
}
}
}
}
2 RecordingActivity (take coordonates form services and processes then) work fine, a comment in method what they do.
public class RecordingActivity extends FragmentActivity {
public final static String BROADCAST_ACTION = "map.trackv";
public BroadcastReceiver receiver;
private GoogleMap map;
private TextView _messageToUser;
private Coordinate _pointFromService;
private long _timeWhenStartButtonWasPressed;
private List<Coordinate> _unprocessedCoords;
private List<Coordinate> _processedCoords;
private Button _stopButton;
private Button _startButton;
private String _startRecordingDate;
private String _stopRecordingDate;
private GPSReceiver _gps;
private DataBaseOperations _dataSource;
private boolean _recording;
private boolean _gpsStatus;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recording_route);
initActvity();
checkIfGPSisOn();
try {
Runtime.getRuntime().exec("logcat -f" + " /sdcard/Logcat.txt");
} catch (IOException e) {
// TODO Auto-generated catch block
Log.d("nu pot", "DDDDD");
e.printStackTrace();
}
receveirWork();
IntentFilter intentFilt = new IntentFilter(BROADCAST_ACTION);
registerReceiver(receiver, intentFilt);
}
public void checkIfGPSisOn() {
//check on start
}
public void receveirWork() {
receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// request points and process then,
}
};
}
#Override
protected void onDestroy() {
super.onDestroy();
if (_stopButton.isEnabled())
{
stopService(new Intent(this, RecordingService.class));
_unprocessedCoords = null;
_processedCoords = null;
}
unregisterReceiver(receiver);
}
#Override
protected void onResume() {
if (!_stopButton.isEnabled()) {
_startButton.setEnabled(true);
_messageToUser.setText(Constants.PRESS_START_BUTTON);
map.clear();
}
super.onResume();
}
// actiune buton start;
public void startButtonEvent(View V) {
buttonsStateAndMessageToShow(false, true, Constants.MESSAGE_TO_WAIT);
_timeWhenStartButtonWasPressed = System.currentTimeMillis();
startService(new Intent(this, RecordingService.class));
// start service to get position
}
public void stopButtonEvent(View V) {
stopService(new Intent(this, RecordingService.class));
// stop service
// save route in BD
// resetData;
}
public void initActvity() {
// init date
}
#Override
protected void onSaveInstanceState(Bundle outState) {
// save state
}
}
3 RecordingServices class, ii think here is the problem.
public class RecordingService extends Service {
private Thread _backgroundWork;
private boolean _threadCanRun;
private GPSReceiver _gps;
private Coordinate _pointToSent;
public void onCreate() {
super.onCreate();
_threadCanRun = true;
_backgroundWork = new Thread(new Runnable() {
#Override
public void run() {
Looper.prepare();
getLocationFromGPS();
Looper.loop();
}
});
}
public int onStartCommand(Intent intent, int flags, int startId) {//
_backgroundWork.start();
return super.onStartCommand(intent, flags, startId);
}
public void onDestroy() {
_threadCanRun = false;
super.onDestroy();
}
public IBinder onBind(Intent intent) {
return null;
}
public void getLocationFromGPS() {
while (_threadCanRun) {
Intent _intent = new Intent(RecordingActivity.BROADCAST_ACTION);
_gps = new GPSReceiver(this);
_gps.getMyLoction();
if (_gps.getIsGPSEnabled()) {
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {}
sentPoint(_intent);
} else {
try {
TimeUnit.MILLISECONDS.sleep(500);
} catch (InterruptedException e) {}
_intent.putExtra("latitude", 0);
_intent.putExtra("longitude", 0);
_intent.putExtra("time", 0);
_intent.putExtra("GPSstatus", false);
sendBroadcast(_intent);
}
}
}
private void sentPoint(Intent _intent) {
_pointToSent = new Coordinate(_gps.getLatitude(), _gps.getLongitude(), _gps.getTime());
_intent.putExtra("latitude", _pointToSent.getLatitude());
_intent.putExtra("longitude", _pointToSent.getlongitude());
_intent.putExtra("time", _pointToSent.getTime());
_intent.putExtra("GPSstatus", _gps.getIsGPSEnabled());
sendBroadcast(_intent);
_pointToSent = null;
}
}
repeating the Location update request depends on how u implemented your tracking system
but in general(which is not recommended , just change your request update rate to save client Battery usage) you can find the distance between your locations by location1.distanceTo(location2) so if the distance is smaller than 30m then put the new location away
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();
}
}