Service stops in android version Oreo and app crashes - java

Service stops in android version Oreo and app crashes. Please guys help me to resolve this problem.
When I am trying to add startServiceForeground(), then it is crashing I have no idea what is going on, so help me.
//Start Service method
private void startSipService() {
Thread t = new Thread("StartSip") {
public void run() {
Intent serviceIntent = new Intent(SipHome.this,SipService.class);
serviceIntent.putExtra(SipManager.EXTRA_OUTGOING_ACTIVITY,
new ComponentName(SipHome.this, SipHome.class));
startService(serviceIntent);z
if(user==null||user==""){
postStartSipService();
}
// boolean status;
// status = loginStatus();
// if (!status){}
// postStartSipService();
}
};
t.start();
}

Android 8.0 (API level 26) also includes the following changes to specific methods: The startService() method now throws an
IllegalStateException if an app targeting Android 8.0 tries to use that method in a situation when it isn't permitted to create background services.
The new Context.startForegroundService() method starts a foreground service. The system allows apps to call Context.startForegroundService() even while the app is in the background. However, the app must call that service's startForeground() method within five seconds after the service is created. For more information, see Background Execution Limits.

Try this:
Intent myService = new Intent(this, MyService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(myService);
} else {
startService(myService);
}

Related

Fix the 'background service' problem on Android 8+

