I tried this example & it is working fine.
But I don't want to see print popup.
Is it possible to prevent the print popup?
Since PrintManager is final you can't override it's methods
But you can hide all System Dialogs using this
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (! hasFocus) {
Intent closeDialog = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
sendBroadcast(closeDialog);
}
}
and If you want an example for Printing document you can refer to Android-wifi-print - GitHub. This is just a prototype for printing document with the help of wifi printer.
Related
I have been looking at the new methods available for Accessibility in Android O. I ran across this new method called getAccessibilityButtonController, I am unsure precisely what it does and an intended use. I know that in Android O there is a navigation button that can be used for an accessibility service. Does this accessibility button only launch the accessibility service, or could it have other functionality within the service such as to do specific tasks? I am curious possible uses for the accessibility and the getAccessibilityButtonController methods. Thank you for your time.
It can do pretty much anything you want it to. From the android accessibility doc, the button allows you to register a callback that has an onClicked method. If you enable the button and provide said callback you can execute whatever you'd like in the context of that callback.
Edit: The android documentation has been updated so the following should no longer be necessary.
Note that if you read the doc there's currently an example that has a call to getAccessibilityButtonController() within onCreate(). This is incorrect because the controller isn't valid until onServiceConnected is called. I've modified the example below to show something that should work.
private AccessibilityButtonController mAccessibilityButtonController;
private AccessibilityButtonController
.AccessibilityButtonCallback mAccessibilityButtonCallback;
private boolean mIsAccessibilityButtonAvailable;
#Override
protected void onServiceConnected() {
mAccessibilityButtonController = getAccessibilityButtonController();
mIsAccessibilityButtonAvailable =
mAccessibilityButtonController.isAccessibilityButtonAvailable();
if (!mIsAccessibilityButtonAvailable) {
return;
}
AccessibilityServiceInfo serviceInfo = getServiceInfo();
serviceInfo.flags
|= AccessibilityServiceInfo.FLAG_REQUEST_ACCESSIBILITY_BUTTON;
setServiceInfo(serviceInfo);
mAccessibilityButtonCallback =
new AccessibilityButtonController.AccessibilityButtonCallback() {
#Override
public void onClicked(AccessibilityButtonController controller) {
Log.d("MY_APP_TAG", "Accessibility button pressed!");
// Add custom logic for a service to react to the
// accessibility button being pressed.
}
#Override
public void onAvailabilityChanged(
AccessibilityButtonController controller, boolean available) {
if (controller.equals(mAccessibilityButtonController)) {
mIsAccessibilityButtonAvailable = available;
}
}
};
if (mAccessibilityButtonCallback != null) {
mAccessibilityButtonController.registerAccessibilityButtonCallback(
mAccessibilityButtonCallback, null);
}
}
if there is no internet means I'm not able to load web resources. For this reason I'm giving the toast like "Check internet connectivity". After this toast, user may enable the internet option at notification bar and comes back. When he comes back, i want to reload the activity. For this requirement, i tried
onWindowFocusChanged and onActivityReenter
override methods but these are not working properly
MyCode
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if(hasFocus){
Intent intent = new Intent(CommonActivity.this, OtherActivity.class);
startActivity(intent);
}
}
When I'm using above code, my activity reloading again and again
There is a solution which i know is not perfect but it will work.
Define a activity level veriable like this
Boolean isAlreadyFocused = false;
Then in your onFocusChanged method do like this.
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if(hasFocus && !isAlreadyFocused ){
isAlreadyFocused = true;
Intent intent = new Intent(CommonActivity.this,OtherActivity.class);
startActivity(intent);
}else{
isAlreadyFocused = false;
}
}
Check this and tell me if this does not work.
By fallowing another way (i got this idea when i saw the flipkart app) i solved this internet checking
I'm checking for the internet connection, if there is no internet means i'm redirecting to NoInternetActivity that's design looks like
When user clicks on Retry button means i'm again checking for internet. If internet was accessible means i'm allowing user to home page of my app otherwise i'm redirecting to the NoInternetActivity again
I've been working on a feature similar to Facebook's chat heads - a feature which needs to be active for our users even if they leave our app to go check email, etc. It needs to be able to create AlertDialogs via javascript - from a place that isn't impacted when the user switches Activities inside our app. It's OK for the feature to close/hide when the user leaves the app to do something else - however it must still be present when they come back.
Other than using a Service I haven't been able to think of another way to let users interact with us while they're off doing other things. When I use a service to create the webview via WindowManager the webview's JsDialogHelper detects that the context is not an activity and prevents it from showing AlertDialogs.
Any ideas would be most helpful around making this work. We need to allow AlertDialogs to pop up and be interactive for this feature to be helpful. We need the webview to stick around between activity transitions.
Is it possible to extend JsDialogHelper so I can get it to work with my code? Is there a better way to have a facebook chat heads like feature using webviews in an android app that I haven't thought of yet? I'm open to complete rewrites if it gets the user experience I'm looking for.
You can display the Dialog from the service, by setting the window type as TYPE_SYSTEM_ALERT. Remember to communicate back the action taken by the user using the JsResult instance passed to onJsAlert.
// maintain reference to JsResult instance passed to onJsAlert, in order
// communicate back the action taken by the user.
private JsResult mResult;
webView.setWebChromeClient(new WebChromeClient() {
#Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
mResult = result;
AlertDialog dialog = new AlertDialog.Builder(MyService.this)
.setTitle("Custom Dialog")
.setMessage("This is our custom dialog, not the one served by JsDialogHelper")
.setOnCancelListener(new CancelListener())
.setNegativeButton("Cancel", new CancelListener())
.setPositiveButton("Ok", new PositiveListener())
.create();
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
dialog.show();
return true;
}
});
private class CancelListener implements DialogInterface.OnCancelListener,
DialogInterface.OnClickListener {
#Override
public void onCancel(DialogInterface dialogInterface) {
mResult.cancel();
}
#Override
public void onClick(DialogInterface dialogInterface, int i) {
mResult.cancel();
}
}
private class PositiveListener implements DialogInterface.OnClickListener {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
mResult.confirm();
}
}
Add the required permissions to the manifest file:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
Alternate solution:
If the web application can be built specific to android webview, then you can create Javascript interfaces which will allow Javascript code to directly invoke the Android code which in turn displays the dialog for you. This avoids the JsDialogHelper route.
Define a javascript interface:
public class WebAppInterface {
Context context;
WebAppInterface(Context c) {
context = c;
}
// Annotation required for api >= 17
#JavascriptInterface
public void showDialog() {
AlertDialog dialog = new AlertDialog.Builder(context)
.setTitle("Custom Dialog")
.setMessage("This is our custom dialog, not the one served by JsDialogHelper")
.create();
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
dialog.show();
}
}
Bind the interface between Javascript and Android code, using addJavascriptInterface.
webView.addJavascriptInterface(new WebAppInterface(this), "Android");
Use the interface in Javascript code.
<input type="button" value="Display dialog" onClick="displayDialog()" />
<script>
function displayDialog() {
//alert("Javascript Dialog");
Android.showDialog();
}
</script>
</input>
You can't display an AlertDialog outside of an Activity.
However, you could build a custom View which looks like the dialog you want to show, and display it in the window via WindowManager.
For example:
WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT
WindowManager.LayoutParams.TYPE_PHONE,
0,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.CENTER;
windowManager.addView(yourView, params);
The code above displays your custom view in the center of the screen, on top of everything else. You could inflate the view via LayoutInflater.
You also need to add the permission in the manifest in order to display the view.
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
I suppose you can create a JavaScript Interface for two way communication between your activity and WebView. I have not tried the solution myself, but have stumbled before on that. The SO Question posted here might help you on that.
I suppose your WebView will reside in an activity too, you can use the Activity's context to pop the AlertDialog. You just need a method in the activity which you'd like to call in the webview (I'd just pass the Activity object in the addJavascriptInterface method).
Another way would be to use a Service and initiate a new Activity which implements Theme.dialog in it to be used as AlertDialog.
Let me know if it doesn't solve your problem.
I currently developing a Content Management app in Kiosk Mode. I pattern this in Surelock Kiosk Lockdown. There's an admin password in order to allow application to run inside the app. The buttons like Home, Back and Recent Task App are disabled in this app. However when I launch a certain app, Back button and Recent Task App are enabled again.
What I'd like to happen is that when the user launch an app, the Recent Task app button and Back button are disabled just like in Content Management App. I am stuck in searching and finding ways on how to do this. Please help me on how to do this. Thank you.
BTW, I'm using this code to disable Buttons:
// Disable Recent Task App
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
Log.d("Focus debug", "Focus changed!");
if (!hasFocus) {
Log.d("Focus debug", "Lost focus!");
Intent closeDialog = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
closeDialog.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
sendBroadcast(closeDialog);
}
}
// Disable Volume Buttons and Back Button
private final List hijackKeys = new ArrayList(Arrays.asList(
KeyEvent.KEYCODE_VOLUME_DOWN, KeyEvent.KEYCODE_VOLUME_UP,
KeyEvent.KEYCODE_BACK));
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (hijackKeys.contains(event.getKeyCode())) {
return true;
} else {
return super.dispatchKeyEvent(event);
}
}
I'm new here.
I have a problem, i try to shutdown a 4.2.2 android device (not root).
I know that ACTION_SHUTDOWN not works with unroot device.
So i want to open the existing shutdown/reboot/airplane dialog, the same we get when we maintain the on/off button. Then the user just have to click shutdown button.
I try to create by this way, without result...
Intent intent = new Intent(Settings.ACTION_DISPLAY_SETTINGS); // or others settings
startActivity(intent);
Thanks,
The is no public sdk access to open the power button menu programatically.
This link has all the approches Here.Simulating power button press to display switch off dialog box
InputManager.getInstance().injectInputEvent(new InputEvent(KeyEvent.KEYCODE_POWER, keyCode), sync);
'sync' becomes either of these:
InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH
InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_RESULT
and you need
import android.hardware.input.InputManager;
This is untested, but puts you in the right direction, also bare in mind, functionality like this is NOT recommend.
failing that:
public static void simulateKey(final int KeyCode) {
new Thread() {
#Override
public void run() {
try {
Instrumentation inst = new Instrumentation();
inst.sendKeyDownUpSync(KeyCode);
} catch (Exception e) {
Log.e("Exception when sendKeyDownUpSync", e.toString());
}
}
}.start();
}
and simply call it like
simulateKey(KeyEvent.KEYCODE_POWER);