The app works fine on AVD, but not on real device - java

I really have very big problem with my project. When my phone is connected to the computer via USB cable, everything works fine, but when the phone is disconnected the app doesn't work as I expected. So, I have already tried with android.os.Debug.waitForDebugger(), I removed the all #SuppressLint("NewApi"), also USB debugger is activated on my phone, but I think the problem can be in the Manifest file. Can someone tell me, what can be done in this case?
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androproject"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/fritz_logo"
android:label="#string/app_name"
android:theme="#style/AppTheme"
>
<activity
android:windowSoftInputMode="adjustPan"
android:name="com.example.androproject.MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#style/ThemeWithCorners"
android:configChanges="keyboardHidden|orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.androproject.RegisterDnsAndUser"
android:screenOrientation="portrait"
android:theme="#style/ThemeWithCorners"
android:configChanges="keyboardHidden|orientation"/>
<activity
android:name="com.example.androproject.MenuApp"
android:screenOrientation="portrait"
android:theme="#style/ThemeWithCorners"
android:configChanges="keyboardHidden|orientation"/>
</application>
</manifest>
I am using the following, handler code. When I put 40000 ms, the app works, but when I put 60000 ms, my application doesn't work if the cable is disconnected.
handler.postDelayed(myRunnable = new Runnable(){
#Override
public void run() {
if(btnToggle.isChecked()){
btnToggle.setChecked(false);
btnToggleTimer.setChecked(false);
}else{
btnToggle.setChecked(true);
btnToggleTimer.setChecked(false);
}
}
}, min*60000);

Most likely you have enabled the "Stay awake"-mode in the Developer options in the settings menu. When you disconnect the phone from the charger it will go into sleep mode after a few seconds.
The solution is to either use a partial wake-lock using the PowerManager or (in the above case) use a PendingIntent and the AlarmManager to trigger a event at a later time.

Related

<uses permission> in android studio is not working / application is not asking for permission

I am trying to make an app which starts by asking permission and accesses the hotspot of any device and any android version with which the very first problem which is bothering me is that why is not working, i have tried it on two devices and both of the attempts were unsuccessful.Please can anyone help me out to point my mistake. I checked similar questions related to it and have tried correcting it too but still not working..
here is the xml code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.akshay.turnonhotspot2">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
It is changed since Android API 23.
You also have to ask permissions at Runtime.
Check this document
Permissions are asked when when you need user personal data such as contacts, storage(photo, video, file) and etc..
You don't need to ask permission for android.permission.INTERNET, android.permission.ACCESS_NETWORK_STATE, android.permission.ACCESS_WIFI_STATE, android.permission.CHANGE_WIFI_STATE

Configuring the google account programmatically in Android

