Android background task issue - java

I am having a background task issue with Service class from Android, Whenever I am starting service from application and return to main menu on my device till then service running properly, but When I will clear all the background application from the recent application list then the service also stops. So how can I manage to avoid such a thing.

The onStartCommand method on your sub IntentService class should return Service.START_STICKY
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
//some code...
return Service.START_STICKY;
}
Then the service will keep running in background.

When I will clear all the background application from the recent application list then the service also stops
You can use startForeground() to declare it to be a foreground service. A foreground service must provide a notification for the status bar, which is placed under the "Ongoing" heading, which means that the notification cannot be dismissed unless the service is either stopped or removed from the foreground.
Notification notification = new Notification(R.drawable.icon, getText(R.string.ticker_text),
System.currentTimeMillis());
Intent notificationIntent = new Intent(this, ExampleActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, getText(R.string.notification_title),
getText(R.string.notification_message), pendingIntent);
startForeground(ONGOING_NOTIFICATION_ID, notification);

Related

Timer in android intent service

i have a class to get notification from my server I don't use firebase cloud messaging so I am getting data with the intentservice class. However I want to check the notification in per 60 seconds but timer is not work in
protected void onHandleIntent(Intent intent) {
it starts normally when I start the app but after that it doesn't check in time.
is there any way to check the notification every 60 seconds at the background ?
note: i don't use GCM firebase and I will never use it
IntentService starts the service when you send it an intent, runs whatever you define for that intent, then ends the service. If you are intending for the timer to be run then perhaps do it outside of the service. Perhaps you could use a Handler and post a delayed runnable firing the intent, although I must strongly advise against this in the long term since doing 60 second checks using the phone's radio will drain its battery fast. GCM's Firebase is meant to handle that issue since it "batches" the network requests, and if you don't want to do that then perhaps use a JobScheduler. Android manages the phone's radio pretty nicely and efficiently so unless 60 seconds is absolutely necessary, then it's not really advisable to do this; users could complain of battery drain.
On that handler thing:
private static Handler handler = new Handler(Looper.getMainLooper);
In your activity, then
private Runnable runnable = new Runnable() {
#Override
public void run() {
try{
//Start your intent service here.
} finally {
handler.postDelayed(runnable, 60000);
}
}
};
The try finally block is if your process somehow may trigger an error. If not necessary then you can remove it.
Then in your Activity's onCreate() or wherever you need to start the repeated trigger:
runnable.run();
You can use Alarm manager
Intent alarmIntent = new Intent(context, MyService.class);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, alarmIntent, 0);
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
manager.setRepeating(AlarmManager.RTC_WAKEUP, 60000, 60000, pendingIntent);

Android - Notification pendingIntent to Stop Service

I have two notification actions, one to stop the service and one to restart it.
I am successfully starting the service but I can't stop it with this code:
PendingIntent show = PendingIntent.getService(this, 1, svc, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent hide = PendingIntent.getService(this, 1, svc, PendingIntent.FLAG_CANCEL_CURRENT);
Any ideas?
Not a duplicate as my question is specifically about notification actions, not buttons (I have no problems getting my buttons to stop and start the service).
That flag alone will not stop the service. I would recommend that you make the stop action instead fire a custom BroadcastReceiver class which runs the stopService() method inside of its onReceive(). Let me know if you need help setting something like that up in more detail.
Edited answer:
Change your Intent and PendingIntent for the hide action to this:
Intent intentHide = new Intent(this, StopServiceReceiver.class);
PendingIntent hide = PendingIntent.getBroadcast(this, (int) System.currentTimeMillis(), intentHide, PendingIntent.FLAG_CANCEL_CURRENT);
Then make the StopServiceReceiver like this, where ServiceYouWantStopped.class is the service to be stopped:
public class StopServiceReceiver extends BroadcastReceiver {
public static final int REQUEST_CODE = 333;
#Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, ServiceYouWantStopped.class);
context.stopService(service);
}
}
Make sure the BroadcastReceiver you just made is declared in your manifest file:
<receiver
android:name=".StopServiceReceiver"
android:enabled="true"
android:process=":remote" />
Hope this helps!

Android is killing my foreground service after Force stop my package