I have code running a service behind the scenes. It is set to run when we copy the text to the phone.
This code works fine on Android 8 below
But the problem is when I run the app on Android 8 and above
In my searches, I realized that I had to use FOREGROUND_SERVICEs and give specific access to the project.
What solutions do you suggest now?
Service Class:
public class AutoDownloadService extends Service {
private ClipboardManager mClipboardManager;
public static final String CHANNEL_ID = "ForegroundServiceChannel";
#Override
public void onCreate() {
super.onCreate();
mClipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
mClipboardManager.addPrimaryClipChangedListener(mOnPrimaryClipChangedListener);
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
String input = intent.getStringExtra("inputExtra");
createNotificationChannel();
Intent notificationIntent = new Intent(this, SettingsActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("Foreground Service")
.setContentText(input)
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentIntent(pendingIntent)
.build();
startForeground(1, notification);
// stopSelf();
return START_NOT_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
if (mClipboardManager != null) {
mClipboardManager.removePrimaryClipChangedListener(mOnPrimaryClipChangedListener);
}
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel serviceChannel = new NotificationChannel(
CHANNEL_ID,
"Foreground Service Channel",
NotificationManager.IMPORTANCE_DEFAULT
);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(serviceChannel);
}
}
private ClipboardManager.OnPrimaryClipChangedListener mOnPrimaryClipChangedListener =
new ClipboardManager.OnPrimaryClipChangedListener() {
#Override
public void onPrimaryClipChanged() {
ClipData clip = mClipboardManager.getPrimaryClip();
String textClipBoard = clip.getItemAt(0).getText().toString();
Toast.makeText(AutoDownloadService.this, textClipBoard, Toast.LENGTH_SHORT).show();
}
};
}
Manifest
<service
android:name=".services.AutoDownloadService"
android:exported="false"
android:permission="android.permission.BIND_JOB_SERVICE" />
and add finally uses permission
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
I think you should use Intent Service instead of service.
what you can do is if system shutdown your service you can again trigger it after sometime using alarm manger.
As stated in documentation
While an app is in the foreground, it can create and run both
foreground and background services freely. When an app goes into the
background, it has a window of several minutes in which it is still
allowed to create and use services. At the end of that window, the app
is considered to be idle. At this time, the system stops the app's
background services, just as if the app had called the services'
Service.stopSelf() methods.
So, you solution is to run foreground service on Android >= 8.0 and do something like this
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(Intent(this, AutoDownloadService.class))
} else {
startService(Intent(this, AutoDownloadService.class))
}
}
First of all, you should not do this.
Monitoring clipboard in background is not something right.
Android 8 added some protection on this, so you should run as foreground services, to let the end user aware your app is monitoring the clipboard.
Anyway clipboard access only available to default IME from Android 10. So, your apps will not work in Android 10.
This example of my code is currently working fine but I have problems with Chinese mobile
Tested on mobile: Xiaomi 7
public class AutoDownloadService extends IntentService {
private ClipboardManager mClipboardManager;
public AutoDownloadService() {
super("AutoDownloadService");
}
#Override
public void onCreate() {
super.onCreate();
mClipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
mClipboardManager.addPrimaryClipChangedListener(mOnPrimaryClipChangedListener);
}
#Override
protected void onHandleIntent(Intent intent) {
Toast.makeText(this, " service started.", Toast.LENGTH_SHORT).show();
}
private final ClipboardManager.OnPrimaryClipChangedListener mOnPrimaryClipChangedListener =
new ClipboardManager.OnPrimaryClipChangedListener() {
#Override
public void onPrimaryClipChanged() {
ClipData clip = mClipboardManager.getPrimaryClip();
String textClipBoard = clip.getItemAt(0).getText().toString();
if (textClipBoard.startsWith("https://www.instagram.com/")) {
Toast.makeText(AutoDownloadService.this, textClipBoard, Toast.LENGTH_SHORT).show();
}
}
};
}
And in the file Manifast.xml This is how it is defined
<service android:name="com.amirhf.inatasave.services.AutoDownloadService" />
When you swipe the app from recent apps, few Chinese OEMs force stop the apps.
Once the App is force stopped, you can't post notifications, start services, receive broadcasts etc.
The only work around is : Your app should be whitelisted, added in
auto start list. Apps like WhatsApp, Facebook etc are already added by
the OEMs in the list.
This blog talks about similar problem of not receiving notifications when App is force stopped. https://hackernoon.com/notifications-in-android-are-horribly-broken-b8dbec63f48a
You can take a similar approach where you can have a "Troubleshoot" section where you can educate the user to add your app in auto start list.
If by any chance your app is very popular you can get in touch with
Chinese Manufacturer and request them to get your app whitelisted but
they do it for very popular apps. For example in my experience
Microsoft and Hike Messenger got it done for their apps.
I didn't quite understand if you're messing with file download or anything else. But I guess you're not going the right way. So here's what I may share.
From https://developer.android.com/training/scheduling/wakelock
If your app is performing long-running HTTP downloads, consider using DownloadManager.
If your app is synchronizing data from an external server, consider creating a sync adapter.
If your app relies on background services, consider using JobScheduler or Firebase Cloud Messaging to trigger these services
at specific intervals.
Also note that if you just have a task that has to done often, use JobIntentService. Its compatible with Oreo and versions below it:
Helper for processing work that has been enqueued for a job/service.
When running on Android O or later, the work will be dispatched as a
job via JobScheduler.enqueue. When running on older versions of the
platform, it will use Context.startService.
On Oreo and above versions there are limitations helping the device save resources (battery, ram...) and even when using JobIntentService you must consider them; otherwise your app may be recognized as Battery Draining App.
If what you're about to do is heavy and is important enough to be shown in notification bar, do it using ForegroundService. So that it will be taken more seriously by android system and chances of it being killed gets fewer.
Try using WorkManager it's a JetPack Library.
Advantages:
Ensures task execution, even if the app or device restarts (Guaranteed Execution)
You don’t need to write device logic to figure out what capabilities the device has and choose an appropriate API; instead, you can just hand your task off to WorkManager and let it choose the best option. It is a wrapper on all the above concepts.
Uses JobScheduler on devices with API 23+
Uses a combination of BroadcastReceiver + AlarmManager on devices with API 14-22
Ref : WorkManager Docs
Ref : Medium Article
Ref : Medium Article(1)
[Update] - stable version is out WorkManager

System.exit(0) in Android Wear Watchface?

