Android black screen in app - android

I have an android application with QR Code scan. Everything works fine until I installed GMD Hide Bar.
The screen will turn completely black after I successfully scan QR Code. But my app continues to run in black screen since I can hear the sound. I have to click the power button on my device twice (to turn off the screen once and turn it on again), then I can see the screen again.
Since they works individually so I'm not sure what cause this. Here is the code when the QR Code scanner scans a code and quit, for which is the moment when the screen turns black.
private final Handler mUnityHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
try {
Class<UnityPlayer> c = com.unity3d.player.UnityPlayer.class;
Method method = c.getMethod("UnitySendMessage", new Class[] { String.class, String.class, String.class });
method.invoke(null, "CodeScannerBridge", "onScannerMessage", mDataStr); // reveiver null
// because
// UnitySendMessage
// is
// static
} catch (NoSuchMethodException e) {
e.printStackTrace();
return;
} catch (Exception e) {
e.printStackTrace();
return;
}
finish();
}
Any idea?
EDIT It doesnt happen 100% of the time, but often. And I have another function:
mHandler.postDelayed(new Runnable() {
public void run() {
finish();
}
}, 10000);
I tested for this. It will turn black screen sometimes too.

Related

Picasso threads keep running

How I am supposed to shut down picasso properly?
Example:
I only have 1 activity, and in this activity in onDestroy, I call shutdown() on all picasso instances(I don't use the singleton one).
But, until this activity gets destroyed, Picasso is keeping the device awake(I start the app, use it, press home, leave the phone for the weekend alone, check it on monday, the battery is dead because Picasso kept the phone awake).
These Picasso threads are still running:
-Picasso-Stats
-Picasso-refQue (twice)
-Picasso-Dispatcher (twice)
Why? Are they supposed to?
What's the best practice for shutting it down? In onStop()? And I should keep a list of unfinished downloads that I might want to retry in onResume()?
As far as i know default singleton instance in picasso cannot be shutdown,
but i solved problem, in 'picasso.java' file at line : 643
found this code:
#Override public void run() {
Process.setThreadPriority(THREAD_PRIORITY_BACKGROUND);
while (true) {
try {
// Prior to Android 5.0, even when there is no local variable, the result from
// remove() & obtainMessage() is kept as a stack local variable.
// We're forcing this reference to be cleared and replaced by looping every second
// when there is nothing to do.
// This behavior has been tested and reproduced with heap dumps.
RequestWeakReference<?> remove =
(RequestWeakReference<?>) referenceQueue.remove(THREAD_LEAK_CLEANING_MS);
Message message = handler.obtainMessage();
if (remove != null) {
message.what = REQUEST_GCED;
message.obj = remove.action;
handler.sendMessage(message);
} else {
message.recycle();
}
} catch (InterruptedException e) {
break;
} catch (final Exception e) {
handler.post(new Runnable() {
#Override public void run() {
throw new RuntimeException(e);
}
});
break;
}
}
}
while (true) have high cpu usage, i decided to change it as follows :
#Override public void run() {
Process.setThreadPriority(THREAD_PRIORITY_BACKGROUND);
while (true) {
try {
// Prior to Android 5.0, even when there is no local variable, the result from
// remove() & obtainMessage() is kept as a stack local variable.
// We're forcing this reference to be cleared and replaced by looping every second
// when there is nothing to do.
// This behavior has been tested and reproduced with heap dumps.
RequestWeakReference<?> remove =
(RequestWeakReference<?>) referenceQueue.remove(THREAD_LEAK_CLEANING_MS);
Message message = handler.obtainMessage();
if (remove != null) {
message.what = REQUEST_GCED;
message.obj = remove.action;
handler.sendMessage(message);
} else {
message.recycle();
}
Thread.sleep(2000);//===> call ever 2 sec to decrease cpu pressure.
} catch (InterruptedException e) {
break;
} catch (final Exception e) {
handler.post(new Runnable() {
#Override public void run() {
throw new RuntimeException(e);
}
});
break;
}
}
}

Finishing Activity after recording video (using CWAC-Camera library)

I am working on an application that automatically invokes an activity to take a video every few seconds.
The activity is started from a service as follows.
Intent intent1 = new Intent(context,CwacCamActivity.class);
intent1.setAction(GlobalVariables.TAKE_VIDEO_ACTION);
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent1.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);//have tried without including this too.
context.startActivity(intent1);
As per the recommendations on the best time to start recording the video,
I start recording the video in
public void autoFocusAvailable()
Here is the code
try {
record();
} catch (Exception e) {
e.printStackTrace();
}
//THread to stop the video after stipulated time ( 5 seconds for example)...
new Thread(new Runnable() {
#Override
public void run() {
//RUnnable to let the record go on for the requested time...
try {
Thread.sleep(5000);
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
try {
stopRecording();
getActivity().finish();
} catch (IOException e) {
e.printStackTrace();
Log.v(GlobalVariables.TAG,"error is"+e.getMessage());
}
}
});
} catch (Exception e) {
Log.v(GlobalVariables.TAG,"error is"+e.getMessage()
}
}
}).start();
When i try the above code by making the activity as MAIN and Launcher, it closes perfectly fine but when running the activity from the Service, it keeps restarting the activity and the whole app crashes in the process.
When taking a picture, it makes sense to finish the activity in the SavePicture().I am not sure if this is the right place to finish the activity or even stopRecording for that matter.However stopRecoring works and the videos are saved as they are supposed to .
I have tried a ton of different things but to no avail.I feel like I am missing something very simple.
Any help is appreciated as I am out of ideas at this point.

Keep Camera Flash On When Screen Off

