0

我在设备管理员接收器中创建对话框时遇到问题。

错误:无法添加窗口--token null 不适用于应用程序。

我只想创建一个对话框来验证存储在我的首选项中的密码,如果匹配,那么您可以禁用设备管理员复选框,否则它应该退出..

有任何想法吗

public static class DeviceAdminSampleReceiver extends DeviceAdminReceiver {
        void showToast(Context context, String msg) {
            //String status = context.getString(R.string.admin_receiver_status, msg);
          //  Toast.makeText(context, status, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onEnabled(Context context, Intent intent) {

            Toast.makeText(context, "Enabled", Toast.LENGTH_SHORT).show();
        }



        @Override
        public void onDisabled(Context context, Intent intent) {
            showMyDialog(context);
            Toast.makeText(context, "Disabled", Toast.LENGTH_SHORT).show();
        }


        public void showMyDialog(final Context context){
            AlertDialog dialog = null;
            SharedPreferences prefs= context.getSharedPreferences("MainPrefs", 0);
            final String tempPass = prefs.getString("password", "");

            LayoutInflater factory = LayoutInflater.from(context);
            final View deviceDialog = factory.inflate(R.layout.device_receiver_layout, null);
           final EditText input = (EditText) deviceDialog.findViewById(R.id.dialog_editText);
            Button done = (Button) deviceDialog.findViewById(R.id.button2);
            Button cancel = (Button) deviceDialog.findViewById(R.id.button1);

            AlertDialog.Builder builder = new AlertDialog.Builder(context);

            builder.setView(deviceDialog);
            builder.setCancelable(false);
            builder.setTitle("Ultimate SMS Password");
            //builder.show();
            dialog = builder.create();
            dialog.show();
            done.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    if(tempPass.regionMatches(0, input.getText().toString(), 0, tempPass.length()))
                    {
                        Toast.makeText(context, "Matches", Toast.LENGTH_SHORT).show();

                    }else{
                        Toast.makeText(context, "Not Matches", Toast.LENGTH_SHORT).show();
                        //finish();

                }
                }
            });

            cancel.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    Toast.makeText(context, "Exit.", Toast.LENGTH_SHORT).show();
                }
            });     
        }
4

1 回答 1

0

您不能在非活动上下文中显示对话框。但是,您可以设计一个透明的活动,使其看起来像对话框,并从设备管理员调用它,如下所示:

Intent intent = new Intent(context, YourActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);

上面代码中声明的上下文是类内任何操作方法的属性DeviceAdminReceiver。为了更好地理解检查以下内容:

@Override
public CharSequence onDisableRequested(Context context, Intent intent) {

    Intent intentcl = new Intent(context, OperationActivity.class);
    intentcl.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(intentcl);
    return super.onDisableRequested(context, intent);
}
于 2015-10-13T10:57:18.123 回答