Android Auto binds a list of MediaItem with subtitle - java

Have you ever try to add a tag like that.
First i thought it is an ImageSpan which is built by SpannableString then set to builder.setTitle(input: Charsequence).
I already tried but not working.
Do you guys know how Spotify can do that? or am i missing something, pls correct me.
Big thank

This isn't something you put into the title, artist or other field, but an explicit flag you can example on each item.
As explained in the Display additional metadata indicators for Android Auto:
The following constants can be used in both MediaItem description extras and MediaMetadata extras:
METADATA_KEY_IS_EXPLICIT: indicates whether the item contains explicit content. To indicate an item is explicit, use this constant as the key and the long METADATA_VALUE_ATTRIBUTE_PRESENT as the value.
They go on to use an example:
val extras = Bundle()
extras.putLong(
MediaConstants.METADATA_KEY_IS_EXPLICIT,
MediaConstants.METADATA_VALUE_ATTRIBUTE_PRESENT)
// Add in other extras
val description =
MediaDescriptionCompat.Builder()
.setMediaId(/*...*/)
.setTitle(resources.getString(/*...*/))
.setExtras(extras)
.build()
return MediaBrowserCompat.MediaItem(description, /* flags */)

Related

How to get current activity correctly?

I need to access current activity of my app. I checked this official code
If i use this code below as documentation says:
PythonActivity = autoclass('org.renpy.android.PythonActivity')
i can't access activity and it returns error. So after searching i found this:
PythonActivity = autoclass('org.kivy.android.PythonActivity')
There is 2 function below. First one is opening webbrowser just like in documentation and IT WORKS:
from jnius import cast
from jnius import autoclass
PythonActivity = autoclass('org.kivy.android.PythonActivity')
Intent = autoclass('android.content.Intent')
Uri = autoclass('android.net.Uri')
intent = Intent()
intent.setAction(Intent.ACTION_VIEW)
intent.setData(Uri.parse('http://kivy.org'))
currentActivity = cast('android.app.Activity', PythonActivity.mActivity)
currentActivity.startActivity(intent)
Now i need to recode this for my own script. I want to add new contact to Contacts. I got 'WRITE_CONTACTS' permission for that. in buildozer.spec:
android.permissions = WRITE_CONTACTS
But my own script returns me error. My function:
from jnius import autoclass,cast
PythonActivity=autoclass('org.kivy.android.PythonActivity')
Intent = autoclass('android.content.Intent')
JS=autoclass('java.lang.String')
intent=Intent()
intent.setAction(Intent.ACTION_INSERT)
intent.setType('ContactsContract.RawContacts.CONTENT_TYPE')
intent.putExtra('ContactsContract.Intents.Insert.NAME',JS('Example Name'))
intent.putExtra('ContactsContract.Intents.Insert.PHONE',JS('7777777777'))
currentActivity=cast('android.app.Activity',PythonActivity.mActivity)
currentActivity.startActivity(intent)
Log:
jnius.jnius.JavaException: JVM exception occurred: No Activity found
to handle Intent { typ=ContactsContract.RawContacts.CONTENT_TYPE (has
extras) } android.content.ActivityNotFoundException
I don't know that Is this error eccours because of activity or my wrong type selection. I would be glad if anyone help me to handle this. Like this contents on media is very few. So i hope like this topics let people learn quickly just like me.
I don't know the Python syntax, but I can tell you what the problem is:
You are missing the Intent ACTION. You need to set the ACTION in the Intent to ContactsContract.Intents.Insert.ACTION (which has the string value "android.intent.action.INSERT")
Also, it looks like you are sending Android constant names instead of the actual string values of these things, for example:
you do this:
intent.setType('ContactsContract.RawContacts.CONTENT_TYPE')
which sets the type in the Intent to the string value "ContactsContract.RawContacts.CONTENT_TYPE", which you can see here:
jnius.jnius.JavaException: JVM exception occurred: No Activity found
to handle Intent { typ=ContactsContract.RawContacts.CONTENT_TYPE (has
extras) } android.content.ActivityNotFoundException
but the type needs to be set to the Android constant ContactsContract.RawContacts.CONTENT_TYPE which has the string value "vnd.android.cursor.dir/raw_contact"
As I said, I don't know Python syntax, but you probably need to do something like this:
intent=Intent()
intent.setAction(ContactsContract.Intents.Insert.ACTION)
intent.setType(ContactsContract.RawContacts.CONTENT_TYPE)
or, if Python supports it, Intent has a constructor that takes an ACTION as a parameter, so this might also work:
intent=Intent(ContactsContract.Intents.Insert.ACTION)
intent.setType(ContactsContract.RawContacts.CONTENT_TYPE)
Let me know if any of this makes sense.
FINALLY, I found solution to access activities and getting Contacts datas.
Accessing correctly PythonActivity:
PythonActivity = autoclass("org.kivy.android.PythonActivity")
Accessing Contacts Java Class:
Contacts = autoclass("android.provider.ContactsContract$Contacts")
Accessing ContactsContract.CommonDataKinds.Phone Java Class (same):
Kinds = autoclass("android.provider.ContactsContract$CommonDataKinds$Phone")
Creating getContentResolver():
cr = PythonActivity.mActivity.getContentResolver()
Now we need to create 2 query. In first, we are going to find ID and need to use it in second query to find equal NUMBER. Also need to check is contact has phone number? too. After that we can loop second query to find number:
name_number_dict= {} # Name:Number # Change as you wish..
while (que1.moveToNext()): #Query 1 Loop
if que1.getString(que1.getColumnIndex(Contacts.HAS_PHONE_NUMBER)): #If has number
que2 = cr.query(Kinds.CONTENT_URI, None, Kinds.CONTACT_ID + " = " + que1.getString(que1.getColumnIndex(CONTACTS_ID)), None, None) #Query 2 Loop
while (que2.moveToNext()):
name_number_dict[que1.getString(que1.getColumnIndex(Contacts.DISPLAY_NAME))] = que2.getString(que2.getColumnIndex(Kinds.NUMBER) # Get datas and put in dic.
If you need to check all ColumnNames of query, use query1.getColumnNames() >> Referance
I hope it helps you guys who need to use this in Python>Kivy applications..

Android Studio - URL string

So I'm pretty new to the whole Android Studio thing and I've been using the internet to help me with a lot of the things I am doing and needed help on something.
I'm not sure if it's possible to connect this to either a string or an SQL database but I have a Main Layouts with a bunch of buttons that allow me to click on them and choose what external player I would like to use to watch the video. In my MainActivity java class, this is how it finds the button.
case R.id.button3:
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("http://videoname.mp4"), "video/*");
startActivity(Intent.createChooser(intent, "Choose an External Player"));
break;"
I wanted to know if "http://videoname.mp4" url can be connected to like a string where I can always update or change the URL instead of manually going to find the URL in the MainActivity java and changing it. As of now I have to manually do it but a different way to do it would be helpful.
I'm sorry if it's all confusing, but if you know, please let me know as soon as.
Thank you.
you just need write your string URL :
open your directory folder /res/values/strings.xml -> write your string : <string name="yourStringName>yourStringURL</string>. to use your string do this
getContext().getString(R.string.yourStringName) in fragment
getString(R.string.yourStringName) in Activity
or you can write directly on your code, put your cursor on your string, then press key alt + enter choose extract string resource fill the resource name with your string name
wherever you need to use it, just do point 1 or 2. also in your Intent
hope this help you, never stop learning!
Add it to strings.xml in your project. That is the recommended way of using strings anyway.