I have this code to turn camera flash on and off in android:
params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.startPreview();
isFlashOn = true;
The problem appear if flash is on and user turn off screen. flash automatically turned off and return on when screen is on.
I need to keep flash on when screen off.
Any idea please?
Here is a link to the problem you're having. "The key is changing the state back to FLASH_MODE_OFF and then back to FLASH_MODE_TORCH."
In the solution, he creates a Timer Task to handle checking if the screen is on or not. He then turns off the flash and then back on.
Down below the linked solution is another solution which added a thread and made it go to sleep for 200 milliseconds before sending the torch command.
So I'd say the solution you're looking for is the combination of both solutions.
#Override
public void onCreate() {
// assume we start with screen on and save that state ;-)
this.pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
screenOn = this.pm.isScreenOn();
// program a timer which checks if the light needs to be re-activated
this.mTimer = new Timer();
this.mTimerTask = new TimerTask() {
public void run() {
// re-activate the LED if screen turned off
if(!pm.isScreenOn() && pm.isScreenOn() != screenOn) {
Log.i("SleepLEDservice", "re-activated the LED");
// really it's NOT ENOUGH to just "turn it on", i double-checked this
setFlashlight(Camera.Parameters.FLASH_MODE_OFF);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
setFlashlight(Camera.Parameters.FLASH_MODE_TORCH);
}
screenOn = pm.isScreenOn();
}
};
}
private void setFlashlight(String newMode) {
try {
this.frontCamPara = this.frontCam.getParameters();
if(this.frontCamPara.getFlashMode() != newMode) {
this.frontCamPara.setFlashMode(newMode);
this.frontCam.setParameters(frontCamPara);
}
} catch (Exception e) {
e.printStackTrace();
}
}
Again, credit should go to #stefanjunker and #BlueJam as their answers are referenced in this.

Android - two actions in app at the same time

I am developing app and I need that my app will be doing two actions at the same time. For example - I can drag marker and the map will be scrolling at the same time.
I tried to update map while dragging marker, everything would be okay, except thing that when thread starts, maps starts scrolling continuously and I cand do anything more. Here is the code, I call it in onCreate() method. I tried to do Runnable and AsyncTask and the results were the same. Any suggestions?
private void update() {
new Thread() {
public void run() {
while (true) {
runOnUiThread(new Runnable() {
#Override
public void run() {
while (drag)
mMap.animateCamera(CameraUpdateFactory.scrollBy(10, 0), 2, null);
}
});
try {
Thread.sleep(2);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}.start();
}
You don't have the code to interrupt your thread. That's why your map keep scrolling. Try to use drag event to drive this animation, and add some code to terminate the thread at some point.

Puzzle: Bluetooth data send interval shortened by half after each reconnect

I modified the standard Bluetoothchat example to send 4 bytes of data at a time to a bluetooth device every half a second. It works fine if I start the App fresh. However, there is a problem if I reconnect as follows:
While Bluetooth is connected, I click the connect button again on the menu and select the same device. This disconnects the bluetooth (not sure whether this is the right procedure to disconnect). Then, I connect again by selecting the device, and it will be reconnected. After reconnection, a very strange problem appears: instead of sending the data every half a second, it will send the data every quarter a second. If I go through the process again and reconnect, the time interval will become even shorter. It gets to a point that the bluetooth device on the receiving end can't keep up with the data. At this point, the only way out is to kill the app and restart again. Then everything becomes normal, till next time I try to reconnect again.
I have tried different things but nothing appear to fix this. For example, I made sure the thread sending the data is killed when disconnected so no multiple threads are sending the data. I was wondering whether the baud rate changed when reconnected, but then why would the baud rate affect the Thread.sleep(500); statement (which is responsible for controlling the half a second data send). Any help would be greatly appreciated.
Here is the code, the SendClass is created under the MainActivity:
class SendClass implements Runnable {
public void run() {
bytearr[0]=0;bytearr[1]=0;bytearr[2]=0;bytearr[3]=0;
while (!Thread.currentThread().isInterrupted()) {
if (mChatService==null || mChatService.getState()
!=BluetoothChatService.STATE_CONNECTED) {
continue;
} else {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mChatService.write(bytearr);
}
}//end of run
}//end of runnable
Then under STATE_CONNECTED:
case BluetoothChatService.STATE_CONNECTED:
setStatus(getString(R.string.title_connected_to,mConnectedDeviceName));
/*
if(sendingThread!=null){
//sendingThread.stop();
sendingThread.interrupt();
if(D) Log.i(TAG, "after sendingThread");
sendingThread = null;
}*/
sendingThread = new Thread(new SendClass());
sendingThread.start();
break;
As you can see, I tried to kill the thread before creating a new one but that didn't make any difference. Any suggestions?
You are creating a thread that never actually stops, even after you create a new thread and assign to the same variable that particular thread wont stop running.
You need to make sure that the thread will stop after it disconnects.
Here is my suggestion
Change your SendClass to:
class SendClass implements Runnable {
private boolean stopped = false;
public void setStopped(boolean s){
this.stopped = s;
}
public void run() {
bytearr[0]=0;bytearr[1]=0;bytearr[2]=0;bytearr[3]=0;
while (!Thread.currentThread().isInterrupted() && !stopped) {
if (mChatService==null || mChatService.getState() !=BluetoothChatService.STATE_CONNECTED) {
continue;
} else {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mChatService.write(bytearr);
}
}//end of run
}//end of runnable
Then when you start your thread keep the reference to the Runnable so you can call the setStopped(true); like this
SendClass sc = new SendClass();
sendingThread = new Thread(sc);
sendingThread.start();
When you disconnect the bluetooth dont forget to call sc.setStopped(true); so your thread will finish by not going into the while.

Categories

Resources