I'm trying to create an application that has a button. If you press that button, a song called Test.mp3 will play. I copied the song with the exact name and file type to the directory, but it always show that the file was not found. I have tried plenty of things including copying the song to different directories, and changing directories, but no luck. here is what i have right now:
package com.example.lenovo.myapplication69;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.File;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
Button bt;
MediaPlayer mediaPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt = (Button) findViewById(R.id.button);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
File f = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC),"Test.mp3");
String media_path = String.valueOf(f);
Toast.makeText(getApplicationContext(),media_path,Toast.LENGTH_LONG).show();
Uri uri = Uri.parse(media_path);
try {
mediaPlayer.setDataSource(getApplicationContext(),uri);
mediaPlayer.prepare();
mediaPlayer.start();
Toast.makeText(getApplicationContext(),"Playback started",Toast.LENGTH_LONG).show();
bt.setEnabled(false);
} catch (IOException e) {
Toast.makeText(getApplicationContext(),"error ",Toast.LENGTH_LONG).show();
e.printStackTrace();
}
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
bt.setEnabled(true);
mediaPlayer.release();
mediaPlayer = null;
Toast.makeText(getApplicationContext(),"Playback finished", Toast.LENGTH_LONG).show();
}
});
}
});
}
}
and here is what I have in my android manifest. Yeah I know a lot of useless permissions, but whatever.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.lenovo.myapplication69">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.READ"/>
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS"/>
<uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL"/>
<uses-permission android:name="android.permission.READ_PHONE_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>
Related
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();
}
}
}
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);
I am trying to create a webview app. I tried to add the access to manifest but it didn't change anything. On the chrome browser the camera works, I can't understand what should I do.
1. This is the manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="i.astiapp.astiappadmindev">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" android:required="true" />
<uses-feature android:name="android.hardware.camera.front" android:required="true" />
<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.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<!-- My Location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<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:screenOrientation="portrait"
android:name=".MainActivity">
</activity>
<activity
android:hardwareAccelerated="true"
android:name=".SplashActivity"
android:screenOrientation="portrait"
android:theme="#style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /></intent-filter>
</activity>
</application>
</manifest>
2. Main Activity
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.PermissionRequest;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
public class MainActivity extends AppCompatActivity implements MyWebChromeClient.ProgressListener{
private ProgressBar chromeProgressBar;
WebView myWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView = (WebView) findViewById(R.id.webview);
//chromeProgressBar = (ProgressBar) findViewById(R.id.progressBarChrome);
//Settings
WebSettings webSettings = myWebView.getSettings();
myWebView.getSettings().setTextSize(WebSettings.TextSize.NORMAL);
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
webSettings.setAllowUniversalAccessFromFileURLs(true);
webSettings.setAllowFileAccessFromFileURLs(true);
}
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setAllowFileAccess(true);
//webSettings.setLoadsImagesAutomatically(true);
//inizialize client
//load website by URL
myWebView.loadUrl("https://www.voyage-app.eu/asti-app/admin/index.php");
//register token for notification
// this.onStart();
chromeProgressBar = (ProgressBar) findViewById(R.id.progressBar);
myWebView.setWebChromeClient(new WebChromeClient() {
// Grant permissions for cam
#Override
public void onPermissionRequest(final PermissionRequest request) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
request.grant(request.getResources());
}
}
});
myWebView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
chromeProgressBar.setVisibility(View.VISIBLE);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
chromeProgressBar.setVisibility(View.GONE);
}
});
}
#Override
public void onUpdateProgress(int progressValue) {
chromeProgressBar.setProgress(progressValue);
if (progressValue == 100) {
chromeProgressBar.setVisibility(View.INVISIBLE);
}
}
public class WebViewController extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
#Override
public void onBackPressed()
{
if(myWebView.canGoBack())
myWebView.goBack();
else
super.onBackPressed();
}
}
I don't think I should post the PHP file since on the chrome browser the page works. It happens like this:
You enter the page
Chrome browers asks you to give you the permission to the camera
The camera starts working
Fixed it. I had to add the permession to the code:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
int hasCameraPermission = checkSelfPermission(Manifest.permission.CAMERA);
List<String> permissions = new ArrayList<String>();
if (hasCameraPermission != PackageManager.PERMISSION_GRANTED) {
permissions.add(Manifest.permission.CAMERA);
}
if (!permissions.isEmpty()) {
requestPermissions(permissions.toArray(new String[permissions.size()]), 111);
}
}
But for this to work I had to add the manifest file too:
import android.Manifest;
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.
I am implementing a simple application executing a background service, I am not able to start the service, following is my Manifest & Code, the service class is properly resolved ( reading resources ), inheriting from IntentService ( and implementing the req methods ) doesn't resolve the problem as-well...
Why does the Background service doesn't start?
any help will be appreciated.
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.newcomp.vagent"
android:versionCode="1"
android:versionName="1.0"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="20" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver android:name="com.newcomp.Infrastructure.BootStarter" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".CaptureService"
android:exported="true"
android:process=":captureService" >
</service>
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>
Service code:
package com.newcomp.vagent;
import android.app.IntentService;
import android.app.Service;
import android.content.Intent;
import android.R.*;
import android.os.IBinder;
import java.io.IOException;
import fi.iki.elonen.NanoHTTPD;
public class CaptureService extends Service {
public CaptureService() {
//super("Capture Service");
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return Service.START_NOT_STICKY;
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
}
}
Activity code:
package com.newcomp.vagent;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
final String strSvcName = getResources().getString(R.string.startup_service);
//setContentView(R.layout.main);
Toast.makeText(getBaseContext(), String.format("Hello from '%s'", strSvcName), Toast.LENGTH_LONG).show();
Class cls = null;
try {
cls = getClassLoader().loadClass(strSvcName);
startService(new Intent(this, cls));// Starts the service
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
//finish();
}
}
The problem was simple, the service was actually starting but since android:process=":captureService" was specified it was starting on a separate process, one that is not connected by the debugger, either specifcally connecting to the service process, OR, omitting android:process=":captureService" resolve the problem.