I've been struggling with this for 2 weeks now.
I'm developing an app that controls call duration.
I receive a braodcast where I start a foreground service to hang up the call.
but after five minutes or more android force stop my package then kills my process which cause the service to crash -I think - (don't know why) with no crash message or any kind of error.
it just disappear.
And it Schedules restart but it never start the service again.
here is the logcat
12-29 00:28:52.857 619-619/? I/ActivityManager: Force stopping package club.androidy.callcontrolfree appid=10006 user=0
12-29 00:28:52.858 619-619/? I/ActivityManager: Killing proc 433:club.androidy.callcontrolfree/u0a10006: force stop club.androidy.callcontrolfree
12-29 00:28:52.894 619-619/? W/ActivityManager: Scheduling restart of crashed service club.androidy.callcontrolfree/.PhoneCallService in 5000ms
12-29 00:28:52.919 619-619/? I/ActivityManager: Force stopping service ServiceRecord{422b1b18 u0 club.androidy.callcontrolfree/.PhoneCallService}
using adb shell dumpsys I got this which insures that my service is a foreground service with the right priority
*APP* UID 10088 ProcessRecord{41aefb98 27922:club.androidy.callcontrolfree/u0a10088}
user #0 uid=10088
class=club.androidy.callcontrolfree.AnalyticsApplication
dir=/data/app/club.androidy.callcontrolfree-1.apk publicDir=/data/app/club.androidy.callcontrolfree-1.apk data=/data/data/club.androidy.callcontrolfree
packageList=[club.androidy.callcontrolfree]
compat={240dpi}
thread=android.app.ApplicationThreadProxy#41cef800
pid=27922 starting=false lastPss=0
lastActivityTime=-11s768ms lruWeight=14097791 serviceb=false keeping=true hidden=false empty=true
oom: max=15 hidden=9 client=9 empty=15 curRaw=2 setRaw=2 nonStopping=2 cur=2 set=2
curSchedGroup=-1 setSchedGroup=-1 systemNoUi=false trimMemoryLevel=0
adjSeq=63108 lruSeq=10968
setIsForeground=false foregroundServices=true forcingToForeground=null
lastRequestedGc=-12s74ms lastLowMemory=-12s74ms reportLowMemory=false
Services:
- ServiceRecord{41fb2578 u0 club.androidy.callcontrolfree/.PhoneCallService}
and in Process LRU list (sorted by oom_adj): section
Proc #24: adj=prcp /FS trm= 0 27922:club.androidy.callcontrolfree/u0a10088 (fg-service)
I'm not binding my service to any activities
I start my service like this:
Intent srvIntent = new Intent(context, PhoneCallService.class);
context.stopService(srvIntent);
PhoneCallService.stopService = false;
context.startService(srvIntent);
and this is my onStartCommand :
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Context context = getApplicationContext();
String txtNotificationTicker = context.getString(R.string.strNotificationTicker);
String txtNotificationTitle = context.getString(R.string.strNotificationContexTitle);
String txtNotificationText = context.getString(R.string.strNotificationContexText);
String ns = Context.NOTIFICATION_SERVICE;
Intent cancelIntent = new Intent(this, StopServiceReceiver.class);
PendingIntent pendingIntentCancel = PendingIntent.getBroadcast(this, 0, cancelIntent
, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Action action =
new NotificationCompat.Action
.Builder(R.drawable.ic_cancel
, context.getString(R.string.stop_service), pendingIntentCancel)
.build();
Intent mIntent = new Intent(this,MainActivity.class);
int HELLO_ID = 1;
PendingIntent pendingIntent = PendingIntent.getActivity(this, HELLO_ID, mIntent , 0);
this.mNotificationManager = (NotificationManager) getSystemService(ns);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
Notification notification = builder
.setContentIntent(pendingIntent)
.setContentTitle(new StringBuilder(String.valueOf(txtNotificationTitle)))
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(getBitmap(context, R.drawable.ic_launcher))
.setContentText(new StringBuilder(String.valueOf(txtNotificationText))
.append(": ").append(PHONE_NUMBER).toString())
.setWhen(System.currentTimeMillis())
.setPriority(NotificationCompat.PRIORITY_MAX)
.addAction(action)
.build();
notification.flags |= Notification.FLAG_NO_CLEAR;
startForeground(1, notification);
this.pt = new PhoneThread();
this.pt.start();
return Service.START_STICKY;
}
I'm acquiring wake lock like this
if (PhoneCallService.sCpuWakeLock == null) {
PhoneCallService.sCpuWakeLock = ((PowerManager)
context.getSystemService(Context.POWER_SERVICE))
.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"CCF");
PhoneCallService.sCpuWakeLock.acquire();
Log.e("androidy","wacklock acquired");
}
I have already tried many solutions from SO and google issues but nothing works for me.
I saw and tried all these :
Foreground service killed by OS
Foreground service gets killed every time
Android foreground service being killed under certain conditions
Foreground Service being killed on Notification click
Foreground service being killed by Android
Users who keep the app are only 30% from total installs.
What I'm doing wrong?
Edit:
If you have the same issue please check Power Saving Options in your phone settings if your phone has this feature it will kill your app after the screen is off and will not allow it to work in background.
Original answer:
After a lot of search I solved it myself
it turned out that my app was power-intensive as shown in the picture below
Hence, android was force stopping it after 5 minutes when there is no activity on the screen to save power.
I solved power consumption issues and everything became OK.

