0

我正在開發應用程序更衣室應用程序。如果在kitkat版本設備應用程序鎖定中安裝了這個 應用程序,並且它的 工作正常。在棉花糖|(6.0)版本設備最初 應用程序正在工作,如果我們重新啓動設備應用程序鎖定不是 工作,鎖定的應用程序打開時不詢問任何密碼。 如果我們只是進入應用程序(應用程序鎖定應用程序),並退出應用程序其 工作完美。這次它要求密碼。重新啓動設備後,我的儲物櫃應用程序無法在Android棉花糖設備上工作

public class StartupServiceReceiver extends BroadcastReceiver { 
@Override 
public void onReceive(Context context, Intent intent) { 
    if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){ 
     if (AppLockerPreference.getInstance(context).isAutoStart()){ 
      if (AppLockerPreference.getInstance(context).isServiceEnabled()){ 
       context.startService(new Intent(context, DetectorService.class)); 
      }else{ 
       AppLockerPreference.getInstance(context).saveServiceEnabled(false); 
      } 
     } 
     return; 
    }else if (AppLockerPreference.getInstance(context).isServiceEnabled()){ 
     context.startService(new Intent(context, DetectorService.class)); 
    } 
} 
} 


public class AppLockerPreference implements OnSharedPreferenceChangeListener { 
Context mContext; 




public boolean isAutoStart() { 
    Log.d("AMK","AppLockerPreference1--->"+mApplicationList.length); 
    return mAutoStart; 
} 

public boolean isServiceEnabled() { 
    Log.d("AMK","AppLockerPreference2--->"+mApplicationList.length); 
    return mServiceEnabled; 
} 
public void saveServiceEnabled(boolean serviceEnabled) { 
    mServiceEnabled = serviceEnabled; 
    mPref.edit().putBoolean(PREF_SERVICE_ENABLED, mServiceEnabled); 
} 
public String[] getApplicationList() { 


    Log.d("AMK","AppLockerPreference3--->"+mApplicationList.length); 
    return mApplicationList; 
} 
public void saveApplicationList(String[] applicationList) { 
    mApplicationList = applicationList; 
    String combined = ""; 
    for (int i=0; i<mApplicationList.length; i++){ 
     combined = combined + mApplicationList[i] + ";"; 
    } 
    mPref.edit().putString(PREF_APPLICATION_LIST, combined).commit(); 
} 

private static final String PREF_SERVICE_ENABLED = "service_enabled"; 
private static final String PREF_APPLICATION_LIST = "application_list"; 
private static final String PREF_AUTO_START = "start_service_after_boot"; 
private static final String PREF_PASSWORD = "password"; 

/** 
* Section for singleton pattern 
*/ 
private SharedPreferences mPref; 
private AppLockerPreference(Context context) { 
    mPref = PreferenceManager.getDefaultSharedPreferences(context); 
    mPref.registerOnSharedPreferenceChangeListener(this); 
    reloadPreferences(); 
} 
private void reloadPreferences() { 
    mServiceEnabled = mPref.getBoolean(PREF_SERVICE_ENABLED, true); 

    mApplicationList = mPref.getString(PREF_APPLICATION_LIST, "").split(";"); 



    Log.d("AMK","selectd apps--->"+mApplicationList); 



    mAutoStart = mPref.getBoolean(PREF_AUTO_START, true); 
    mPassword = mPref.getString(PREF_PASSWORD, "2468"); 
    if (mPref.getBoolean("relock_policy", true)){ 
     try{ 
      mRelockTimeout = Integer.parseInt(mPref.getString("relock_timeout", "-1")); 
     }catch(Exception e){ 
      mRelockTimeout = -1; 
     } 
    }else{ 
     mRelockTimeout = -1; 
    } 
} 

private static AppLockerPreference mInstance; 
public static AppLockerPreference getInstance(Context context){ 
    return mInstance == null ? 
      (mInstance = new AppLockerPreference(context)) : 
       mInstance; 
} 

private boolean mServiceEnabled, mAutoStart; 
private String[] mApplicationList; 
private String mPassword; 
private int mRelockTimeout; 

public int getRelockTimeout(){ 
    return mRelockTimeout; 
} 

public String getPassword() { 
    return mPassword; 
} 
public void savePassword(String password) { 
    mPassword = password; 
    mPref.edit().putString(PREF_PASSWORD, password); 
} 

public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, 
     String key) { 
    reloadPreferences(); 
} 
} 

