Android Toast prevents my devices to sleep - android

I use Toast to indicate my app's state. And I use the following code in order to control toast showing time.
Toast noCardDetectedToast=null;
void setVisibilityNoCardDetectedToast(boolean visible)
{
if(visible)
{
if(noCardDetectedToast==null)
noCardDetectedToast = Toast.makeText(this, R.string.msg_no_card_detected, Toast.LENGTH_SHORT);
noCardDetectedToast.show();
}
else
{
if(noCardDetectedToast!=null)
{
noCardDetectedToast.cancel();
noCardDetectedToast=null;
}
}
}
When it's necessary to show this Toast, the application starts invoking setVisibilityNoCardDetectedToast(true) several times per second.
And when the application doesn't need this toast any more, it invokes setVisibilityNoCardDetectedToast(false).
Everything works fine, but my android device does not fall asleep, as long as the toast is visible.(I tested my application on Android 4.x and 5.0)
This behaviour looks strange for me. What do I do wrong here?

Related

PIXEL API 30 device not support toast error messages?

I have a problem with a piece of code in my application. I am using Android Studio and as a device to view the application I use PIXEL API 30. Now the problem is that the devide PIXEL API 30 does not show error messages to the user. for example I have the following fragment that does a check on a user's name:
public class Fragment_user{
public boolean checkname(String name){
if(name.length() == 0) {
Toast.makeText(getContext(), "user add", Toast.LENGTH_SHORT).show();
return false;
}else return true;
}
}
now if I don't enter name I want to show the user a toast error message but toast doesn't work with the PIXEL API 30 device. why? Did I get the code wrong or does the PIXEL API 30 device not support toast error messages?
note: I don't want to use setError
Some times system launcher get's crash idk why!, So try to close your emulator and restart it with using Cold boot now function. I also faced that error so i fixed it with using like that.

dronekit-android API addMavlinkObserver causes system crash

I am using dronekit-android packages that can successfully connect to PX4 board. However, I try to receive the mavlink message in a while 1 thread, the receive API addMavlinkObserver causes the system crash.
After the drone is connected, I simply call this function upon pressing a button and it crashes.
this.drone.addMavlinkObserver(new MavlinkObserver() {
#Override
public void onMavlinkMessageReceived(MavlinkMessageWrapper mavlinkMessageWrapper) {
//Log.d("Received Mavlinks:", mavlinkMessageWrapper.getMavLinkMessage().toString());
Toast.makeText(getApplicationContext(), "MAV receive " + mavlinkMessageWrapper.getMavLinkMessage().toString(),Toast.L‌​ENGTH_LONG).show();
}
})
Does anyone have any idea?

DevicePolicyManager resetPassword not working some times

All,
I am having a very strange issue.
I have the following code that enables and disables the PIN based on certain conditions
DevicePolicyManager mDPM;
ComponentName mDeviceAdminSample;
mDPM = (DevicePolicyManager)context.getSystemService(Context.DEVICE_POLICY_SERVICE);
mDeviceAdminSample = new ComponentName(context, DeviceAdminSampleReceiver.class);
public void disablePINLock(DBHelper myDBHelper, Context context) {
mDPM.setPasswordQuality(mDeviceAdminSample,
DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED);
mDPM.setPasswordMinimumLength(mDeviceAdminSample, 0);
boolean result = false;
result = mDPM.resetPassword("", DevicePolicyManager.RESET_PASSWORD_REQUIRE_ENTRY);
if (result) {
// Toast: "Successfully Disabled PIN lock"
}
else {
// Toast: "Could not disable PIN lock"
}
}
public void enablePINLock(DBHelper myDBHelper, Context context) {
mDPM.setPasswordQuality(mDeviceAdminSample,
DevicePolicyManager.PASSWORD_QUALITY_NUMERIC);
mDPM.setPasswordMinimumLength(mDeviceAdminSample, 4);
Cursor c = myDBHelper.getSetting("'random'");
if (c != null) {
boolean result = mDPM.resetPassword("1234",
DevicePolicyManager.RESET_PASSWORD_REQUIRE_ENTRY);
if (result) {
// Toast: "Successfully Enabled PIN lock"
}
else {
// Toast: "Could not enable PIN lock"
}
}
else {
// Toast: "Could not enable PIN lock"
c.close();
}
}
The code was working fine until a few days ago. I restarted the phone to finish updating and since then the disablePINLock function is not working correctly. When the function executes all I get is
"result" is false and toast message "Could not disable PIN". The enablePINLock method is working just fine and has always worked fine.
Since the restart result = mDPM.resetPassword("", DevicePolicyManager.RESET_PASSWORD_REQUIRE_ENTRY); has failed to execute. I tried to catch an exception but it didn't help.
At this point I am clueless as to why the method stopped working after the restart. I checked the device administrators, enabled/disabled the app few times without any success.
Also I am not sure if this is related, but let me say this. The device administrator screen has my app always checked, even after I uncheck the app and deactivate it, the app name has the box checked.
Thank you in advance for any response.
You say you've "checked the device administrators" but you don't say what you saw there. Are there any other active device administrators on the device? DevicePolicyManager.resetPassword() will return false if the password you provide is not compliant with the aggregate of password policies set by all device admins. It sounds like perhaps another device admin is still active.
Also, with regard to your last point:
The device administrator screen has my app always checked, even after I uncheck the app and deactivate it, the app name has the box checked.
That's a common bug with the Device Administrators screen: the checkbox often stays on even when you've successfully disabled an admin. Try backing out of the Device Administrators screen then going back in. It should be unchecked now.

