Android SecurityException ACCESS_WIFI_STATE - android

I am building an Android app that uses wifi.
I have properly declared these uses permissions in the manifest, however for some reason the app is throwing a SecurityException that causes the app to force-close.
I traced the cause of the security exception in LogCat to the system saying it doesn't have the access_wifi_state permission. This is strange and confusing to me because I have already declared it in the manifest.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android.wifi"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="ACCESS_WIFI_STATE" />
<uses-permission android:name="CHANGE_WIFI_STATE" />
<uses-feature android:name="android.hardware.wifi" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name="android.wifi.AndroidWiFiActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
And here is the code that causes the exception:
package android.wifi;
import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.wifi.R;
public class AndroidWiFiActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = (TextView) findViewById(R.id.textbox);
tv.setText("ABC 123");
WifiManager wifiMgr = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if(wifiMgr.isWifiEnabled())
{
tv.append("Wifi is enabled");
}
else
{
tv.append("Wifi is disabled");
}
}
}
Any help is greatly appreciated. Thanks.

The permission is:
android.permission.ACCESS_WIFI_STATE

Related

"Error: cannot find symbol class app"

I have external package class (my custom library), which I want to include in the project. It uses Activity, but shows error as you see.
If I place this command in main project class, it works well... What to do?
Here is the code:
package com.__MyDefaultLibrary;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.ScaleAnimation;
import android.widget.*;
import java.io.File;
import java.util.Calendar;
//
import android.app.Activity;
import com.oceanesa.samplevideorecorder.R;
public class __MyDefaultFunctions{
public android.app.Activity actv1 = android.app.Activity;
public void Initt(){
PreferenceManager.setDefaultValues(actv1.getBaseContext(), R.xml.mypreferences, false);
}
// ========== MY CUSTOM LIBRARY =============//
//button find
public Button fvb(int id) {
return (Button) actv1.findViewById(id);
}
//message show
public void msg(String text) {
Toast.makeText(actv1.getApplicationContext(), text, Toast.LENGTH_LONG).show();
}
public View.OnClickListener optionsListener2 = new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(actv1.getBaseContext(), com.__MyDefaultLibrary.__MyDefaultPreferencesInit1.class);
actv1.startActivity(i);
}
};
}
And AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.oceanesa.samplevideorecorder"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-feature
android:name="android.hardware.camera.front"
android:required="false" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:screenOrientation="landscape"
android:theme="#style/AppTheme"
>
<activity
android:name="com.oceanesa.samplevideorecorder.VideoCaptureExample"
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="com.__MyDefaultLibrary.__MyDefaultPreferencesInit1"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN_ACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Update Answer just passing context to class
public class MyClass {
private static Context mContext;
private static Activity mActivity;
public MyClass(Context c) {
mContext= c;
}
public MyClass(Activity act) {
mActivity= act;
}
public static void showToastMethod() {
// then passing context or activity to
Toast.makeText(mContext, "mymessage ", Toast.LENGTH_SHORT).show();
// or Toast.makeText(mActivity, "mymessage ", Toast.LENGTH_SHORT).show();
}
}
Finally I got it working with the help of topic: How to display a Toast message in from a class that doesn't extend Activity
Problem was in my code. There was no need to call andorid.app at all. I didnt pass Activity to my external file, from where I couldnt call anything..
I had to pass activity like this (from main project file):
public MyExternalClass myEx = new MyExternalClass(Activity);
and needed some modifications...

location button not pointing me to my location in google map api v2

google map is displying perfectly and googleplayservices are also present,
but my location button on top right corner is not working in google maps api v2 my wifi and gps both are on.Kindly help.
i am testing it on my handset.
I dont know why its not working
mainActivity.java
package com.example.mapexample;
import android.app.Dialog;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
public class MainActivity extends FragmentActivity{
final int RQS_GooglePlayServices = 1;
private GoogleMap myMap;
private LocationManager locationManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Do a null check to confirm that we have not already instantiated the map.
if (myMap == null) {
myMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
// Check if we were successful in obtaining the map.
if (myMap != null) {
// The Map is verified. It is now safe to manipulate the map.
setUpMap();
}
}
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
if (resultCode == ConnectionResult.SUCCESS){
Toast.makeText(getApplicationContext(),
"isGooglePlayServicesAvailable SUCCESS",
Toast.LENGTH_LONG).show();
}else{
GooglePlayServicesUtil.getErrorDialog(resultCode, this, RQS_GooglePlayServices);
}
}
private void setUpMap(){
myMap.setMyLocationEnabled(true);
}
}
mainifest file is here with proper permissions.
mainfest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mapexample"
android:versionCode="1"
android:versionName="1.0" >
<permission
android:name="com.example.mapexample.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.mapexample.permission.MAPS_RECEIVE" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<uses-library
android:name="com.google.android.maps"
android:required="true" />
<activity
android:name="com.example.mapexample.MainMenuActivity"
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="com.example.mapexample.MainActivity">
</activity>
<activity
android:name="com.example.mapexample.ATMlist">
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="MY KEY HERE" />
</application>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
</manifest>
Try turning GPS satellites off in Location access settings. If it's enabled but GPS can't be reached, the my location button fails silently; by turning it off it apparently forces the feature to use the network provider.

