I want to start an ever running service in android that checks which application is in background. If the foreground application is in the database of my app then it opens a password screen to enter the password.
What I've achieved is that my service runs as long as the application is running but when I stop the application(remove it from previous apps) then my service stops. I can open the locked apps without entering password. I tried a lot of solutions in the internet but none seems to work. Please help me,
package services;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;
import com.antzion.salmanali.lockapp.FeedReaderContract;
import com.antzion.salmanali.lockapp.FeedReaderDBHelper;
import com.antzion.salmanali.lockapp.PasswordSet;
import com.antzion.salmanali.lockapp.Pattern;
/**
* Created by Salman Majid Ali on 12/26/2016.
*/
public class TestService extends Service {
String [] names;
FeedReaderDBHelper helper;
private AppChecker appChecker;
private String currentLocked = "";
Detector detector;
public TestService()
{
if(Utils.postLollipop())
detector = new LollipopDetector();
else
detector = new PreLollipopDetector();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(getApplicationContext(), "service Start", Toast.LENGTH_SHORT).show();
System.out.println("StartRemove");
helper = new FeedReaderDBHelper(this);
names = helper.getNames();
Thread t = new Thread(new Runnable() {
@Override
public void run() {
if(!AppChecker.running)
{
startChecker();
}
}
});
t.start();
return START_STICKY;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onTaskRemoved(Intent rootIntent) {
System.out.println("Remove");
Intent restart=new Intent(getApplicationContext(),this.getClass());
restart.setPackage(getPackageName());
startService(restart);
super.onTaskRemoved(rootIntent);
}
@Override
public void onDestroy()
{
Intent restart=new Intent(getApplicationContext(),this.getClass());
restart.setPackage(getPackageName());
startService(restart);
super.onDestroy();
}
private void startChecker()
{
appChecker = new AppChecker();
appChecker
.when(getPackageName(), new AppChecker.Listener() {
@Override
public void onForeground(String packageName) {
//Toast.makeText(getBaseContext(), "Our app is in the foreground.", Toast.LENGTH_SHORT).show();
}
})
.other(new AppChecker.Listener() {
@Override
public void onForeground(String packageName) {
//System.out.println("Foreground " + packageName);
//System.out.println("In Other " + setting.getBooleanValue(PrefVars.onlock) + " " + setting.getBooleanValue(PrefVars.onLock3) +
// " " + setting.getBooleanValue(PrefVars.lockImmediately));
try {
if(currentLocked != null && !packageName.equals(currentLocked)
&& !helper.isLocked(currentLocked)
&& helper.getValue(FeedReaderContract.FeedEntry.onexit).equals("YES"))
{
Log.i("Locking ", currentLocked);
helper.lock(currentLocked);
}
if(helper.getPackageName(packageName))
{
//System.out.println(packageName + "App is in the Database");
if(helper.isLocked(packageName))
{
System.out.println("UnLocking " + packageName);
getPassword(packageName);
currentLocked = packageName;
}
}
}catch(Exception e)
{
}
//Toast.makeText(getBaseContext(), "Foreground: " + packageName, Toast.LENGTH_SHORT).show();
}
})
.start(this);
}
private void getPassword(String pack)
{
int i = helper.getPassOrPat();
boolean pass = false;
if(i == 1)
pass = true;
else
pass = false;
if(pass)
{
Intent intent = new Intent(this, PasswordSet.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("PASS", pack);
intent.putExtra("CONFIRM", "YES");
startActivity(intent);
}
else
{
Intent intent = new Intent(this, Pattern.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("PASS", pack);
intent.putExtra("CONFIRM", "YES");
startActivity(intent);
}
}
}
I can provide AppChecker class's code if you want. please let me know what should I do to achieve the desired results. I will be very grateful to you.