Load a string resource based on selected MenuItem resource ID - Android JAVA

I am writing my first Android app. It will have hundreds of menu links that will change with each version released. I am looking for a way to process the onclicks based on the Resource ID.
Menus are defined in XML like so:
<item android:id="#+id/Spring_2013" android:title="Spring 2013" android:showAsAction="ifRoom|withText" />
The onclicks will load a URL stored in strings.xml:
<string name="Summer_2013">file:///android_asset/catalog/current/genadmissionreq.html</string>
So, seems like all I need is a simple, single onclick method that loads whatever string is needed based on the selected Resource ID. The MenuItem ID is ALWAYS equal to the URL string ID in my code. So, without using hundreds of if() or switch/case statments, cant I just say:
String url = getString("R.string." + menuItem.getItemId());
Obviously that assignment wont work for a number of reasons. But you get the idea right? So, how CAN I make this work?
Im sorry for the newb question. I know it should be simple but I've been reading for days and cannot find any way to do this without an if() statement for EVERY menu item. That method would make the weekly updates a nightmare!
Try using getIdentifier()
String title = menuItem.getTitle().toString().replace(' ', '_');
id = getResources().getIdentifier(title, "string", getPackageName());
String string = getString(id);
The first line fetches Spring 2013 and converts it to Spring_2013.
The second line builds "R.string." with whatever the title is, in this case "R.string.Spring_2013".
The third line simply fetches the String resource.

Android - search based on property of AutoCompleteTextView suggestion, or value of AutoCompleteTextView

I have a MyObj class with a name and an id. I have an AutoCompleteTextView that is currently letting you type MyObj names and it auto-completes them for you. There's a button next to the AutoCompleteTextView that, when pressed, I want to:
initiate a search based on the id of the selected MyObj, if one of the auto-complete suggestions was used, OR
initiate a search based on the value in the AutoCompleteTextView if none of the auto-complete suggestions was used (so a partial name string).
How should I go about this? I'm new to Android, so suggestions for other ways to do this are very welcome. Here's some of my current code:
AutoCompleteTextView actv = (AutoCompleteTextView)findViewById(R.id.searchText);
ArrayAdapter<MyObj> adapter = new ArrayAdapter<MyObj>(this,
android.R.layout.simple_dropdown_item_1line, myObjectsList);
actv.setAdapter(adapter);
And also:
public void searchButtonOnClick(View view) {
// Don't know how to get id of selected MyObj here, or just the value in the
// AutoCompleteTextView otherwise
}
I ended up creating a search instead of using an AutoCompleteTextView, since the search gives suggestions as you type also. I was able to pass the id along via the URI in an Intent to display another Activity with the MyObj details.

Unable to pass values between Activities in Android

I am using 2.3.3 version of the Android SDK and I'm trying to pass data between two activities using the following code
Intent myIntent = new Intent(MainAct.this, FriendsActivity.class);
myIntent.putExtra(USER_NAME, ((EditText)findViewById(R.id.username)).getText());
MainAct.this.startActivity(myIntent);
In the FriendsActivity i'm retrieving the value using
Bundle b = getIntent().getExtras();
String user = b.getString(MainAct.USER_NAME);
But the user is null after these lines are executed. Not sure whats wrong here. Read similar questions on SO http://goo.gl/zOJfa but still the problem continue to be seen.
You need to pass (EditText)findViewById(R.id.username)).getText().toString() instead. getText() on an EditText View doesn't return a string, it returns an Editable Object.
just do (EditText)findViewById(R.id.username)).getText().toString() you are able to get string value.

Categories