Android AccountManager.addAccountExplicitly is stopping my App

I'm still having problems with the Method AccountManager.addAccountExplicitly.
I want to create a App which is submitting a User PW combo to a website.
The app should save the login data and therefore i wanted to use AccountManager:
HttpAuth.java:
package com.geberit.eismi.httpauth;
import android.os.Bundle;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.HttpAuthHandler;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.Button;
public class HttpAuth extends Activity {
private EditText field_username;
private EditText field_password;
private Button btn_login;
private AccountManager accountM;
private Account eismi;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
accountM = AccountManager.get(this);
eismi = new Account("eismi", "com.geberit.sap.t81");
boolean test = accountM.addAccountExplicitly(eismi, "testpassword", null);
field_username = (EditText) findViewById(R.id.field_user);
field_password = (EditText) findViewById(R.id.field_password);
btn_login = (Button) findViewById(R.id.btn_login);
field_username.setText(accountM.getPassword(eismi));
HttpAuth Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.geberit.eismi.httpauth"
android:versionCode="1"
android:versionName="1.0"
android:sharedUserId="com.geberit.eismi"
>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.geberit.eismi.httpauth.HttpAuth"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Everytime when executing the step
boolean test = accountM.addAccountExplicitly(eismi, "testpassword", null);
my app is stopping!!
some output of the console:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.geberit.eismi.httpauth/com.geberit.eismi.httpauth.HttpAuth}: java.lang.SecurityException: caller uid 10039 is different than the authenticator's uid
I hope you can help me
Thanks Michi

Android Google Maps Fragment crashes

I have embedded Google Maps V2 in my App, as the Google Tutorial says.
I get the following errors, when switching to the map fragment.
03-08 12:52:01.600: E/AndroidRuntime(30314): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2105)
03-08 12:52:01.600: E/AndroidRuntime(30314): FATAL EXCEPTION: main
03-08 12:52:01.600: E/AndroidRuntime(30314): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{de.arvidg.exampleactionbartabs/de.arvidg.exampleactionbartabs.StartActivity}:
java.lang.ClassNotFoundException: de.arvidg.exampleactionbartabs.StartActivity
Heres the source code
package de.arvidg.exampleactionbartabs;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.maps.MapActivity;
import data.Device;
import data.Mission;
import de.arvidg.exampleactionbartabs.R;
import logic.FetchData;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
public class MapFrag extends MapFragment {
protected FetchData thread;
protected Device device;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View lView = inflater.inflate(R.layout.map, container,
false);
return lView;
}
public void setDumb() {
}
public Device getDevice() {
return device;
}
public void setDevice(Device device) {
this.device = device;
}
}
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
My Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.arvidg.exampleactionbartabs"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="14" />
<permission
android:name="de.arvidg.exampleactionbartabs.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-permission android:name="de.arvidg.exampleactionbartabs.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".StartActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.Holo" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MapFrag"
android:label="Map"
android:theme="#android:style/Theme.Holo" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="removed" />
<uses-library android:name="com.google.android.maps" />
</application>
</manifest>
change
android:name="com.google.android.gms.maps.MapFragment"/>
to
android:name="com.google.android.gms.maps.SupportMapFragment"/> in activity_main.xml
and change
public class MainActivity extends Activity
to
public class MainActivity extends FragmentActivity in MainActivity.java
these two fixes worked for me.

Error on opening camera and getParameters

This is the code from my MainActivity:
package com.simple.flashlight;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Camera;
import android.hardware.Camera.Parameters;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class Main extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_menue);
Button turnOnButtOn = (Button) findViewById (R.id.button1);
Button turnOnButtOff = (Button) findViewById (R.id.button2);
Camera mCam = Camera.open(); //here is an error
Parameters p = mCam.getParameters(); //here is an error
turnOnButtOn.setOnClickListener(new Button.OnClickListener(){
public void onClick(View cameraButton){
//Turn ON
}
});
turnOnButtOff.setOnClickListener(new Button.OnClickListener(){
public void onClick(View cameraButton){
//Turn OFF
}
});
}
}
and this what I have in the manifest file:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.simple.flashlight"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<permission
android:name="android.permission.FLASHLIGHT"
android:permissionGroup="android.permission-group.HARDWARE_CONTROLS"
android:protectionLevel="normal" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Main"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I am getting errors on Camera mCam = Camera.open(); and Parameters p = mCam.getParameters(); the errors are:
1. the method open() is undefined for type Camera.
2. the method getParameters() is undefined for type Camera.
I'm new to Android development, so maybe I have missed something really stupid,
Thanks in advance!
Delete the import android.graphics.Camera;
It's confusing your calls and does not have an open() method.
Delete import android.graphics.Camera; and when importing, import 'Camera'(android.hardware) instead of 'Camera'(android.graphics).

Categories

Resources