2016-12-26 60 views
0

我要开始在Android中,检查哪一个应用程序在后台不断运行的服务。如果前台应用程序在我的应用程序的数据库中,那么它会打开一个密码屏幕来输入密码。开始不断运行服务

我已经取得了什么是我的服务,只要在应用程序运行,但是当我停止应用程序(从以前的应用程序中删除),那么我的服务停止运行。无需输入密码即可打开锁定的应用程序。我在互联网上尝试了很多解决方案,但似乎没有任何工作。请帮我,

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); 
    } 

} 

}

我可以提供AppChecker类的代码,如果你想要的。请让我知道我应该怎么做才能达到预期的效果。我会非常感激你。

+0

您是否尝试过在服务的OnCreate移动你的代码,因为当服务重新创建,它总是调用ONC reate –

+0

你在测试什么设备? –

+0

嗨,谢谢大卫对你的回应。我正在使用运行android 5.1的HUAWEI TAG-L21进行测试。当应用程序从最近的应用程序中移除时,该服务停止。 –

回答

0

对于开始不断运行的服务,你必须在清单来定义服务等为

<service 
     android:name=".service.youservice" 
     android:exported="true" 
     android:process=":ServiceProcess" /> 

通过使用此,您的服务将在ServiceProcess的另一个进程中运行。

有几个步骤,定义长时间运行的服务为

  • onStartCommand()返回START_STICKY
  • 服务
  • 中的onDestroy自我start()方法创建一个后台程序服务
  • 创建本地守护程序进程(斤)
+0

我已经声明与您在清单中描述的相同,但没有任何帮助。其他人,如果你可以更详细地解释。 –

+0

我找到了一些东西。该服务在棒棒糖下面的设备上运行,但棒棒糖无论我做什么都会停止。请帮助我解决这个问题。 –

0

如果服务由您的应用程序启动时,它会尽快的应用程序就会被杀死运行你的应用过程,也是如此的服务。你可以做的是您的服务onTaskRemoved方法,把这个

Intent intent = new Intent("com.whatever.restartservice"); 
sendBroadcast(intent); 

,并设置广播接收器在服务本身,并在的onReceive,重新启动该服务。我已经测试过,每次应用程序被杀时它都会正常工作。

编辑: 试试这个

@Override public void onTaskRemoved(Intent rootIntent){ 
    Intent restartServiceIntent = new Intent(getApplicationContext(), this.getClass()); 

    PendingIntent restartServicePendingIntent = PendingIntent.getService(
     getApplicationContext(), 1, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT); 
    AlarmManager alarmService = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
    alarmService.set(ELAPSED_REALTIME, elapsedRealtime() + 1000, 
     restartServicePendingIntent); 

    super.onTaskRemoved(rootIntent); 
} 

这将重新启动你的服务,不管它如何被杀害

+0

如果我在onTaskRemoved本身启动服务,而不是发送和接收广播,该怎么办? –

+0

我已经完成了你所说的,但服务不会重新开始。广播发送但没有任何反应。 –

+0

更新了答案,请检查并让我知道 –