How to Change Screen Resolution Programmatically? - java

Let me tell you my problem. I want to change my screen resolution.
I can change it in an application but it changes only application's
screen. I wanna set system's resolution so it won't be important which
application is running on front. My device's resolution is set as 1280
* 720 p. Can I make it 1260 * 680? If it requires to make changes in
Android source code, I can. Just tell me where to change. Waiting for
your help.

This thread on xda-developers should set you on the right track.

Searching too a valid answer to this, but I have a lead to the solution :
WARNING Experimental buggy stuff :
/*
Requires android.permission.WRITE_SETTINGS
and android.permission.WRITE_SECURE_SETTINGS, thus it requires the app to be a system app.
*/
public void changeResolution(int x, int y){
try { Class c = Class.forName("android.os.ServiceManager");
try { Method method = c.getDeclaredMethod("checkService", String.class);
try {
IWindowManager mWindowManager = IWindowManager.Stub.asInterface((IBinder) method.invoke(null,Context.WINDOW_SERVICE));
try { mWindowManager.setForcedDisplaySize(Display.DEFAULT_DISPLAY,x,y);
} catch (RemoteException e) {e.printStackTrace();}
} catch (IllegalAccessException e) {e.printStackTrace();}
catch (InvocationTargetException e) {e.printStackTrace();}
} catch (NoSuchMethodException e) {e.printStackTrace();}
} catch (ClassNotFoundException e) {e.printStackTrace();}
}
Add a reduced AIDL version of IWindowManager to your project :
/app/src/main/aidl/android/view/IWindowManager.aidl
package android.view;
interface IWindowManager
{
boolean startViewServer(int port); // Transaction #1
boolean stopViewServer(); // Transaction #2
boolean isViewServerRunning(); // Transaction #3
void setForcedDisplaySize(int displayId, int width, int height);
void clearForcedDisplaySize(int displayId);
void setForcedDisplayDensity(int displayId, int density);
void clearForcedDisplayDensity(int displayId);
}
The app will require to be in the system apps folder.
It does something for sure, but right now it also lead to severe bugs.
Rebooting seems to cancel changes.
Waiting for feedback on this.

If you are using Windows and know how to use JNI, Microsoft provides C++ Win32 function calls to do this: ChangeDisplaySettingsEx() and EnumDisplaySettings().

Related

Toggle hotspot programmatically in android N

I'm using android N and I'm trying to turn on wifi hotspot when the user opens the application. I was able to turn on the hotspot using the code that I mentioned below but other mobiles are not able to connect to this network, they stuck at Obtaining IP address.
Functionality
public void changeStateWifiAp(boolean activated) {
Method method;
try {
method = wifiManager.getClass().getDeclaredMethod("setWifiApEnabled", WifiConfiguration.class, Boolean.TYPE);
method.invoke(wifiManager, wifiConfiguration, activated);
} catch (Exception e) {
e.printStackTrace();
}
}

Android camera2 abandoned surface on Nougat but works on Marshmallow

EDIT 05/05/2018
I still do not know the reason why this happens, but apparently it does not affect Lolipop and Marshmallow devices. This error only comes up on Nougat 7.0 as far as I have tested.
I'm making a app that lets you record a video which then used for further processing. However, I'm currently having trouble with the recording part.
Code to setup/start the recording:
private void startRecord()
{
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediaRecorder.setOutputFile(videoFile.getAbsolutePath());
mediaRecorder.setVideoEncodingBitRate(1000000);
mediaRecorder.setVideoFrameRate(30);
mediaRecorder.setVideoSize(videoSize.getWidth(), videoSize.getHeight());
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
mediaRecorder.setOrientationHint(totalRotation);
try
{
mediaRecorder.prepare();
SurfaceTexture surfaceTexture = textureView.getSurfaceTexture();
surfaceTexture.setDefaultBufferSize(previewSize.getWidth(), previewSize.getHeight());
Surface previewSurface = new Surface(surfaceTexture);
Surface recordSurface = mediaRecorder.getSurface();
captureRequestBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_RECORD);
captureRequestBuilder.addTarget(previewSurface);
captureRequestBuilder.addTarget(recordSurface);
cameraDevice.createCaptureSession(Arrays.asList(previewSurface, recordSurface),
new CameraCaptureSession.StateCallback()
{
#Override
public void onConfigured(CameraCaptureSession session)
{
recordCaptureSession = session;
try
{
recordCaptureSession.setRepeatingRequest(captureRequestBuilder.build(), null, null);
}
catch (CameraAccessException e) {}
}
#Override
public void onConfigureFailed(CameraCaptureSession session) {}
}, null);
}
catch (IOException ioEx) {} // mediaRecorder.prepare()
catch (CameraAccessException caEx) {} // cameraDevice.createCaptureSession()
}
Specifically at cameraDevice.createCaptureSession() method,
the problem lies in recordSurface being abandoned and thus throwing IllegalArgumentException on that line.
I have read some posts about this problem and most said that it was due to the variable going out of scope which caused the GC to kick in and delete the surface.
I've tried making it a member variable like the posts suggested but the error still persists. Others using the above resolution seems to fix the problem but it does not work on mine.
Any insight on this? Thank you!

Java Me 8 raspberry pi - java.security.AccessControlException

