Determining the current foreground application from a background task or service

With regards to “2. How my background application can know what the application currently running in the foreground is.” Do NOT use the getRunningAppProcesses() method as this returns all sorts of system rubbish from my experience and you’ll get multiple results which have RunningAppProcessInfo.IMPORTANCE_FOREGROUND. Use getRunningTasks() instead This is the code I use in my … Read more

Android onCreate or onStartCommand for starting service

onCreate() is called when the Service object is instantiated (ie: when the service is created). You should do things in this method that you need to do only once (ie: initialize some variables, etc.). onCreate() will only ever be called once per instantiated object. You only need to implement onCreate() if you actually want/need to … Read more

PendingIntent does not send Intent extras

Using PendingIntent.FLAG_CANCEL_CURRENT not a good solution because of inefficient use of memory. Instead use PendingIntent.FLAG_UPDATE_CURRENT. Use also Intent.FLAG_ACTIVITY_SINGLE_TOP (the activity will not be launched if it is already running at the top of the history stack). Intent resultIntent = new Intent(this, FragmentPagerSupportActivity.class). addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); resultIntent.putExtra(FragmentPagerSupportActivity.PAGE_NUMBER_KEY, pageNumber); PendingIntent resultPendingIntent = PendingIntent.getActivity( this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT ); Then: … Read more

What is the difference between an IntentService and a Service? [duplicate]

Service is a base class of service implementation. Service runs on the application’s main thread which may reduce the application performance. Thus, IntentService, which is a direct subclass of Service is available to make things easier. The IntentService is used to perform a certain task in the background. Once done, the instance of IntentService terminates … Read more

Difference between Service, Async Task & Thread?

Probably you already read the documentation description about them, I won’t repeat them, instead I will try to give answer with my own words, hope they will help you. Service is like an Activity but has no user interface. Probably if you want to fetch the weather for example you won’t create a blank activity … Read more

Android – Listen For Incoming SMS Messages

public class SmsListener extends BroadcastReceiver{ private SharedPreferences preferences; @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub if(intent.getAction().equals(“android.provider.Telephony.SMS_RECEIVED”)){ Bundle bundle = intent.getExtras(); //—get the SMS message passed in— SmsMessage[] msgs = null; String msg_from; if (bundle != null){ //—retrieve the SMS message received— try{ Object[] pdus = (Object[]) bundle.get(“pdus”); msgs = … Read more

Permission Denial: startForeground requires android.permission.FOREGROUND_SERVICE

This will happen if you have set targetSdkVersion = 28 (Android 9 / Pie) or above and have not declared the usage of the FOREGROUND_SERVICE permission. From the migration notes for Android 9: Apps wanting to use foreground services must now request the FOREGROUND_SERVICE permission first. This is a normal permission, so the system automatically … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)