你能幫我嗎。

+0

你有''<使用許可權的android: name =「android.permission.BOOT_COMPLETED」/> ''在你的清單文件中? – Grisgram

+0

是的,我有這個權限。 – Narendra

+0

給我們您的清單 –

回答

0

檢查設備是否棉花糖和授予權限

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.MARSHMALLOW) { 
    // only for marshmallow and newer versions 
} 
+0

不工作...重新啓動設備獲取鎖定的應用程序長度後。如果我們只是進入應用程序(應用程序鎖定應用程序),並退出應用程序的工作完美這次它要求密碼。 – Narendra

0

以下的Android 6.0(棉花糖),其只需要提清單檔案中的所有權限,而在棉花糖及以上版本的所有的權限被授予由用戶,你需要顯示彈出權限。 一樣,如果你想使用下面的權限,然後做這樣的編碼:

public void checkForMarshmallowPermission() { 

    for (int i = 0; i < 3; i++) { 
     switch (i) { 
      case 0: 
       isPermissionGiven(Manifest.permission.ACCESS_COARSE_LOCATION, PERMISSION_LOCATION_COARSE); 
       break; 

      case 1: 
       isPermissionGiven(Manifest.permission.ACCESS_FINE_LOCATION, PERMISSION_LOCATION_FINE); 
       break; 

      case 2: 
       isPermissionGiven(Manifest.permission.READ_PHONE_STATE, PERMISSION_READ_PHONE_STATE); 
       break; 
     } 
    } 

} 


private boolean isPermissionGiven(String permissionName, int requestCode) { 
     LoggerUtility.e(TAG, "isPermissionGiven :-" + permissionName); 
     if (ContextCompat.checkSelfPermission(LoginActivity.this, 
       permissionName) 
       != PackageManager.PERMISSION_GRANTED) { 
      ActivityCompat.requestPermissions(YourActivity.this, 
        new String[]{permissionName}, 
        requestCode); 
      return false; 
     } 
     return true; 
    } 

而且覆蓋在你的活動onRequestPermissionsResult方法:

@Override 
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { 
     LoggerUtility.e(TAG, "onRequestPermissionsResult :-" + requestCode); 
     switch (requestCode) { 
      case PERMISSION_LOCATION_FINE: 
       if (grantResults.length > 0 
         && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

        // permission was granted, yay! Do the 
        // contacts-related task you need to do. 
        isPermissionGiven(Manifest.permission.READ_PHONE_STATE, PERMISSION_READ_PHONE_STATE); 

       } else { 
        // permission denied, boo! Disable the 
        // functionality that depends on this permission. 
       } 
       break; 

      case PERMISSION_LOCATION_COARSE: 
       if (grantResults.length > 0 
         && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

        // permission was granted, yay! Do the 
        // contacts-related task you need to do. 
        isPermissionGiven(Manifest.permission.READ_PHONE_STATE, PERMISSION_READ_PHONE_STATE); 

       } else { 

        // permission denied, boo! Disable the 
        // functionality that depends on this permission. 
       } 
       break; 

      case PERMISSION_READ_PHONE_STATE: 
       if (grantResults.length > 0 
         && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

        // permission was granted, yay! Do the 
        // contacts-related task you need to do. 


       } else { 

        // permission denied, boo! Disable the 
        // functionality that depends on this permission. 
       } 
       break; 
     } 
    } 
+1

在這個應用程序中,我只有和重新啓動設備後鎖定了應用程序的長度。如果我們只是進入應用程序(應用程序鎖定應用程序),並退出應用程序的工作完美這次它要求密碼。 – Narendra

+0

「重新啓動設備獲取鎖定的應用程序長度後」 - 沒有得到這一行。幫助我簡要介紹一下。 –

+0

我們鎖定了應用程序,重新啓動設備後即將到來。 Log.d( 「AMK」, 「AppLockerPreference1 --->」 + mApplicationList.length); – Narendra

相關問題