I want to build Music Player app for Android 10 device, I want to list all audio in the MediaStore to the Listview, but unfortunate MediaStore return only one song out of many songs I had on my device, I think all the remaining songs are not present in the MediaStore.
Have tried all my possible best, but still the same.
I think there is no audio file in my device MediaStore except one.
Please!!!!
Here the main activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView mAudioListView = (ListView) findViewById(R.id.audioListView);
ArrayList<String> mAudioList = new ArrayList<>();
//detail of each audio
String[] mAudioDetailArray = { MediaStore.Audio.Media._ID, MediaStore.Audio.Media.DISPLAY_NAME};
//INTERNAL_CONTENT_URI to display audio from internal storage
//EXTERNAL_CONTENT_URI to display audio from external storage
Cursor mAudioCursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, mAudioDetailArray, null, null, null);
if(mAudioCursor != null){
if(mAudioCursor.moveToFirst()){
do{
int audioIndex = mAudioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
mAudioList.add(mAudioCursor.getString(audioIndex));
}while(mAudioCursor.moveToNext());
}
}
mAudioCursor.close();
ArrayAdapter<String> mAdapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,android.R.id.text1, mAudioList);
mAudioListView.setAdapter(mAdapter);
}
}
Related
I am making an android app that asks for the users to select a Bluetooth paired device via spinner.
When the user opens the app first time, user selects a Bluetooth paired device** from list. Then when app opens second time, I want the same Bluetooth paired device to be selected. I don't want user to select the paired device every time the app is opened. How to do that?
Spinner btPairedDevicesSpinner;
BluetoothManager mBluetoothManager;
BluetoothAdapter mBluetoothAdapter;
BluetoothDevice [] mBluetoothDeviceArray;
btPairedDevicesSpinner = findViewById(R.id.btPairedDevicesSpinner);
mGetBluetoothPairedDevice ();
btPairedDevicesSpinner.setOnItemSelectedListener(mPairedDeviceOnItemSelectedListener);
public void mGetBluetoothPairedDevice () {
Set<BluetoothDevice> mPairedDevice = mBluetoothAdapter.getBondedDevices();
mBluetoothDeviceArray = new BluetoothDevice[mPairedDevice.size()];
String [] strings = new String[mPairedDevice.size()];
int index = 0;
if (mPairedDevice.size() > 0) {
for (BluetoothDevice device : mPairedDevice) {
mBluetoothDeviceArray [index] = device;
strings [index] = device.getName();
index++;
}
}
else {
String mOnDevice = "No Device found";
mPairedDeviceArrayAdapter.add(mOnDevice);
}
ArrayAdapter<String> mArrayAdapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, strings);
btPairedDevicesSpinner.setAdapter(mArrayAdapter);
}
private final AdapterView.OnItemSelectedListener mPairedDeviceOnItemSelectedListener = new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
ConnectThread mConnectThread = new ConnectThread(mBluetoothDeviceArray [i], view);
mConnectThread.start();
mBluetoothToolBar.setSubtitle("Connecting");
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
Toast.makeText(BluetoothMain.this, "Nothing is selected", Toast.LENGTH_SHORT).show();
}
};
One way that should work. Store the selected device name in shared preferences. (Create the sharedPref object somewhere else like onCreate() and do the edit in spinners onItemChanged() )
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
sharedPref.edit().putString("btDeviceName", "the name").apply();
After calling
btPairedDevicesSpinner.setAdapter(mArrayAdapter);
load the device name from shared preferences and if there is one call setSelection() on spinner
String deviceName = sharedPref.getString("btDeviceName", null);
if(deviceName != null){
btPairedDevicesSpinner.setSelection(mArrayAdapter.getPosition("deviceName "));
}
My app displays a customed list of videos with the option to download using IntentService. The custom list is displayed on the UI using a recyclerView system. Below is a pic of the list
When a video is clicked and downloaded, the download icon turns blue as seen in the first child of the list shown in the picture above and when it has not been downloaded it shows the default black download icon. I have strung up some codes to make it work, the challenge is, when the app is restarted, The download icon revert to its default color even when it was previously downloaded. How do I update and save changes made to the color of the download icon so it will reflect when the app is restarted? I understand the SharedPreference class can be used to save changes made to the App, but I don't know how to achieve this. Would appreciate any assist that can be lent to achieve this
Below is my onCreate mtd
#Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lecture);
setUpRecyclerView();
And below is where I created the setUpRecyclerView()
private void setUpRecyclerView() {
Query query = lectureRef.orderBy("position", Query.Direction.ASCENDING);
FirestoreRecyclerOptions<LectureClasses> options = new FirestoreRecyclerOptions.Builder<LectureClasses>()
.setQuery(query, LectureClasses.class).build();
adapter = new LectureClassesAdapter(options);
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
}
Below is my call of the onViewItemclick #overide method setup in my adapter class
#Override
public void onViewItemClick(DocumentSnapshot snapshot, int position, View itemView) {
//init variables
CircularProgressBar progressBarCir = itemView.findViewById(R.id.progress_circular);
progressBarCir.setProgressMax(100f);
ImageView downloadImage = itemView.findViewById(R.id.download);
PopupMenu popupMenu = new PopupMenu(LectureActivity.this,downloadImage);
LectureClasses lectureClasses = snapshot.toObject(LectureClasses.class);
lectureClasses = adapter.getItem(position);
String titleNow = lectureClasses.getTitle();
String urlNow = lectureClasses.getUrl();
class DownloadReceiver extends ResultReceiver {
public DownloadReceiver(Handler handler) {
super(handler);
}
#Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
super.onReceiveResult(resultCode, resultData);
if (resultCode == TichaDownloadService.UPDATE_PROGRESS) {
int progress = resultData.getInt("progress");
progressBarCir.setProgress(progress);
downloadImage.setVisibility(View.GONE);
progressBarCir.setVisibility(View.VISIBLE);
Log.i("STATUS","DOWNLOADING>>>");
if (progress == 100) {
progressBarCir.setVisibility(View.GONE);
downloadImage.setImageDrawable(getDrawable(R.drawable.download_blue));
downloadImage.setVisibility(View.VISIBLE);
}
}else {
Log.i("STATUS"," NOT DOWNLOADING,BOSS");
}
}
}
}
you are can to check the file with below code
File videoFile = new File("patch_video_file");
if (videoFile.exists()){
// visible button blue
}else {
// visible button default
}
I'm trying to make my app print a list of available Bluetooth-enabled devices upon opening the activity.
First I enable Bluetooth on the device:
public class Home extends AppCompatActivity {
TextView textView3;
private static final int REQUEST_ENABLE_BT = 1;
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
textView3 = (TextView) findViewById(R.id.textView3);
if (btAdapter == null) {
textView3.append("\nBluetooth not supported. Aborting.");
}
}
then I attempt to add any found devices to the Adapter
private final BroadcastReceiver bReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
//add device to the adapter
btAdapter.add(device.getName(), device.getAddress());
}
}
};
I know there should be a startDiscovery() call somewhere, but I'm having trouble making sense of other answers online. Also, my btAdapter.add() isn't recognized for some reason.
Any help would be appreciated!
my btAdapter.add() isn't recognized for some reason.
Because there is no such method in BluetoothAdapter.
I know there should be a startDiscovery() call somewhere, but I'm having trouble making sense of other answers online.
you can call startDiscovery, once you determine that device supports Bluetooth and Bluetooth is ON from DeviceSettings.
I am using the bellow code to list a unique list of people that sent me SMS. It works fine but still its a bit slow it takes 4 to 5 seconds to load and I have 650 SMS on my device any suggestion ?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listSMS();
}
private void listSMS()
{
TextView tview = (TextView) findViewById(R.id.list);
Uri uriSMSURI = Uri.parse("content://sms/inbox");
ContentResolver cr= this.getContentResolver();
Cursor cur = cr.query(uriSMSURI, null, null, null, null);
LinkedHashSet contactList= new LinkedHashSet();
String sms = "";
while (cur.moveToNext()) {
if(!contactList.contains(cur.getString(2)))
{
contactList.add(cur.getString(2));
sms += "From :" + getContactName(cur.getString(2),cr)+"\n";
}
}
cur.close();
tview.append(sms);
}
public static String getContactName(String num, ContentResolver cr) {
Uri u = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI,Uri.encode(num));
String[] projection = new String[] { ContactsContract.Contacts.DISPLAY_NAME};
Cursor c = cr.query(u, projection, null, null, null);
try {
if (!c.moveToFirst())
return num;
int index = c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
return c.getString(index);
} finally {
if (c != null)
c.close();
}
}
Instead of preparing the list of contacts with their names up front and then passing it to the adapter, try preparing the list with ids only and then fetch the corresponding names inside the adapter. This will solve the delay to start but will make scrolling of the ListView a bit slower which can be solved by using a View Holder or some caching mechanism to prevent fetching the same name more than once. Also note that the adapter will query for names of contacts that are currently visible to the user only.
In my project I can get the list of all installed application by the following code:
public class ApplicationFilterActivity extends Activity implements
OnItemClickListener {
ListView appfilter;
SharedPreferences preferences;
public static String filenames = "RotateData";
PackageManager pck;
ArrayList<Applications> results = new ArrayList<Applications>();
ArrayList<String> gotPackagename = new ArrayList<String>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
appfilter = (ListView) findViewById(R.id.lvApp);
preferences = getSharedPreferences(filenames, 0);
PackageManager packageManager = this.getPackageManager();
List<PackageInfo> applist = packageManager.getInstalledPackages(0);
Iterator<PackageInfo> it = applist.iterator();
while (it.hasNext()) {
PackageInfo pk = (PackageInfo) it.next();
results.add(new Applications(pk.applicationInfo
.loadIcon(packageManager), ""
+ pk.applicationInfo.loadLabel(packageManager)));
Log.i("AppName", "" + pk.applicationInfo.packageName);
gotPackagename.add(pk.applicationInfo.packageName);
}
appfilter.setAdapter(new Customarrayadapter(this, results));
appfilter.setOnItemClickListener(this);
}
}
but now I want to filter that the list will show only the games , is it possible?
There is no way to set your app as "Game Type" in Android, so you could not filter apps by category.
I think, best solution - to parse all games packages (game name could be different because of phone localization) from Google Play and put that list in app and sometimes update it from your server. If you always got internet connection in your application you could send found apps list to server and server will return back games from that list. So, you need to write server code for that.
You could get the package name of every app installed on the device and scrape the google play store to see if that app is in one of the games categories. But that's a bit hackish :).
So first add this permission in your Manifest file
uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"
it will return a boolean if it's true then your package name is from game category
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
binding = GameListBinding.inflate(getLayoutInflater());
View view = binding.getRoot();
setContentView(view);
final PackageManager pm = getPackageManager();
//get a list of installed apps.
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo packageInfo : packages) {
Boolean b = checkAppIsGame(GameList.this,packageInfo.packageName);
if(b){
Toast.makeText(GameList.this, "This is Game "+ packageInfo.packageName, Toast.LENGTH_SHORT).show();
}
}
}
public static boolean checkAppIsGame(Context context, String packageName) {
try {
ApplicationInfo info = context.getPackageManager().getApplicationInfo(packageName, 0);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Log.e("MyGame", "App Packagename is game : " + packageName);
return info.category == ApplicationInfo.CATEGORY_GAME;
} else {
Toast.makeText(context, "is game 2 " + packageName, Toast.LENGTH_SHORT).show();
Log.e("Util", "Package is game 2 : " + packageName);
return (info.flags & ApplicationInfo.FLAG_IS_GAME) == ApplicationInfo.FLAG_IS_GAME;
}
} catch (PackageManager.NameNotFoundException e) {
Log.e("MyGame", "Package info Not Found For : " + packageName, e);
return false;
}
}
No it is not possible.
Catagories are only for using in market Applications don't have any identifier to distinguish between app catagories.