I've read that using System.exit(0) is frowned upon when it comes to Java and Android, but so far I can find no alternative for what I'm trying to accomplish. To be clear, this is for a Watchface, which is simply a service extending CanvasWatchFaceService. I cannot call finish() in this case. I've also tried stopService and startService with no success.
The issue I'm trying to solve: It's well known that changing timezones on your device will not be reflected on a watchface unless it is restarted. In my testing, I found that System.currentTimeMillis() quite literally does not respond to timezone changes in Android Wear. The watchface must be restarted in order for it to show the correct time after a timezone change.
So I built a system with the following code:
private final BroadcastReceiver timeChangeReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (!restarting) {
if (action.equals(Intent.ACTION_TIMEZONE_CHANGED)) {
if (upSeconds >= 15) {
System.exit(0);
} else {
restarting = true;
int delay = ((15 - upSeconds) * 1000);
new CountDownTimer(delay, delay) {
#Override
public void onTick(long millisUntilFinished) { }
#Override
public void onFinish() {
System.exit(0);
}
}.start();
}
}
}
}
};
The delay is in case a user triggers a time zone change more frequently than 15 seconds at a time. Android Wear seems to detect system exits that are too frequent and replace the watchface with the "Simple" watchface.
It seems to work great, Android Wear automatically boots the watchface back up on exit. But I would eventually like to put this app on the Google Play Store, so I thought I should make sure I'm not playing with fire here.
I can't believe I went through all that work when the proper solution was so simple. Thanks ianhanniballake for the link!
After looking at the Analog Watchface Sample, I found that all I needed to do was use mCalendar.setTimeZone(TimeZone.getDefault());. In many places I was directly comparing the time in milliseconds fetched with long now = System.currentTimeMillis();, so I simply did a now = mCalendar.getTimeInMillis() to take care of that.
Now the watchface changes time properly when the timezone is changed. I guess the other watchfaces I downloaded did not properly handle this!

Read current running activity from service

I have created an app that runs a service to read which app/activity user have opened and using it at the current time. The problem is that the service reads only the launcher application. It doesn't return me the cirrently open app/activity. Can you help? The code i write is below. Thanks in advance.
#Override
public void onStart(Intent intent, int startId) {
// For time consuming an long tasks you can launch a new thread here...
Toast.makeText(this, " Service Started", Toast.LENGTH_LONG).show();
Runnable runable = new Runnable() {
public void run() {
try{
ActivityManager am2 = (ActivityManager) getSystemService(Activity.ACTIVITY_SERVICE);
String packageName = am2.getRunningTasks(1).get(0).topActivity
.getPackageName();
Log.w("RunningTask", packageName);
handler.postDelayed(this, 8000);
}
catch (Exception e){
}
}
};
handler.postDelayed(runable, 8000);
}
If you use Android 5.0 or above, getRunningTasks() is deprecated and will only return a small subset, including the caller's own tasks, and possibly some other tasks such as home.
You may check out getRunningAppProcesses() which worked before Android 5.1.1. See Cannot get foreground activity name in Android Lollipop 5.0 only and Android 5.1.1 and above - getRunningAppProcesses() returns my application package only
Example using getRunningAppProcesses:
ActivityManager am2 = (ActivityManager) getSystemService(Activity.ACTIVITY_SERVICE);
String processName = am2.getRunningAppProcesses().get(0).processName;
Log.w("Running process", processName);

Phonegap - communicating between plugin and service

