【问题标题】:After re-starting device my locker application is not working on the Android marshmallow device重新启动设备后,我的储物柜应用程序无法在 Android 棉花糖设备上运行
【发布时间】:2017-04-03 07:30:49
【问题描述】:

我正在开发应用储物柜应用程序。如果安装了这个 kitkat版设备应用程序锁定及其应用 工作完美。在 marshmallow|(6.0) 版本设备中最初 应用程序正在运行,如果我们重新启动设备应用程序锁不 工作时,锁定的应用程序无需询问任何密码即可打开。 如果我们只是进入应用程序(应用程序锁定应用程序)并从应用程序退出 工作完美。那个时候它会询问密码。

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

你能帮帮我吗?

【问题讨论】:

  • 你的清单文件中有&lt;uses-permission android:name="android.permission.BOOT_COMPLETED"/&gt; ?
  • 是的。我有这个权限。
  • 给我们你的清单

标签: android android-service sharedpreferences android-6.0-marshmallow android-broadcastreceiver


【解决方案1】:

检查设备是否是棉花糖并授予权限

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

【讨论】:

  • 不工作...重新启动设备后获得锁定的应用程序长度。如果我们只是进入应用程序(应用程序锁定应用程序)并从应用程序退出它的工作完美。那个时候它会询问密码。
【解决方案2】:

在 Android 6.0 (MARSHMALLOW) 以下只需要在清单文件中提及所有权限,而在棉花糖及以上版本中,所有权限均由用户授予,因此您需要显示权限弹出窗口。 就像如果你想使用以下权限,然后像这样进行编码:

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

【讨论】:

  • 在这个应用程序中,我只有 和重启设备后获得锁定的应用程序长度。如果我们只是进入应用程序(应用程序锁定应用程序)并从应用程序退出它的工作完美。那个时候它会询问密码。
  • “重新启动设备后获得锁定的应用程序长度” - 没有得到这一行。帮我简要介绍一下。
  • 我们锁定了哪些应用程序,重启设备后该长度即将到来。 Log.d("AMK","AppLockerPreference1--->"+mApplicationList.length);
  • 嘿,你能分享你的服务代码,以便我可以在这里调试问题,因为在我的例子中,我制作了服务 STICKY 并配置了警报和作业调度程序以确保服务正常运行。还要检查您的服务是否在重新启动后启动。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多