Esta mañana me he tenido que pelear con un AlertDialog que podía hacer dos opciones, por un lado mandar el documento adjunto por mail o por otro lado abrir el pdf. El problema era que llamaba al AlertDialog desde fuera de la Activity por lo que he tenido que usar
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
y añadir un permiso en el AndroidManifest.xml
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
El método completo que he usado es el siguiente:
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
alertDialog.setTitle("Documento correctamente generado");
alertDialog.setMessage("¿Que desea hacer con el documento?");
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL,"Mandar por email" ,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Log.i("Print.java", "Button email");
Uri path = Uri.fromFile(pdfFile);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("message/rfc822");
Uri uri = Uri.fromFile(pdfFile);
intent.putExtra(Intent.EXTRA_SUBJECT, "Texto");
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
context.startActivity(intent);
}
});
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Imprimir",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Log.i("Print.java", "Button imprimir");
Uri path = Uri.fromFile(pdfFile);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
context.startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(context,"No Application Available to View PDF", Toast.LENGTH_SHORT).show();
}
}
});
alertDialog.show();
No funciona para dispositivos nexus 5 y superiores
ResponderEliminar