I have the following simple code written in java-ME embedded:
public class JavaMEApplication2 extends MIDlet {
#Override
public void startApp() {
GPIOPinConfig config1 = new GPIOPinConfig(DeviceConfig.DEFAULT, 4, GPIOPinConfig.DIR_OUTPUT_ONLY,
DeviceConfig.DEFAULT , GPIOPinConfig.TRIGGER_NONE, true);
try {
GPIOPin pin = (GPIOPin) DeviceManager.open(config1);
Thread.sleep(2000);
pin.setValue(false);
pin.setDirection(GPIOPinConfig.MODE_INPUT_PULL_UP);
} catch (IOException ex) {
Logger.getLogger(JavaMEApplication2.class.getName()).log(Level.SEVERE, null, ex);
} catch (InterruptedException ex) {
Logger.getLogger(JavaMEApplication2.class.getName()).log(Level.SEVERE, null, ex);
}
}
#Override
public void destroyApp(boolean unconditional) {
}
}
The previous code run just fine (My LED is turned ON and OFF) until the execution reach this statement:
pin.setDirection(GPIOPinConfig.MODE_INPUT_PULL_UP);
The following exception occurs:
TRACE: <at java.security.AccessControlException: >, startApp threw an Exception java.security.AccessControlException:
My API permissions Configuration:
Can any one please tell me why this exception occurs? and if there's another way to toggle the same pin between OUTPUT Mode and INPUT Mode in java-Me embedded?
I am running into the same problem when i try and use the ADSONG AM2302 temp and humidity sensor, What I have done to avoid that is connect another pin with a pull up resistor to the current pin that is initially input/output and set one pin to output and one to input, it gets rid of the permission problem at least. The sensor is still not responding to my start signal though so this may have cause unforeseen problems
I think it may be failing because you had this parameter in the call to the GPIOPinConfig constructor:
GPIOPinConfig.DIR_OUTPUT_ONLY
Maybe try GPIOPinConfig.DIR_BOTH_INIT_OUTPUT instead.
Look here for the different values and their meanings:
https://docs.oracle.com/javame/8.0/api/dio/api/index.html
open your jwc_properties.ini file from /home/pi/javame8/bin directory inside raspberry pi and add:
authentication.provider = com.oracle.meep.security.NullAuthenticationProvider
under [internal] section

Android Concurrency, App dying after about 3 uses

I'm developing an app with different views populated with data acquired from a Server API. As of right now, I've come up with a 'solution' as to how to make the most out of the multicore Android devices that are currently available. I thought that a Producer/Consumer strategy was the right choice for this problem. However I'm using as many consumers as the Phone has cores and when executing the application everything works fine. But in some cases the application dies and doesn't do anything when I launch it, if I clear the RAM on my device I can launch the application again.
Consumer run method:
while(true) {
lock.lock();
while(queuedOperations.isEmpty()) {
ma.debug("Slave " + slaveId + " is waiting for requests.");
try {
slaveCondition.await();
if(doTerminate) { return; }
} catch (InterruptedException e) {
e.printStackTrace();
}
}
ma.debug("Executing Request... " + slaveId);
JSONRequestParam[] params = queuedOperations.poll();
ma.debug("Polled an item. " + slaveId);
lock.unlock();
ma.debug(request.execute(params)); // returns a String.
}
Using the following shutdown method:
public void destroy() {
ma.debug("Terminating all external resources...");
lock.lock();
this.doTerminate = true;
slaveCondition.signalAll();
lock.unlock();
try {
service.awaitTermination(10, TimeUnit.SECONDS);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
service.shutdown();
}
The way I trigger the Consumers is by a signal from the Condition class. Here's the code:
public void submitRequest(JSONRequestParam... params) {
lock.lock();
queuedOperations.add(params);
slaveCondition.signal();
lock.unlock();
}
I overrided the onBackPressed method in the main activity as well;
#Override
public void onBackPressed() {
try {
JSONRequestManager.getInstance(this).destroy();
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
super.onBackPressed();
}
but to no avail. The application is very buggy and unstable. I have tried to find a replicate of this problem here and on numerous of different sites, couldn't find anything that helped. I'm almost sure there's no deadlocks in this application, I have a reasonable amount of experience working with concurrent applications, however Android is a tad bit different when it comes to threading...
Is there any hints or practices that I'm missing out? Why is the application working when the RAM is cleared and on first launch? Is there something jamming in the background? I really do not know. I hope you can tell!
Any help is greatly appreciated!

Using the content handler API (JSR 211) to open applications

I want to be able to launch native and J2ME applications through my application using the content handler API (JSR 211) on a Nokia 6212.
At the moment, I am unable to do so, as it always states that there is "No Content Handler Found" and throws a javax.microedition.content.ContentHandlerException.
At the moment, I am trying to get the phone to launch its browser and go to a certain website, just to test that I can use the framework. I have tried many different Invocation objects:
//throw exceptions
new Invocation("http://www.somesite.com/index.html",
"application/internet-shortcut");
new Invocation("http://www.google.co.uk","text/html");
// a long shot, I know
new Invocation("http://www.somesite.com/text.txt","text/plain");
// massive long shot
new Invocation("http://www.google.co.uk","application/browser");
//appears to download the link and content (and definitely does in the Nokia
// emulator) and then throws an exception
new Invocation("http://www.google.co.uk");
new Invocation("http://www.somesite.com/index.html");
Below is the code that I have been using, please bear in mind the parameters often changed to generate the different Invocation objects.
/*
* Invokes an application using the Content Handler API
*/
public void doInvoke(String url, String mime, String payload){
Registry register = Registry.getRegistry(this.getClass().getName());
Invocation invoke = new Invocation(url, mime, null, false,
ContentHandler.ACTION_OPEN);
boolean mustQuit = false;
try {
mustQuit = register.invoke(invoke);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (ContentHandlerException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if(mustQuit){
this.quit();
}
}
Try this:
Registry register = Registry.getRegistry(this.getClass().getName());
You must call Registry.getRegistry for the MIDlet inheritor. Just use your MIDlet for getting the class name.

Categories