How to Start Service Intent

Start service in Android

Probably you don't have the service in your manifest, or it does not have an <intent-filter> that matches your action. Examining LogCat (via adb logcat, DDMS, or the DDMS perspective in Eclipse) should turn up some warnings that may help.

More likely, you should start the service via:

startService(new Intent(this, UpdaterServiceManager.class));

Start context.startService(intent) on Android Pie

I faced the same Problem with a small Service which i didn't want to change to a ForegroundService. Then i found this workaround provided by Google for starting services in onResume:

ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> runningAppProcesses = activityManager.getRunningAppProcesses();
if (runningAppProcesses != null) {
int importance = runningAppProcesses.get(0).importance;
// higher importance has lower number (?)
if (importance <= ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
//start your service the same way you did before in here
startService(serviceIntent);
}
}

The issue has been addressed in future Android release.

There is a workaround to avoid application crash. Applications can get
the process state in Activity.onResume() by calling
ActivityManager.getRunningAppProcesses() and avoid starting Service if
the importance level is lower than
ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND. If the
device hasn’t fully awake, activities would be paused immediately and
eventually be resumed again after its fully awake.

How to start a service from an activity in android?

If you can start your service then use

public class YourService extends Service {

public static boolean isRunning = false;

@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
isRunning = true;
return START_STICKY;
}
}

if(!YourService.isRunning){
Intent intent = new Intent(this, YourService.class);
startService(intent);

Date date = new Date(System.currentTimeMillis());
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm aa");
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date dt = new Date(System.currentTimeMillis());
String strTime = sdf.format(dt);
String strDate = dateFormat.format(date);
}

and also define

<service android:name="com.mypackagename.YourService"></service>

in manifest.

If you want check service running or not then use one static variable in service and check before start your service. If that false then start service.

Can I get the Intent to start a Service by Service name?

yes you can start any service with componentname and package name.

Intent intent=new Intent();
intent.setComponent(new ComponentName("service packagename", "service componentname"));
bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);

thats it...

Android AIDL - Unable to start service Intent

I did more searching and finally found the answer... All credits go to the SO answer here:

https://stackoverflow.com/a/55712326/3718584

TLDR:

Had to change the intent from implicit to explicit due to API 21

intent.setClassName("com.rchan.nveeapplication","com.rchan.nvee_sdk.detectedactivity.DetectedActivityService")


Related Topics



Leave a reply



Submit