Missing permissions required by FusedLocationClient - java

I'm working on getting an app to get GPS location from a phone. I have added permissions to the XML for ACCESS_COURSE_LOCATION but it is still saying that I need permissions for that, even though the permission is already in the XML.
Here's my code:
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.location.Location;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.tasks.OnSuccessListener;
public final class MainActivity extends AppCompatActivity {
public static final String TAG = "Kindness";
private FusedLocationProviderClient mFusedLocationClient;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(
this);
mFusedLocationClient.getLastLocation().addOnSuccessListener(this,
new OnSuccessListener<Location>() {
#Override
public void onSuccess(Location location) {
if (location != null) {
}
}
});
}
}
And here's the XML:
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="edu.byui.cit.kindness">
<uses-permission android:name="android.permisison.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/appName"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name="edu.byui.cit.kindness.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>

This is likely because location permissions are considered "dangerous" permissions by Android, and when your application's target SDK is 23 or greater, you are required to ask for "dangerous" permissions at runtime. Just adding them to the manifest does not work.
ActivityCompat.requestPermissions(this,
arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION),
REQUEST_CODE);

Related

Try to the File.listFiles() is returning null in android 11

I was creating an application to test whether File.listFiles() method is working or not. To check this I made an application and I used it there but this returning null in place of an array. This is my full code please help and I have granted all permissions for android 11
Manifest File
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.myapplication">
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
tools:node="merge" />
<uses-permission
android:name="android.permission.STORAGE"
tools:node="merge" />
<uses-permission
android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:requestLegacyExternalStorage="true"
android:theme="#style/Theme.MyApplication">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.File;
import java.io.IOException;
import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
public class MainActivity extends AppCompatActivity {
private Button deleteButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
deleteButton = findViewById(R.id.deleteButton);
deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
deleteFiles(view);
}
});
}
private void deleteFiles(View v) {
File file = new File("/storage/emulated/0/DCIM/");
if (file.isDirectory())
{
File[] files;
files = file.listFiles();
int a = files.length;
for (int i = 0; i < files.length; i++)
{
new File(file, files[i].getName()).delete();
}
Toast.makeText(getApplicationContext(), "Done", Toast.LENGTH_LONG).show();
}else {
Toast.makeText(getApplicationContext(), (CharSequence) file, Toast.LENGTH_LONG).show();
}
}
}

Moving a file from one computer into other computer

I am a really really new developer. I am currently work a BLE stuff with my friend, but we always have an issue to move my project into his computer, and his project move to my computer.
He does not like to use GitHub at all, me too actually, so we want to just use flash drive to transfer the project folder.(this is the way I found from stackoverflow.com)
I always have errors, and this transfer folder way never work as we respect.
Most of question from "Cannot resolve symbol 'R' such as:
setContentView(R.layout.activity_main);
I already look up online, and most of people just say "clean project", but it does not work at all.
Some of people said, activity_main.xml has problem, but I cant find the problem.
MainActivity.java
package test.research.sjsu.beaconbroadcast;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
import android.bluetooth.le.AdvertiseCallback;
import android.bluetooth.le.AdvertiseData;
import android.bluetooth.le.AdvertiseSettings;
import android.bluetooth.le.BluetoothLeAdvertiser;
import android.content.Context;
import android.os.Bundle;
import android.os.ParcelUuid;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import java.util.UUID;
public class MainActivity extends AppCompatActivity {
BluetoothManager mBluetoothManager;
BluetoothAdapter mBluetoothAdapter;
BluetoothLeAdvertiser mBluetoothLeAdvertiser;
AdvertiseData mAdvertiseData;
AdvertiseData.Builder mAdvertiseDataBuilder = new AdvertiseData.Builder();
byte[] Data = new byte[4];
ParcelUuid mServiceDataUUID;
AdvertiseSettings mAdvertiseSettings;
AdvertiseSettings.Builder mAdvertiseSettingBuilder = new AdvertiseSettings.Builder();
Button BroadcastButton;
Button StopBroadcastButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = mBluetoothManager.getAdapter();
mBluetoothLeAdvertiser = mBluetoothAdapter.getBluetoothLeAdvertiser();
Data = hexStringToByteArray("abcdefgh");
mServiceDataUUID = ParcelUuid.fromString(UUID.randomUUID().toString());
mAdvertiseDataBuilder.setIncludeDeviceName(true);
mAdvertiseDataBuilder.setIncludeTxPowerLevel(true);
mAdvertiseData = mAdvertiseDataBuilder.build();
mAdvertiseDataBuilder.addServiceData(mServiceDataUUID,Data);
mAdvertiseSettingBuilder.setAdvertiseMode(1);
mAdvertiseSettingBuilder.setTimeout(0);
mAdvertiseSettingBuilder.setTxPowerLevel(2);
mAdvertiseSettingBuilder.setConnectable(true);
mAdvertiseSettings = mAdvertiseSettingBuilder.build();
BroadcastButton = (Button) findViewById(R.id.BroadcastButton);
BroadcastButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startAdvertise();
}
});
StopBroadcastButton = (Button) findViewById(R.id.StopBraodcastButton);
StopBroadcastButton.setVisibility(View.INVISIBLE);
StopBroadcastButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
stopAdvertise();
}
});
}
Here is my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="test.research.sjsu.beaconbroadcast">
android:versionCode="1"
android:versionName="1.0"
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true" />
<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>
Sometime there are other red lines, but now the R problem just make me crazy.