I have created a plugin that starts a service and this is working fine. However I wish to be able to send variables to the running service from the plugin, and to get variables out of the service. I have researched broadcast/receivers and binding but haven't be able to to get any examples working with the code structure I am using below. Does anyone have any tips? I'm new to android development and pretty new to Java (but not programming) so there is a conceptual leap that I haven't quite got yet.
Plugin
public class IOIOconnect extends CordovaPlugin {
private Context thiscontext;
private Intent ioioService;
// Handle calls from Javascript
#Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
// Call from javascript to startup the IOIO service
if (action.equals("ioioStartup")) {
this.ioioStartup(callbackContext);
return true;
}
}
// Initialise IOIO service (Called from Javascript)
private void ioioStartup(CallbackContext callbackContext) {
// Initialise the service variables and start it it up
thiscontext = this.cordova.getActivity().getApplicationContext();
ioioService = new Intent(thiscontext, HelloIOIOService.class);
ioioService.putExtra("loadinterval", 800); // Set LED flash interval
thiscontext.startService(ioioService);
}
}
Service
public class HelloIOIOService extends IOIOService {
private int interval = 100;
#Override
public IBinder onBind(Intent intent) {
return null;
}
// USUAL IOIO SERVICE STUFF
#Override
public void onStart(Intent intent, int startId) {
// Service has been started
super.onStart(intent, startId);
// IOIO When service is started load external vars (if set)
int loadinterval = intent.getIntExtra("loadinterval", -1);
if(loadinterval>=0){ interval = loadinterval; }
// Native IOIO stuff
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (intent != null && intent.getAction() != null && intent.getAction().equals("stop")) {
// User clicked the notification. Need to stop the service.
nm.cancel(0);
stopSelf();
} else {
// Service starting. Create a notification.
Notification notification = new Notification(
R.drawable.ic_launcher, "IOIO service running",
System.currentTimeMillis());
notification
.setLatestEventInfo(this, "IOIO Service", "Click to stop",
PendingIntent.getService(this, 0, new Intent(
"stop", null, this, this.getClass()), 0));
notification.flags |= Notification.FLAG_ONGOING_EVENT;
nm.notify(0, notification);
}
}
}
I had exact same problem: writing plugin for Salesforce Mobile SDK (based on Cordova 2.3.0).
My case: plugin and app is different android projects.
The solutions, is that you have to publish the Service in AndroidManifest.xml of the main (app) project. Remember to sign it with full qualified path and as exported, like this:
<service
android:name="full.qualified.path.to.Service"
android:exported="true">
</service>
I managed to create a working plugin which is used in this project:
https://github.com/opensystemsassociation/southendtransportresearch/tree/master/phonegap
The code's not tidy as its still in development, but the 'hacky' approach works fine so hopefully it will help someone along the line a bit

orientation change sound

Good morning,
We have developed an android app, and I have been charged with finding out how to remove the undesired behavior of a notification sound every time that the screen orientation changes. Obviously this behavior only exists on devices running OS version 3.2.3 or later.
I have read several posts that indicate that this can be turned off by unchecking USB Debugging in the Settings --> Developer options, however this option is not checked and none of the other apps that are on any of our Android devices make this notification sound upon orientation change.
The application does require there to be a notification when a "message is received" (the app connects to a webservice and gets new messages from the service every so often). So this would rule out any solution that disabled notifications.
Thus far, I have tried several potential solutions:
1) When a message is received, instantiate a new NotificationManager, and after the notification is sounded, destroy the NotificationManager.
if(MessageReceived == true) {
String ns = Context.NOTIFICATION_SERVICE;
messageNotifyManager = (NotificationManager) getSystemService(ns);
}
showNotification();
messageNotifyManager = null;
2) I realize that an orientation change is essentially the view being destroyed and re-created. I put set a flag in the initial onCreate method and checked to see if that flag had value before recreating the Notification Manager.
public static int Flag = 0;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(Flag == 0) {
String ns = Context.NOTIFICATION_SERVICE;
messageNotifyManager = (NotificationManager) getSystemService(ns);
Flag = 1;
}
}
3) In the application's main class, I created a public OrientationEventListener property and then set its value in the onCreate method, disabling it immediately. When that didn't disable the sound I tried disabling the property in every class that referenced the application's main class.
public OrientationEventListender listener;
#Override
public void onCreate() {
super.onCreate();
appContext = getApplicationContext();
GetPreferences();
//...
listener = new OrientationEventListener(appContext){
public void onOrientationChanged(int Orientation){
}
};
listener.disable();
}
Now, as you can probably tell, I am very new to Android development. I assume that this solution is something so simple that everyone knows, and that is why there are no answers anywhere handy on the web. But any help with this simple problem would be greatly appreciated. Thanks!
This issue was solved by modifying the AndroidManifest, adding the following tag to each activity: android:configChanges="orientation"

Categories

Resources