I'm creating an android application that has a gesture to expand the status bar.. I used the following code to expand it.. :
try {
Object sbservice = getSystemService("statusbar");
Class<?> statusbarManager = Class.forName("android.app.StatusBarManager");
Method showsb;
if (Build.VERSION.SDK_INT >= 17) {
showsb = statusbarManager.getMethod("expandNotificationsPanel");
} else {
showsb = statusbarManager.getMethod("expand");
}
showsb.invoke(sbservice);
} catch (Exception e) {
e.printStackTrace();
}
I tried this on an emulator and it worked.. But when I tried on a real device this throws an exception
SecurityException: Permission Denial: get/set setting for user asks to
run as user -2 but is calling from user 0; this requires
android.permission.INTERACT_ACROSS_USERS_FULL
Any Idea ?
android.permission.INTERACT_ACROSS_USERS_FULL is a signature level permission. Your app will not be able to use it until and unless it has the same signature as the system.
you need to add permission in manifest.xml
<uses-permission android:name="android.permission.EXPAND_STATUS_BAR" />
Related
hello i try to get the ssid and the password of the hotspot. i use in the method below and its work for android below to 8.
but in android 8.0 its throw exption , i read that i need to get some premisiion
permission:android.permission.OVERRIDE_WIFI_CONFIG.
but we cant get this premission
Method[] methods = m_wifiManager.getClass().getDeclaredMethods();
for (Method m : methods) {
if (m.getName().equals("getWifiApConfiguration")) {
try {
m_wifiConf = (WifiConfiguration) m.invoke(m_wifiManager);
message = m_wifiConf.SSID + '/' + m_wifiConf.preSharedKey + '/';
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
Not sure if this relates to your approach with reflection, but as of Android 8.1 you need to have at least ACCESS_COARSE_LOCATION permission and location services enabled in order to use getConnectionInfo() (see getScanResults()).
Anyway, that won't get you access to the wifi password.
As for the OVERRIDE_WIFI_CONFIG permission, it is defined as
<permission android:name="android.permission.OVERRIDE_WIFI_CONFIG"
android:protectionLevel="signature|privileged" />
This means that your app has to be signed by a platform key or to be added to the privileged whitelist in order to use this permission.
Kindly give answer,i have trouble to refresh mobile network,I don't want system app,I need only for android Mobile apps,I need to refresh my mobile data network pro grammatically in current version like marshmallow and nougat,I already put permissions for this,i attached following my codes
private static boolean setMobileConnectionEnabled(Context context, boolean enabled) {
try {
// Requires: android.permission.CHANGE_NETWORK_STATE
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD) {
Log.i("if", "" + Build.VERSION.SDK_INT);
// pre-Gingerbread sucks!
final TelephonyManager telMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
final Method getITelephony = telMgr.getClass().getDeclaredMethod("getITelephony");
getITelephony.setAccessible(true);
final Object objITelephony = getITelephony.invoke(telMgr);
final Method toggleDataConnectivity = objITelephony.getClass()
.getDeclaredMethod(enabled ? "enableDataConnectivity" : "disableDataConnectivity");
toggleDataConnectivity.setAccessible(true);
toggleDataConnectivity.invoke(objITelephony);
}
// Requires: android.permission.CHANGE_NETWORK_STATE
else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
Log.i("else", "" + Build.VERSION.SDK_INT);
final ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
// Gingerbread to KitKat inclusive
final Field serviceField = connMgr.getClass().getDeclaredField("mService");
serviceField.setAccessible(true);
final Object connService = serviceField.get(connMgr);
try {
final Method setMobileDataEnabled = connService.getClass()
.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
Log.i("try", "" + setMobileDataEnabled);
setMobileDataEnabled.setAccessible(true);
setMobileDataEnabled.invoke(connService, Boolean.valueOf(enabled));
} catch (NoSuchMethodException e) {
// Support for CyanogenMod 11+
final Method setMobileDataEnabled = connService.getClass()
.getDeclaredMethod("setMobileDataEnabled", String.class, Boolean.TYPE);
setMobileDataEnabled.setAccessible(true);
Log.i("catch", "" + setMobileDataEnabled);
Log.i("errr", "" + e.getMessage());
try {
setMobileDataEnabled.invoke(connService, context.getPackageName(), Boolean.valueOf(enabled));
} catch (InvocationTargetException e1) {
e1.printStackTrace();
}
}
}
// Requires: android.permission.MODIFY_PHONE_STATE (System only, here for completions sake)
else {
// Lollipop and into the Future!
final TelephonyManager telMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
Log.i("telMgr",""+telMgr);
final Method setDataEnabled = telMgr.getClass().getDeclaredMethod("setDataEnabled", Boolean.TYPE);
Log.i("telMgdfgsdgr",""+setDataEnabled);
setDataEnabled.setAccessible(true);
setDataEnabled.invoke(telMgr, Boolean.valueOf(enabled));
}
return true;
} catch (NoSuchFieldException e) {
Log.e("", "setMobileConnectionEnabled", e);
} catch (IllegalAccessException e) {
Log.e("gsdjkghskd", "setMobileConnectionEnabled", e);
} catch (IllegalArgumentException e) {
Log.e("lllll", "setMobileConnectionEnabled", e);
} catch (NoSuchMethodException e) {
Log.e("nooooo", "setMobileConnectionEnabled", e);
} catch (InvocationTargetException e) {
Log.e("innnnnn", "setMobileConnectionEnabled", e);
}
return false;
}
My Manifest file
<permission
android:name="android.permission.READ_PHONE_STATE"
android:maxSdkVersion="24" />
<permission
android:name="android.permission.ACCESS_FINE_LOCATION"
android:maxSdkVersion="24" />
<permission
android:name="android.permission.MODIFY_PHONE_STATE"
android:maxSdkVersion="24" />
<permission
android:name="android.permission.CHANGE_NETWORK_STATE"
android:maxSdkVersion="24" />
Kindly give some suggestions please
I can't comment so I'll put my answer here, but it might disappoint you.
There's currently no way to do that in Android >= 5.
You can do that only on rooted phone or system app. It was possible through reflection but since it was #Hide annotated Google removed it and had every right to do so.
If you're looking for disabling only (or disabling the the possibility to change cellular data state) you can use your device provider's api (like Samsung). Samsung allows to disable the possibility to change the cell data state, but... you cannot force the state back to on, after the possibility to change was turned off.
Google repeatedly ignores tickets from the community to bring back this option to SDK.
Please downvote this answer only if you have a solution how to do that without rooted phone or system app.
I downloaded source code, and seems that there is something connected with Android Settings. I want to implement some it's features in my application. How it works?
Finally I found a way to set Android system locale:
public void changeLanguage() {
IActivityManager am = ActivityManagerNative.getDefault();
Configuration config;
try {
config = am.getConfiguration();
config.locale = Locale.US; // Change it to your locale
config.userSetLocale = true;
am.updateConfiguration(config);
BackupManager.dataChanged("com.android.providers.settings");
} catch (RemoteException e) {
e.printStackTrace();
}
}
It also requires CHANGE_CONFIGURATION permission:
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION" />
NOTE: In Android 4.2 or newer, permission policy has been changed, and 3rd party application can't get this permission. If you have root access, execute pm grant command:
String.format("pm grant $1%s android.permission.CHANGE_CONFIGURATION", new Object[]{getPackageName()})
When I tried to enable wifi tethering from the following code it throws the exception
java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at com.....
.... not granted this permission: android.permission.WRITE_SETTINGS
But this works fine in android 6.0 and below versions. And also tried with giving android.permission.WRITE_SETTINGS too.
Is there any limitation in accessing wifiAP in android 6.1?
Follow I attached the code sample that I used to enable hotspot.
WifiConfiguration netConfig = new WifiConfiguration();
netConfig.SSID = ssId;
netConfig.preSharedKey = passkey;
netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
try {
boolean apstatus = (Boolean) method.invoke(wifiManager, netConfig, true);
for (Method isWifiApEnabledmethod : wmMethods) {
if (isWifiApEnabledmethod.getName().equals("isWifiApEnabled")) {
while (!(Boolean) isWifiApEnabledmethod.invoke(wifiManager)) {}
for (Method method1 : wmMethods) {
if (method1.getName().equals("getWifiApState")) {
int apstate;
apstate = (Integer) method1.invoke(wifiManager);
Log.i(TAG, "Apstate ::: " + apstate);
}
}
}
}
if (apstatus) {
Log.d(TAG, "Access Point created");
} else {
Log.d(TAG, "Access Point creation failed");
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
I think Android M don't support to create hotspot programmatically . You can take your Marshmallow user to settings page to create hotspot by himself. below code will help yo to go setting page.
startActivity(
new Intent(Settings.ACTION_SETTINGS));
This is not the correct way.But this fixed the issue.
Changed the target sdk version to 21. Then hotspot will start programmatically even in android 6.0.1. Think there should be a proper way to do this for android 6 and later versions. I think requesting runtime permissions needs to execute those kind of processess.
This talks about the android permission requesting in runtime
Set target SDK version 21 and ask for write_settings permission in your activity. Also add android.permission.WRITE_SETTINGS permission in manifest.
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.WRITE_SETTINGS)){
}else {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_SETTINGS},
121);
}
For more details please visit http://developer.android.com/training/permissions/requesting.html
Guys I tried everything and I wasn't able to start the hotspot in Android 6.0.
You can just check if Api is >= 23, and if so just take the user to settings page to create hotspot by himself.
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
final Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
final ComponentName cn = new ComponentName(
"com.android.settings",
"com.android.settings.TetherSettings");
intent.setComponent(cn);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity( intent);
}else{
createhotSpot();
}
Permission is not your problem. You need something like this code :
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.System.canWrite(getApplicationContext())) {
Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS, Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, 200); //You need a callback for activity result so that check if user enabled this feature, go for starting hotspot (google for it)
} else {
// Do your stuff about starting hotspot (in network thread)
}
}
Camera cam = Camera.open();
Parameters p = cam.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
cam.setParameters(p);
cam.startPreview();
The above dose not work on Lollipop, Because Camera is deprecated in Lollipop. I cant able to find any other way to turn on flash programmatically in Lollipop. How can I achieve this. Thanks in advance.
Camera class is now deprecated.
For LOLLIPOP above you need to use camera2 Api
so nickkadrov's solution doesent work for 6.0 & above device,best way to on/off flash light is try code below
public static void toggleFlashLight(){
toggle=!toggle;
try {
CameraManager cameraManager = (CameraManager) getApplicationContext().getSystemService(Context.CAMERA_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
for (String id : cameraManager.getCameraIdList()) {
// Turn on the flash if camera has one
if (cameraManager.getCameraCharacteristics(id).get(CameraCharacteristics.FLASH_INFO_AVAILABLE)) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
cameraManager.setTorchMode(id, true);
}
}
}
}
} catch (Exception e2) {
Toast.makeText(getApplicationContext(), "Torch Failed: " + e2.getMessage(), Toast.LENGTH_SHORT).show();
}
}
where toggle is class level static Boolean variable whose default value is false
static boolean toggle=false;
mCam = Camera.open();
Camera.Parameters p = mCam.getParameters();
p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
mCam.setParameters(p);
mPreviewTexture = new SurfaceTexture(0);
try {
mCam.setPreviewTexture(mPreviewTexture);
} catch (IOException ex) {
// Ignore
}
mCam.startPreview();
It works for me on Android 5.0.x. And don't forget to add permission in manifest for camera usage.
<uses-permission android:name="android.permission.CAMERA" />
Your code should actually work. Please check if you added the permission for using the camera properly:
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT"/>
This should be added to your AndroidManifest above of your other specifications.
Additionally, there is an interesting discussion about different devices and an example which should work on every device here: Flashlight in Android
If you dont want to use the deprecated API, you can check out:
Package Summary of Camera2
Camera device specification on the new api
Unfortunately I can not give you an example for using the new API, because I did not use it myself yet.