I need to use AsyncTask that called "Logout" when my app is killed by swiped. I used Service to handle this event but it doesn't work.
Added service tag in AndroidManifest.xml
<service
android:name=".MyService"
android:stopWithTask="false" />
My Service code
public class MyService extends Service {
PostClass post = new PostClass();
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("ClearFromRecentService", "Service Started");
return START_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
new Logout().execute();
Log.d("ClearFromRecentService", "Service Destroyed");
}
#Override
public void onTaskRemoved(Intent rootIntent) {
Log.e("ClearFromRecentService", "END");
new Logout().execute();
stopSelf();
}
private class Logout extends AsyncTask<Void, Void, Void> {
JSONObject j;
ProgressDialog pDialog;
JSONObject jso;
String veri_string = "";
protected void onPreExecute() {
Log.e("MyService", "onPreExecute()");
}
protected Void doInBackground(Void... unused) {
Log.e("MyService", "doInBackground()");
return null;
}
protected void onPostExecute(Void unused) {
Log.e("MyService", "onPostExecute()");
}
}
}
I started this service in my first activity.
startService(new Intent(this, MyService.class));
is there any way to solve this problem ? Thanks..
Edit: i changed my AsyncTask code. i put some logs after the all lines. When i see my Logcat, "test1", "test2", "test3", "test4" are showing but "test5" and the others are not.
EDIT:
I test with my phone today, and Use IntentService in other process is working :
<service
android:name=".LogoutService"
android:process=":LogoutProcess"/>
<!-- LogoutService use a different process, So when MyService is destroyed can
start LogoutService -->
<service
android:name=".MyService"
android:enabled="true"
android:exported="false"
android:stopWithTask="false"/>
And the two services code:
public class MyService extends Service {
public static final String TAG = "MyService";
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
Log.e(TAG, "onCreate() called");
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(TAG, "onStartCommand() called with: intent = [" + intent + "], flags = [" + flags + "], startId = [" + startId + "]");
return super.onStartCommand(intent, flags, startId);
}
#Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
Log.e(TAG, "onTaskRemoved() called with: rootIntent = [" + rootIntent + "]");
Intent logoutService = new Intent(this, LogoutService.class);
startService(logoutService);
this.stopSelf();
}
}
LogoutService.java code
public class LogoutService extends IntentService {
public static final String TAG = "LogoutService";
public LogoutService() {
super(TAG);
Log.e(TAG, "LogoutService() called");
}
#Override
protected void onHandleIntent(#Nullable Intent intent) {
Log.e(TAG, "onHandleIntent() called with: intent = [" + intent + "]");
// This is in the background thread, just call your logout logic:
try {
Log.e(TAG, "onHandleIntent: " );
HttpURLConnection connection = (HttpURLConnection) new URL("https://www.google.com").openConnection();
String result = readInputStreamToString(connection);
Log.e(TAG, "onHandleIntent() result = [" + result + "]");
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, "onHandleIntent: 111e" );
}
Log.e(TAG, "onHandleIntent: 111a" );
}
}
And the logcat:
03-07 10:04:05.969 8146-8146/org.goodev E/MyService: onCreate() called
03-07 10:04:05.969 8146-8146/org.goodev E/MyService: onStartCommand() called with: intent = [Intent { cmp=org.goodev/com.google.samples.gridtopager.MyService }], flags = [0], startId = [1]
03-07 10:04:10.940 8146-8146/org.goodev E/MyService: onTaskRemoved() called with: rootIntent = [Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=org.goodev/com.google.samples.gridtopager.MainActivity }]
03-07 10:04:10.952 1126-1848/? I/ActivityManager: Start proc 8287:org.goodev:LogoutProcess/u0a248 for service org.goodev/com.google.samples.gridtopager.LogoutService
03-07 10:04:11.016 8287-8287/? E/LogoutService: LogoutService() called
03-07 10:04:11.018 8287-8302/? E/LogoutService: onHandleIntent() called with: intent = [Intent { cmp=org.goodev/com.google.samples.gridtopager.LogoutService }]
03-07 10:04:11.018 8287-8302/? E/LogoutService: onHandleIntent:
03-07 10:04:12.154 8287-8302/org.goodev:LogoutProcess E/LogoutService: onHandleIntent() result = [<!doctype html><html lang="zh-HK"><head><meta content="width=device-width,minimum-scale=1.0" name="viewport"><meta content="telephone=no" name="format-detection"><meta content="address=no" name="format-detection">...orstr=#3f76d3,GradientType=0)}.gb_8a{display:none!import
03-07 10:04:12.154 8287-8302/org.goodev:LogoutProcess E/LogoutService: onHandleIntent: 111a
WARNING: Because the LogoutService is running in other process. So in your logcat do not select "Show only selected application" filter, you should use no filter and filter by tag "MyService|LogoutService".
Why not use IntentService:
#Override
protected void onDestroy() {
// On your main activity's onDestroy method
Intent logoutService = new Intent(this, LogoutService.class);
startService(logoutService);
super.onDestroy();
}
and
public class LogoutService extends IntentService {
public LogoutService() {
super("LogoutService");
}
#Override
protected void onHandleIntent(#Nullable Intent intent) {
// This is in the background thread, just call your logout logic:
List<NameValuePair> params = new ArrayList<>();
try {
veri_string = post.httpPost(cikisURL, "POST", params, 10000);
jso = new JSONObject(veri_string);
}
catch (JSONException e){
e.printStackTrace();
}
Log.e("Response: ", veri_string);
}
}
public class MyService extends Service {
PostClass post = new PostClass();
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("ClearFromRecentService", "Service Started");
return START_NOT_STICKY;
}
#Override
public void onDestroy() {
new Logout().execute();
Log.d("ClearFromRecentService", "Service Destroyed");
}
#Override
public void onTaskRemoved(Intent rootIntent) {
Log.e("ClearFromRecentService", "END");
new Logout().execute();
stopSelf();
}
private class Logout extends AsyncTask<Void, Void, Void> {
JSONObject j;
ProgressDialog pDialog;
JSONObject jso;
String veri_string = "";
protected void onPreExecute() {
}
protected Void doInBackground(Void... unused) {
List<NameValuePair> params = new ArrayList<>();
try {
veri_string = post.httpPost(cikisURL, "POST", params, 10000);
jso = new JSONObject(veri_string);
}
catch (JSONException e){
e.printStackTrace();
}
Log.e("Response: ", veri_string);
return null;
}
protected void onPostExecute(Void unused) {
try {
if(jso.has("data")) {
}
else {
}
pDialog.dismiss();
}
super.onDestroy();
catch (Exception e) {
e.printStackTrace();
}
}
}
}
Try this
Related
I am currently working on a video conferencing app, what I want to achieve is that when someone calls me and upon receiving their call I closed the app by swiping it from recent apps, and then I want to notify the caller by rejecting the call when the app is closed from recent apps
public class RecentAppService extends Service {
private static final String TAG = "RecentAppStatus";
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "onCreate()");
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "onStartCommand()");
return START_STICKY;
}
#Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
Log.i(TAG, "onTaskRemoved()....!!!!!!!!!!");
Boolean callactive= AppsharedPreference.getAppPrefrerence(this)
.readBooleanData("callingactive");
Log.i(TAG, "onTaskRemoved()....!!!!!!!!!! Callactive ::::"+callactive);
if(callactive){
NotificationManager nManager = ((NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE));
nManager.cancelAll();
rejectCall();
}
Intent restartService = new Intent(getApplicationContext(), this.getClass());
restartService.setPackage(getPackageName());
PendingIntent restartServicePI = PendingIntent.getService(
getApplicationContext(), 1, restartService,
PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmService = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
alarmService.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() +1000, restartServicePI);
}
#Override
public void onDestroy() {
super.onDestroy();
Log.i(TAG, "onDestroy()");
}
#Override
public void onLowMemory() {
super.onLowMemory();
Log.i(TAG, "onLowMemory()");
}
public void rejectCall(){
AsyncTaskRunner runner = new AsyncTaskRunner(this);
runner.execute();
}
}
I have added the above Sticky service to perform the tasks when someone closes my app from recent apps,i have also added the below code that contains Asynctask to the Service to perform the reject call function
class AsyncTaskRunner extends AsyncTask<String, String, String> {
private String resp;
private Context mcontext;
public AsyncTaskRunner(Context mContext) {
this.mcontext=mContext;
}
#Override
protected String doInBackground(String... params) {
publishProgress("Sleeping..."); // Calls onProgressUpdate()
try {
String mUserHash= AppsharedPreference.getAppPrefrerence(mcontext)
.readStringData("mUserHash");
String rejectApi= AppsharedPreference.getAppPrefrerence(mcontext)
.readStringData("rejectApi");
String apiToken= AppsharedPreference.getAppPrefrerence(mcontext)
.readStringData("apiToken");
String room= AppsharedPreference.getAppPrefrerence(mcontext)
.readStringData("room");
String callId= AppsharedPreference.getAppPrefrerence(mcontext)
.readStringData("callId");
ApiCallData apiCallData = new ApiCallData(rejectApi, apiToken, room, callId,mUserHash);
WebService.getWebservice().rejectCall(mcontext, apiCallData, new FutureCallback<String>() {
#Override
public void onCompleted(Exception e, String result) {
Log.i(TAG, "onTaskRemoved()....!!!!!!!!!! Result"+result);
}
});
} catch (Exception e) {
e.printStackTrace();
resp = e.getMessage();
}
return resp;
}
#Override
protected void onPostExecute(String result) {
Log.i(TAG, "onTaskRemoved()....!!!!!!!!!! Result"+result);
}
#Override
protected void onPreExecute() {
}
#Override
protected void onProgressUpdate(String... text) {
}
}
Everything works fine and the call gets disconnected correctly at the callerside when the application is not in foreground but when the app is open,then task is not achieved in Asynctask i.e, Asynctask stops when app is closed.. I am newbie to Android ,Thank you
You can use FirebaseJobDispatcher to perform some action even if app is closed.
to use FirebaseJobDispatcher first you need to create a Job , which will be something like this :-
FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(context));
Job myJob = dispatcher.newJobBuilder()
.setService(SyncJobScheduler.class)
.setTrigger(Trigger.executionWindow(0, 0))
.setTag("Set_your_unique_tag_here")
.setReplaceCurrent(false)
.build();
dispatcher.mustSchedule(myJob);
Put this above code where you want to start any task / perform any specific task
and then you can create your Scheduler class like this :-
public class SyncJobScheduler extends JobService {
#Override
public boolean onStartJob(final JobParameters job) {
new Thread(new Runnable() {
#Override
public void run() {
try {
if (("Set_your_unique_tag_here").equalsIgnoreCase(job.getTag())) {
//PERFORM YOUR TASK HERE_ WHICH YOU WANT TO RUN IN ASYNCTASK OR SERVICE
//IT WILL RUN EVEN IF APP IS IN BACKGROUND /CLOSED
}
} catch (Throwable ignored) {
}
}
}).start();
return false;
}
#Override
public boolean onStopJob(JobParameters job) {
return false;
}
}
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.
I'm creating a service for my android application and I want it to run in onPostExecute on another activity.
below is CheckStatus.class (service class)
public class CheckStatus extends Service {
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e("service", "start");
loop();
return START_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
Log.e("service", "stop service");
}
private void loop(){
long endTime = System.currentTimeMillis()+5*1000;
while (System.currentTimeMillis()<endTime) {
synchronized (this) {
try {
wait(endTime - System.currentTimeMillis());
} catch (Exception e) {
}
}
}
Log.e("service", "Check status");
new CheckStatusTask().execute();
}
private class CheckStatusTask extends AsyncTask<String, Void, Void> {
char check_online;
#Override
protected Void doInBackground(String... params) {
//call url json POST method
try {
//Check return data status
} catch (Exception e) {
e.printStackTrace();
check_online = '2';
}
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
dialog.dismiss();
if (check_online == '1') {
Toast.makeText(getApplicationContext(), "online", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "offline", Toast.LENGTH_LONG).show();
}
}
}
}
So I try to call the service on another activity
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
Intent check_status = new Intent(MainActivity.this,CheckStatus.class);
startService(check_status);
}
But this is not running.
How can I make it run (the service)?
Please add following in your manifest.xml
<service
android:name=".NameOfService">
</service>
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;
}
}
I started my service properly, its working well. I write condition that if service is already running then it don't start service again. condition returns true values which shows that service is running. But whenever I force stop my application, or if any exception occurred, my service stops loading data at background and till showing that service is running. That's why service is not re-started.
Intent that I used to start service from activity :
this.startService(new Intent(MainMenu1.this, MovieService.class));
And My service class is :
public class MovieService extends Service {
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TBD
Log.i("Movie Service : "," Created2");
new MovieDownloaderTask().execute();
return Service.START_FLAG_REDELIVERY;
//return Service.START_STICKY;
}
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
#Override
public void onCreate() {
Log.i("Movie Service : "," Created");
}
#Override
public void onDestroy() {
Log.d("Movie Service","onDestroy was Called!");
super.onDestroy();
}
#Override
public void onStart(Intent intent, int startid) {
Log.i("Movie Service : "," Started");
}
public class MovieDownloaderTask extends AsyncTask<Void, Void, Integer>
{
#Override
protected Integer doInBackground(Void... params) {
Log.i("Movie Service"," : Started2");
//Moviejson.createDB();
try
{
Moviejson.insert_Movie_Data();
}
catch(Exception e)
{
Log.e("Exception > ", "i m in doInBackground. Trying to stop Movie service.");
MovieService.this.stopSelf();
e.printStackTrace();
}
return 1;
}
#Override
protected void onPostExecute(Integer result) {
if (result == 1) {
if(MainMenu1.Movies_new_time==null)
{
MainMenu1.Movies_new_time=MainMenu1.sp.getMovies_Old_Date();
}
else
{
MainMenu1.sp.setMovies_Current_Date(MainMenu1.Movies_new_time);
}
MainMenu1.sp.setMovies_Current_Date(MainMenu1.Movies_new_time);
Log.i("Movie Date:", " : "+MainMenu1.Movies_new_time);
MovieService.this.stopSelf();
Log.i("Movie Service"," : Stoped");
super.onPostExecute(result);
}
}
}
}
In your onStartCommand(), replace
return Service.START_FLAG_REDELIVERY;
with
return Service.START_REDELIVER_INTENT