I know there are a lot of questions on this already but it doesn't seem to work, so basically I have a custom Service, the service works fine when the timer inside it expires, and calls the stopservice method. however when I try and stop the service from another activity it doesn't work?
code for custom service:
public class ConnectionService extends Service {
Intent service_intent;
Timer t;
int expiry_time = 0;
int timer_tick_starter = 0;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
t = new Timer();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
try {
service_intent = intent;
Log.d("onConnectionService", "onStartCommand");
expiry_time = Integer.parseInt(intent.getStringExtra("expiry"));
//convert the minutes to milliseconds
timer_tick_starter = (expiry_time * 60000);
Log.d("onConnectionService", "Expiry Time (in milliseconds) " + timer_tick_starter);
} catch (Exception e) {
Log.d("onConnectionService", e.getMessage().toString());
}
//timer will only start to run on the expiry_time which will then close the connection
t.schedule(new TimerTask() {
#Override
public void run() {
Log.d("onConnectionService", "inside expiry time");
//close the connection
stopService(service_intent);
}
}, 30000);
return START_NOT_STICKY;
}
#Override
public boolean stopService(Intent name) {
Log.d("onConnectionService", "StopService called");
String message = name.getStringExtra("msg");
//broadcast sender
Intent intent = new Intent();
intent.putExtra("msg", message);
intent.setAction("fi.android.inetkeyapplication.CUSTOM_INTENT");
intent.putExtra("is_selected", UserConnectionState.getInstance().is_selected);
sendBroadcast(intent);
Log.d("onConnectionService", "Service has been killed");
return super.stopService(name);
}
#Override
public void onDestroy() {
Log.d("onConnectionService", "Destroy called");
}
}
here I start the service from onCreate:
Thread thread = new Thread(new Runnable() {
#Override
public void run() {
Log.d("onConfigurationActivity", "Service starting ");
// Intent i = new Intent(ConfigurationActivity.this, ConnectionService.class);
Intent i = new Intent(context, ConnectionService.class);
i.putExtra("expiry", "1");
startService(i);
}
});
thread.start();
however later in my app when I try to kill it from the activity in which I start it, it doesn't work:
Button exit_button = (Button) findViewById(R.id.btnExit);
connect_button.setTypeface(roboto_light);
exit_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//do closing call
response_close = "";
//need to close service
//also kill the service
Log.d("onConfigurationActivity", "killing service");
Intent intent = new Intent(context, ConnectionService.class);
intent.putExtra("msg", "You have been disconnected");
stopService(intent);
}
});
I have tried my class - ConfigurationActivity.this (as context) and I have tried (this) at the moment I am using a custom context that I simply get inside onCreate. however nothing seems to work what am I missing?
thanx
ok so apparently stopping the service doesn't stop the timer. so I just called cancel() on timer
Related
This question already has answers here:
How to detect power connected state?
(6 answers)
Closed 2 years ago.
I am developing a dash cam android application. What I am wondering is that is there any way to auto launch the application when the power is connected to the android device. I am thinking to make the app fully automated like when the car engine starts and the power connects to the device with the usb cable, the application automatically launch and start to operate. I read some posts about auto start when the device boots. but, is there any way to auto start when the power connects to the device? thank you.
first you have to make service app.
and using BroadcastReceiver for get state of power.
private final BroadcastReceiver mConnectionReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_POWER_CONNECTED.equals(intent.getAction())) {
Log.e("mConnectionReceiver", "AC Connected by onReceive");
// turn on your app via using Intent
} else if (Intent.ACTION_POWER_DISCONNECTED.equals(intent.getAction())) {
Log.e("mConnectionReceiver", "AC Disconnected by onReceive");
}
}
};
for make service app
public class test extends AppCompatActivity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main ); // run Background Service
Intent serviceintent = new Intent( test.this, MyService.class );
startService( serviceintent );
}
}
and serviceThread
public class ServiceThread extends Thread {
Handler handler;
boolean isRun = true;
public ServiceThread(Handler handler) {
this.handler = handler;
}
public void stopForever() {
synchronized (this) {
this.isRun = false;
}
}
public void run() {
while (isRun) {
handler.sendEmptyMessage( 0 );
try {
Thread.sleep( 1000 );
} catch (Exception e) {
}
}
}
}
and make Service
public class MyService extends Service {
ServiceThread thread;
private final BroadcastReceiver mConnectionReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_POWER_CONNECTED.equals(intent.getAction())) {
Log.e("mConnectionReceiver", "AC Connected by onReceive");
// turn on your app via using Intent
} else if (Intent.ACTION_POWER_DISCONNECTED.equals(intent.getAction())) {
Log.e("mConnectionReceiver", "AC Disconnected by onReceive");
}
}
};
#Override public IBinder onBind(Intent intent) {
return null;
}
#Override public int onStartCommand(Intent intent, int flags, int startId) {
myServiceHandler handler = new myServiceHandler();
thread = new ServiceThread( handler );
thread.stopForever(); return START_STICKY;
}
public void onDestroy() {
myServiceHandler handler = new myServiceHandler();
thread = new ServiceThread( handler );
thread.start();
}
public void start() {
myServiceHandler handler = new myServiceHandler();
thread = new ServiceThread( handler );
thread.start();
}
public void stop() {
myServiceHandler handler = new myServiceHandler();
thread = new ServiceThread( handler );
thread.stopForever();
}
public class myServiceHandler extends Handler {
#Override public void handleMessage(android.os.Message msg) {
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_POWER_CONNECTED);
filter.addAction(Intent.ACTION_POWER_DISCONNECTED);
registerReceiver(mConnectionReceiver, filter);
}
}
this is just example. so check this and fix what you want.
I am trying to load an URL (API) after every 15 seconds in a service. Everything is working fine but when the app is killed URL is not called. I dont need any UI in my app. I just want it to work in background when the app is killed. I have been finding a solution for two days but nothing worked. Please help!
Here is my service code :
public class MyService extends Service {
Handler handler = new Handler();
Runnable runnable;
int delay = 15*1000;
String data ;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(final Intent intent, int flags, int startId) {
handler.postDelayed( runnable = new Runnable() {
public void run() {
data = (String) intent.getExtras().get("data");
Toast.makeText(MyService.this, ""+data, Toast.LENGTH_SHORT).show();
loadURL(data);
handler.postDelayed(runnable, delay);
}
}, delay);
return START_STICKY ;
}
#Override
public void onDestroy() {
handler.postDelayed( runnable = new Runnable() {
public void run() {
Toast.makeText(MyService.this, ""+data, Toast.LENGTH_SHORT).show();
loadURL(data);
handler.postDelayed(runnable, delay);
}
}, delay);
}
public void loadURL(String data){
try{
RequestFuture<JSONObject> requestFuture=RequestFuture.newFuture();
final String mURL = "http://192.168.1.12/att.php?emp_id=" + data + "&status=&submit=Insert";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST,
mURL,new JSONObject(),requestFuture,requestFuture);
MySingleton.getInstance(this).addToRequestQueue(request);
Toast.makeText(this, "Done", Toast.LENGTH_SHORT).show();
try {
JSONObject object= requestFuture.get(10, TimeUnit.SECONDS);
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e){
Toast.makeText(this, ""+e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
}
Here is my MainActivity.java where I am getting an intent as a userID :
public class MainActivity extends AppCompatActivity {
String data;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
data = getIntent().getStringExtra("ID");
Intent intent = new Intent(MainActivity.this, MyService.class);
intent.putExtra("data", data);
startService(intent);
}
}
You will have to turn that background service into a foreground service, because of the limitations called Background Execution Limits that started from android Oreo.
Please check out this link for more better understanding:
https://developer.android.com/about/versions/oreo/background
i have a problem in my service, the service is runnig from MainActivity (i am doing test) and extends of service.
i want to use the service from foreground and background (when the app is closed) and i already have my first problem:
my service(have a counter that is displayed by LOG) is restarting when i close the app.
also i want to be able to use the service with the open app and close app, in other words to use both the Service Started and Link Service
public class MyService extends Service {
private Thread backgroundThread;
private boolean isRunning;
public MyService() {
Log.e("MyService","constructor");
}
#Override
public void onCreate() {
super.onCreate();
isRunning = false;
}
#Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
#Override
public int onStartCommand(final Intent intent, int flags, int startId) {
Log.e("onStartCommand","Servicio Llamado");
if (!this.isRunning) {
Log.e("onStartCommand","hilo iniciandose");
this.backgroundThread = new Thread(myTask);
runTask();
}
return super.onStartCommand(intent, flags, startId);
}
#Override
public void onDestroy() {
super.onDestroy();
Log.e("onDestroy","servicio destuido");
}
private void runTask(){
this.isRunning = true;
this.backgroundThread.start();
}
private Runnable myTask = new Runnable() {
public void run() {
Log.e("myTask","hilo iniciado");
int i = 0;
do{
pauseService();
Log.e("myTask","hilo contador: "+i);
//Toast.makeText(getApplicationContext(),"CONTADOR = "+j, Toast.LENGTH_SHORT).show(); //no working :(
i++;
}while (i<10);
/*
//linea de detencion del servicio
//stopSelf();
isRunning=false;
backgroundThread.interrupt();
//backgroundThread = new Thread(myTask);
*/
Log.e("myTask","hilo cerrado");
}
};
private void pauseService(){
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
In main Activity
Intent intent = new Intent(MainActivity.this,MyService.class);
intent.putExtra("iteraciones",10);
startService(intent);
why because the service restarts when I close the application and how can I avoid it?
I have searched and found questions similar to the title, but no solution so far.
I have the following service
public class MyService extends Service {
public MyService() {
}
private static final String TAG =
"ServiceExample";
#Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "Service onCreate");
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "Service onStartCommand " + startId);
final int currentId = startId;
Runnable r = new Runnable() {
public void run() {
for (int i = 0; i < 3; i++)
{
long endTime = System.currentTimeMillis() +
10*1000;
while (System.currentTimeMillis() < endTime) {
synchronized (this) {
try {
wait(endTime -
System.currentTimeMillis());
} catch (Exception e) {
}
}
}
Log.i(TAG, "Service running " + currentId);
}
//stopSelf();
}
};
Thread t = new Thread(r);
t.start();
return Service.START_STICKY;
}
#Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "Service onBind");
return null;
}
#Override
public void onDestroy() {
Log.i(TAG, "Service onDestroy");
super.onDestroy();
//Log.i(TAG, "Service Destroyed");
}
}
And this is called from my activity that has two buttons: One for starting the service and one for stopping it
public class ServiceExampleActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_service_example);
}
public void buttonClick(View view)
{
Intent intent = new Intent(this, MyService.class);
startService(intent);
}
public void stopClick(View view)
{
Intent intent = new Intent(this, MyService.class);
stopService(intent);
}
}
However this does not work. The service is not stopped.
Strangely the onDestroy function is called but after that the runnable is still running.
How can I stop the service?
=============================================
EDIT: I finally achieved it by the following changes:
In the onDestroy function
#Override
public void onDestroy() {
Log.i(TAG, "Service onDestroy");
// mHandler.removeCallbacks(r);
t.interrupt();
super.onDestroy();
//Log.i(TAG, "Service Destroyed");
}
the thread t had to be a class member (before it was declared inside onStartCommand) and I modified the runnable as this
r = new Runnable() {
public void run() {
for (int i = 0; i < 3; i++)
{
long endTime = System.currentTimeMillis() +
10*1000;
while (System.currentTimeMillis() < endTime) {
synchronized (this) {
try {
wait(endTime -
System.currentTimeMillis());
} catch (Exception e) {
return; //HERE to detect if the thread has been interrupted
}
}
}
Log.i(TAG, "Service running " + currentId);
}
stopSelf();//I have to use this otherwise it doesn't stop
}
};
I wonder why normally I have to use stopSelf but when interrupted it is not necessary. I suppose stopSelf just calls onDestroy
Stopping a service just kills the context. The framework does not know about any other Threads you may have started- its your job to kill them in onDestroy(). Save the Thread when you create it. In onDestroy(), interrupt it. And in your thread's runnable, regularly check to see if the Thread is interrupted, and return if so. Do not call Thread.stop(), as it may leave your app in an unsafe state.
Service stop working when turn on /of Wi-Fi many time, when I start service do counter 1,2,3 etc or any thing then turn on /of Wi-Fi many time the service stops working ,I have BroadcastReceiver class doing start service, no exceptions , error appear , only I sent one message to phone to start service..
This is the code inside BroadcastReceiver:
if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
Intent recorderIntent = new Intent(context, Start2.class);
context.startService(recorderIntent);
}
This My Start2 Service:
public class Start2 extends Service {
private static final String TAG = Start2.class.getSimpleName();
int mStartMode;
#Override
public void onDestroy() {
Log.d(TAG, "Stop Service onDestroy");
}
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>() {
#Override
protected String doInBackground(Void... params) {
final Handler handler = new Handler(Looper.getMainLooper());
Runnable runnable = new Runnable() {
int i = 0 ;
#Override
public void run() {
try{
//do your code here
Log.d(TAG, "Start Service Repeat Time.. " + i);
i++;
}
catch (Exception e) {
// TODO: handle exception
}
finally{
//also call the same runnable to call it at regular interval
handler.postDelayed( this, 5000 );
}
}
};
handler.postDelayed(runnable, 1000 );
return null;
}
};
task.execute();
return mStartMode;
}
}