Activity freezes when running service

I have a button that when pressed, starts a service. This service starts logging information from the device. However, when I press the run, the screen temporarily freezes for about 8 seconds whilst it logs, and then once it is finished the screen unfreezes, a toast message shows (that's meant to show as soon as you press the button) and then I can do whatever. If i go on the application screen again, when the application is logging (note: the service is always running so that doesnt freeze it, just the logging) then the activity is frozen again and doesnt let me do anything until the logging is complete.
I have tried asynctasks and running on a new runnable/new thread but none of these have seemed to work for me. Whether i am implementing them correctly or not I'm unsure, but I'd like to fix the problem.
Here is my onStartCommand in the service class:
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
// THIS WOULD BE THE METHOD THAT RUNS WHATEVER YOU WANT TO DO EVERY SO OFTEN SO FOR ME THIS IS GETTING ALL DATA ETC
getProcessInfo();
// LOG DELAY = SECONDS BETWEEN RUNNING SERVICE/METHOD. SET AT THE TOP
myPendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Alarmmgr.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+LOG_DELAY*1000, myPendingIntent);
Intent notificationIntent = new Intent(this, LogOrCheck.class);
PendingIntent contentIntent = PendingIntent.getActivity(this,
101, notificationIntent,
PendingIntent.FLAG_NO_CREATE);
NotificationManager nm = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
Resources res = this.getResources();
Notification.Builder builder = new Notification.Builder(this);
builder.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.arrow_up_float)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.arrow_down_float))
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setContentTitle("Androigenius")
.setContentText("Service running OK");
Notification n = builder.getNotification();
startForeground(100, n);
return Service.START_STICKY;
}
And here is where I call startService:
but1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (isClicked) {
Context context = getApplicationContext();
Toast toast = Toast.makeText(context, "Please press 'MINIMIZE' at the top to continue logging.", Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP|Gravity.RIGHT, 10, 55);
toast.show();
System.out.println("Start logging");
but1.setText("Stop Logging");
but1.setTextColor(Color.RED);
// START SERVICE, DONE. (STOP IS BELOW).
startService(myIntent);
isClicked = false;
}
else if (!isClicked) {
System.out.println("Stop Logging");
but1.setText("Start Logging");
// STOP SERVICE, DONE.
stopService(myIntent);
but1.setTextColor(getResources().getColor(color.cornblue));
isClicked = true;
}
Services run on the UI thread which is causing the 'freeze'. Using an IntentService will create a service that automatically runs in the background, not on the UI thread. Here is some details of the IntentService class:
IntentService Class
By default, the Service also run in the main thread of your applicaiton, in which your UI operations occur. Therefore, if you perform a long task in your onStartCommand() method, it will block the main thread.
To avoid the problem, you have to migration the logging task into a separate thread. You may use AsycTask class to do so. Please refer to http://developer.android.com/reference/android/os/AsyncTask.html for how to use the class.

Receiving notifications: on click handle for multiple activities

In my application, I have two main activities:
MainActivity and MessagesActivity
When the notification is clicked and the application is not running, I want it to start the MainActivity. However, when the application is running and the MessagesActivity is in view, I want when the user clicks the notification to somehow receive the intent data.
I create my notification something like this:
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.putExtra(MESSAGE_KEY, receivedMessage);
notificationIntent.putExtra(MESSAGE_ID_KEY, receivedMessageId);
int requestID = (int) System.currentTimeMillis();
PendingIntent contentIntent = PendingIntent.getActivity(context, requestID, notificationIntent, PendingIntent.FLAG_ONE_SHOT, intent.getExtras());
Notification notification = new Notification.Builder(context)
....build();
notificationManager.notify(requestID, notification);
I've tried playing around with other flags, launchModes, etc. This wouldn't be a problem if it was all just one activity but onNewIntent is never called when the application is running in either MainActivity or MessagesActivity

Categories

Resources