Android catch app crash

I was wondering if it would be possible to override a method (or something like that), that will be called when the application will crash due to some exception.
I'd like to do this, so the next time the user loggs in, it gives him/her a message that the app has crashed and that the bug will be fixed as soon as possible.
Use Thread and setDefaultUncaughtExceptionHandler().
I found a simple little solution that works perfectly:
When my app is created I do something like this:
// Ceck if the app crashed last time
if(this.getIO().readPrimitiveInternalMemoryBoolean(FileNames.SpaceDroid.CrashReport) == true) {
this.getGameLog().d(classTAG, "App crashed last time");
}
else {
this.getGameLog().d(classTAG, "App didn't crash last time");
}
// Flag the app as chrashed and when the app is exited, then mark it as false.
this.getIO().writePrimitiveInternalMemory(FileNames.SpaceDroid.CrashReport, true);
When my app is being closed, then I do something like this:
#Override
public void onDispose() {
super.onDispose();
this.getIO().writePrimitiveInternalMemory(FileNames.SpaceDroid.CrashReport, false);
}
You could use a try catch statement and log info in your logs. Also google play provides a mechanism to notify you about crashes in your apps in production.

Android Toast Won't Disappear

I show the toast, it doesn't disappear, even after the app is closed. How do I fix?
#Override
public void onClipStoreLoadedClipsNotification(ClipStoreLoadedClipsNotification notif)
{
final ClipStoreLoadedClipsNotification notification = notif;
runOnUiThread(new Runnable() {
#Override
public void run()
{
Dialogs.DismissAll();
list.onRefreshComplete();
TextView text = (TextView)findViewById(R.id.loadclipstext);
ProgressBar pb = (ProgressBar)findViewById(R.id.loadclipsprogress);
if (notification.moreClipsAvailable)
{
text.setText(context.getString(R.string.loading_clips));
pb.setVisibility(View.VISIBLE);
}
else
{
text.setText(context.getString(R.string.no_clips));
pb.setVisibility(View.INVISIBLE);
int duration = Toast.LENGTH_SHORT;
Toast.makeText(SugarLoafContext.playbackTabContext, "No clips found.", duration).show();
}
SugarLoafContext.currentCamera = notification.camera;
clipList = notification.clips;
refreshListView();
readyToLoadMoreClips = true;
if (!firstClipsLoaded)
firstClipsLoaded = true;
}
});
}
Is it running inside a IntentService???
If it is so, the problem is that the Intent Services in Android run in a different thread than the main one, so the Toast is shown in a different thread than the main one, after the time to show is finished, the Android system is unable to find where is the Toast, so it can´t remove it.
I had the same problem, and now, i recommend everyone NOT to show Toast inside a IntentService, instead try to run one commom Service, or to open an Activity, or try something different if it is completely necessary to show the Toast.
The fact that the Toast doesn´t dissapear when you close the App is that the IntentService is still running, and you have to reboot the system or to uninstall the App for the Intent Service to be close.
The only explanation is that your Toast is called in a loop. You should track the toast .show() and see if it is not called an infinite times.
An other visual way would be to do this
Toast.makeText(SugarLoafContext.playbackTabContext, "No clips found.", duration).show();
Toast.makeText(SugarLoafContext.playbackTabContext, "Do you understand now?", duration).show();
I am sure you will see both toast alternatively during a looong time...
This can also happen if your device has no network connection, and your app does a license check and shows a Toast of results. One of my apps had this problem, as I was displaying a Toast when checking the app was licensed. Without a network connection the Toast telling the user that a license retry was to be done remained, and when I connected the Toast was removed, because the license check could work.

Categories

Resources