i need some help, i was trying configuring the google account behind the android, the main idea is don't use the wizard to configure the google account, i need do this by code, i was searching, but don't successfully, the only way I was trying to do was the following, but I believe that this is merely a way to make my current application tivese access the account I'm trying to use, but still without success
#Override
public void onClick(View v) {
try{
AccountManager mgr = AccountManager.get(this);
Account acc = new Account("sexo#gmail.com", "com.google");
if(mgr.addAccountExplicitly(acc, "pass", new Bundle())) {
Toast.makeText(MainActivity.this, "Done", Toast.LENGTH_LONG);
}
}catch(Exception e){
Log.d("test", e.getMessage());
Log.d("test", e.toString());
e.getStackTrace();
e.printStackTrace();
}
And this is my manifest used
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.actteste"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permaission.INTERACT_ACROSS_USERS_FULL"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
android:installLocation="internalOnly">
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="com.company.demo.account.AuthenticatorService" android:exported="false">
<intent-filter>
<action android:name="android.accounts.AccountAuthenticator"/>
</intent-filter>
<meta-data
android:name="android.accounts.AccountAuthenticator"
android:resource="#xml/authenticator"/>
</service>
</application>
</manifest>
I tried to create a service and also reference in my manifest
<?xml version="1.0" encoding="utf-8"?>
<account-authenticator xmlns:android="http://schemas.android.com/apk/res/android"
android:accountType="com.company.demo.account.DEMOACCOUNT"
android:icon="#drawable/ic_launcher"
android:smallIcon="#drawable/ic_launcher"
android:label="qualquer"/>
You cannot use the account manager to setup a new account on the Google server. It can only be used for login.
The Setup application in Android also has no API available for setting up a new account. The only real way would be to reverse engineer their application code and try to inject values. It not smart and here is why:
All of this is designed this way for customer safety and Google protection. When doing this in an automated way, the customer is not agreeing to the Terms and Conditions as well as creating an account with dangerous access to user information on the device or from the device itself.
If this is 100% required, I suggest that you have the device communicate to a server you own which handles the negotiations with Google and alerts the user accordingly. Its not what you may want to hear, but this is not an advisable route.

Google Play Services - Google login yields a network error

I am trying to integrate Google Play Services into my application in order to enable achievements. Upon application launch, a login attempt is being made which results in one of the following errors:
There was a problem communicating with Google servers.
Failed to log in. Please check your network connection and try again later.
A̶p̶p̶l̶i̶c̶a̶t̶i̶o̶n̶ ̶c̶r̶a̶s̶h̶e̶s̶
The application is incorrectly configured. Check that the package name and signing certificate match the cliend ID created in Developer console. Also, if the application is not yer published, check that the account you are trying to sign in with is listed as a tester account. See logs for more information.
Logcat displays absolutely no data related to this problem so I don't even have a clue what could be wrong. The app is authenticated correctly, with the correct SHA1 certificate fingerprint and app id copied to manifest. Does anyone have any could what this could be?
Thanks.
EDIT: Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.*****.xxx"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.app.Main"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#style/NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.app.GameAct"
android:label="#string/title_activity_game"
android:screenOrientation="portrait" >
</activity>
<meta-data
android:name="com.google.android.gms.games.APP_ID"
android:value="#string/app_id" />
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
</application>
</manifest>
I resolved the issue I had by deleting Google+ data related to my previous game login and logging in again. I do not know what caused this error in the first place but it may be due to wrong settings in my Developer Console. I did change some things, like re-authenticated the app but the changes took effect only after deleting old data.

Android Bluetooth_admin permission error

Regarding Android bluetooth discovery.
Getting this error from manifest permission error. However, the only errors that I found posted, was that they forgot to put the permission outside the application. This is however not the case here.
The program is tested on a Galaxy S2 running Jelly Bean 4.1.
The error is as follows:
03-19 13:08:03.933: W/dalvikvm(12616): threadid=1: thread exiting with uncaught exception (group=0x413082a0)
03-19 13:08:03.938: E/AndroidRuntime(12616): FATAL EXCEPTION: main
03-19 13:08:03.938: E/AndroidRuntime(12616): java.lang.SecurityException: Need BLUETOOTH_ADMIN permission: Neither user 10004 nor current process has android.permission.BLUETOOTH_ADMIN.
Basically the manifest looks like:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.motioncontrol"
android:versionCode="1"
android:versionName="1.0" >
<user-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:debuggable="true"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.motioncontrol.Main"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".AccelerometerDat"
android:label="#string/accelerator_title" />
<activity
android:name=".GyroscopeData" />
<activity
android:name=".Gravitydat" />
<activity
android:name=".Linearacc" />
<activity
android:name=".Bluetooth" />
<activity
android:name=".CheckBluetooth" />
<activity
android:name=".About"
android:label="#string/about_title"
android:theme="#android:style/Theme.Dialog" />
</application>
</manifest>
And the class where the error occurs:
private void doDiscovery() {
if (D) Log.d(TAG, "doDiscovery()");
// Indicate scanning in the title
setProgressBarIndeterminateVisibility(true);
setTitle(R.string.scanning);
Log.d(TAG, "indicates scanning");
// Turn on sub-title for new devices
findViewById(R.id.title_new_devices).setVisibility(View.VISIBLE);
Log.d(TAG, "Set up sub-title for new devices");
// If we're already discovering, stop it
if (mBtAdapter.isDiscovering()) {
mBtAdapter.cancelDiscovery();
Log.i(TAG, "Already running discovering, stop it!");
}
// Request discover from BluetoothAdapter
mBtAdapter.startDiscovery();
Log.d(TAG, "Request discover form BluetoothAdapter");
}
Hope for an answer, what is going wrong :).
Thanks!
Change
<user-permission android:name="android.permission.BLUETOOTH_ADMIN" />
to
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<user-permission> is used to regulate the access to the current component by other application. You should try <uses-permission> to get the privilege.

Android multiple instances of Activity

I'm having a problem where multiple instances of an activity are present in the backstack.
e.g.
Stack: A > B > C
Press home and use some other apps then relaunch my app
Stack: A > B > C > A
I would expect my app to resume with activity C, which in most cases it does, but occasionally it resumes with activity A and then when i press back it goes to activity C.
I know this problem is not caused by android killing my app as if this was the case it would resume with activity A as the root.
I have tried altering the launchMode attribute to singleTask and singleInstance but this results in activity A being resumed as the root, which is not the behaviour that i want, i want it to go back to the previously viewed activity with the backstack intact (I understand this is not always possible if the OS kills the app, which is fine).
I dont believe setting the launchMode attribute to singleTop would be useful either as this would prevent A > A situations but not A > B > A or A > B > C > A etc
Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package=""
android:versionCode="1"
android:versionName="1.0"
android:installLocation="auto"
>
<application android:label="#string/app_name" android:debuggable="false"
android:name=""
android:icon="#drawable/icon" android:theme="#style/CustomTheme"
android:targetSdkVersion="8" android:minSdkVersion="4">
<activity android:name=".activity.A"
android:windowSoftInputMode="stateUnchanged|adjustPan"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".activity.B"
android:screenOrientation="portrait"/>
<activity android:name=".activity.C"
android:screenOrientation="portrait" />
<activity android:name=".activity.D"
android:screenOrientation="landscape"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen" />
<activity android:name=".activity.E"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen" />
<service android:name=".service.S" />
<uses-library android:name="com.google.android.maps" />
</application>
<uses-sdk android:minSdkVersion="4"
android:targetSdkVersion="8"
/>
<supports-screens
android:largeScreens="false"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true"
/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Any advice would be greatly appreciated
David
The behavior you're looking for is what singleTask was meant for.
The problem, then, is why resuming your app pops back to the root. If it's just due to the usual way the system works (e.g., 30-minute timeout), and you really want to prevent that, you could use android:alwaysRetainTaskState="true".

Categories

Resources