I check by the process name. but I want to search by application name. How can I do that ? For example, facebook, whatsapp, instagram etc. to check if the applications are running? What string value should I send for this? isnamedprocessrunning ( "com.example.processlis");
will take the application name as string and will find it accordingly
MyService.java
public class MyService extends Service {
@Override
public void onCreate() {
super.onCreate();
isNamedProcessRunning("com.example.processlistener");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
@Nullable
@Override
public IBinder onBind(Intent ıntent) {
return null;
}
boolean isNamedProcessRunning(String processName) {
if (processName == null)
return false;
ActivityManager manager =
(ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> processes = manager.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo process : processes) {
if (processName.equals(process.processName)) {
Toast.makeText(this, "Same", Toast.LENGTH_SHORT).show();
return true;
}
}
Toast.makeText(this, "Different", Toast.LENGTH_SHORT).show();
return false;
}
}
MainActivity.java
Intent startIntent = new Intent(this,MyService.class);
startService(startIntent);