2016-12-26 50 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

更新了答案,請檢查並讓我知道 –