Pregunta

i am experiencing problem in creating a dialog in device admin receiver.

Error: unable to add window--token null is not for an application.

i just want to create a dialog which verifies a password stored in my preferences, if it matches then you can disable device admin check box else it should exit..

any ideas

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

No hay solución correcta

Otros consejos

You cannot show a dialog on non-activity context. But, You can design a transparent activity to make it look like dialog and call it from Device Admin as following:

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

context declared in the code above is an attribute for any action method inside DeviceAdminReceiver class. For better understanding check following:

@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);
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top