Google Maps tracking my own location isnt working in WebView

Hello I'm working on programming a Google Maps WebView. I was trying to track and display my own location. When I press the button, it loads for a while and it said that Google Maps couldn't determine my precise location. I tried running it on Android 5.1 Api 22 Lollipop, it still doesnt work.
MainActivity.java:
package com.example.ali.bellsolution;
import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.GeolocationPermissions;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
private static final int REQUEST_FINE_LOCATION = 0;
WebView myWebView;
String mapPath = "https://www.google.co.uk/maps/#51.5112044,-0.2712415,14z";
/**
* WebViewClient subclass loads all hyperlinks in the existing WebView
*/
public class GeoWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// When user clicks a hyperlink, load in the existing WebView
view.loadUrl(url);
return true;
}
}
/**
* WebChromeClient subclass handles UI-related calls
* Note: think chrome as in decoration, not the Chrome browser
*/
public class GeoWebChromeClient extends WebChromeClient {
#Override
public void onGeolocationPermissionsShowPrompt(String origin,
GeolocationPermissions.Callback callback) {
// Always grant permission since the app itself requires location
// permission and the user has therefore already granted it
callback.invoke(origin, true, false);
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
loadPermissions(Manifest.permission.ACCESS_COARSE_LOCATION, REQUEST_FINE_LOCATION);
setContentView(R.layout.activity_main);
myWebView = (WebView) this.findViewById(R.id.mapview);
myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
myWebView.getSettings().setBuiltInZoomControls(true);
myWebView.setWebViewClient(new GeoWebViewClient());
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setGeolocationEnabled(true);
myWebView.setWebChromeClient(new GeoWebChromeClient());
myWebView.loadUrl(mapPath);
}
#Override
public void onBackPressed() {
// Pop the browser back stack or exit the activity
if (myWebView.canGoBack()) {
myWebView.goBack();
}
else {
super.onBackPressed();
}
}
private void loadPermissions(String perm, int requestCode) {
if (ContextCompat.checkSelfPermission(this, perm) != PackageManager.PERMISSION_GRANTED) {
if (!ActivityCompat.shouldShowRequestPermissionRationale(this, perm)) {
ActivityCompat.requestPermissions(this, new String[]{perm}, requestCode);
}
}
}
}
Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ali.bellsolution" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="23" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="WebViewApp"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<activity android:name=".MainActivity"
android:label="WebViewApp"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

unable to locate current gps position

I am constructing an Android application that is to return the current position of the device running the app. I have currently got the google map displaying on the screen but I am having an issue with the location listener, I put break points within it and they never be hit. I will include my MainActivity.java and androidMainfest.xml.
MainActivity.java
package com.example.androidgooglemap;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.maps.GoogleMap;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.content.Context;
import android.view.Menu;
import android.widget.Toast;
public class MainActivity extends FragmentActivity {
public GoogleMap mMap;
LocationManager location;
public double latG;
public double lonG;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
location = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
location.getLastKnownLocation(Context.LOCATION_SERVICE);
LocationListener locationListener = new LocationListener(){
#Override
public void onLocationChanged(Location location)
{
if (location != null)
{
latG = location.getLatitude();
lonG = location.getLongitude();
Toast.makeText(MainActivity.this,
latG + " " + lonG,
Toast.LENGTH_LONG).show();
}
}
};
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/* Request updates at startup */
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidgooglemap"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<permission
android:name="com.example.androidgooglemap.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.example.androidgooglemap.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<!-- The following two permissions are not required to use
Google Maps Android API v2, but are recommended. -->
<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" >
<uses-library android:name="com.google.android.maps" />
<activity
android:name="com.example.androidgooglemap.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>
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="API-KEY"/>
</application>
</manifest>
Some guidance needed. Thank you
You forgot to register the listener to the location service. Add this code after the declaration of locationListener variable.
if(location.isProviderEnabled("gps"))
location.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
if(location.isProviderEnabled("network"))
location.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

Unfortunately, app has stopped

Unfortunately, app has stopped. The purpose of the app is to display a web page when it is launched. I have researched this problem, and tried changing code in the manifest file. I have been unsuccessful at eliminating this message.
MainActivity.java
package com.example.testb1;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.content.Intent;
import android.net.Uri;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState)
{
Intent getmobilepages = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("http://www.mcohio.org/government/auditor/mobile_app/home.cfm");
getmobilepages.setData(uri);
startActivity(getmobilepages);
}
}
testb1 Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testb1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.testb1.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.getmobilepages" />
</intent-filter>
</activity>
</application>
</manifest>
Edit: I tried running your code and was completely wrong. You are just missing the line:
super.onCreate(savedInstanceState);
At the start of your oncreate method. Add that in and it should compile just fine.
Make sure to post your logcat in the future, the error is identified clearly there.
You have not called super you will get SuperNotCalled Exception. You need to set you layout. Better to start another activity on button click.
Add internet permission in manifest
<uses-permission android:name="android.permission.INTERNET"/>
MainActivty
public class MainActivity extends Activity {
Button b;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// call super
setContentView(R.layout.activity_main);
// set your layout
b = (Button) findViewById(R.id.button1);
//get button id
b.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
// do something
Intent getmobilepages = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("http://www.mcohio.org/government/auditor/mobile_app/home.cfm");
getmobilepages.setData(uri);
startActivity(getmobilepages);
}
});
}
}

Categories