Android - Picking a song to play using Intent - android

I am trying to pick a song using an Activity which will give meta-information about the songs. I want to do this instead of a simple file browser . I have the following code but it unfortunately also plays the song once clicked. I simply want the user to be able to select a song from their MediaStore and act upon it later without playing.
public class Main extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_PICK);
intent.setData(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
startActivity(intent);
}
}

As far as I can tell, the 'picker' you're seeing is a 'preview' type of design (looking at it on my phone).
It's a bit like 'picking' a ringtone, for example...
You see a list and can select each one in turn to get a 'preview'. When you decide on the ringtone you want, you click OK and that returns the selection. Clicking Cancel simply leaves things as they were (existing ringtone selection is kept).
I can't see any way of overriding this behaviour of the picker and haven't found any alternative way (Intent parameters, for example) to achieve what you want to do.
In other words, as I understand it, you simply want the user to silently pick a piece of music and it to return to your Activity but the (preview) picker doesn't work that way.
You can find out what the user previewed/selected when they click OK in the picker however, if you use...
startActivityForResult(intent, 1234);
Note, 1234 is just an arbitrary code.
If you check the Intent returned to onActivityResult() it will have the content Uri of the piece of music the user selected before they pressed OK.

Related

ANDROID: open an URL in external browser, while continuing the application

In the starting activity of my app I show a dialog to user and ask if he wants to see some contents in my website or not.
If he clicks No, the dialog disappears and I call continueActivity() to do some process and go from current activity to MainActivity.
If he click yes, I want to open the webpage in the external browser and again I call continueActivity() to do some process and go from current activity to MainActivity.
The problem is in positive state. This is my code in positive state:
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.aaa.ir"));
startActivity(browserIntent);
continueActivity();
Because of calling continueActivity(), the external browser can't open and if I don't call continueActivity(), the URL opens in the external browser and the app sticks in the current activity.
So how could I open the URL and at the same time, continue the process and go to other activities.
I usually use
startActivityForResult(browserIntent, BROWSER_REQUEST);
and override onActivityResult(int reqCode, int resultStat, Intent intent)
if(reqCode == BROWSER_REQUEST) {
continueActivity();
}
Once you call startActivity, your current Activity will follow through the exit of the Activity lifecycle. This is unavoidable.
If you have processing you want to continue in the meantime, you should consider a Service. If you want the App to return to its current state, you need to store relevant data and load your Activity to its previous state (or next intended state).
in both ways ContinueActivity is opening. i don't know you architecture. You could just add to intent some extra like
intent.putBoolean("openExternalLink", dialogResultHere);
Then in continueActivity you will got this intent like
getIntent().getBoolean("openExternalLink")
also dont forget to delete this option, otherwise you will open browser each time after Activity recreation (screen rotation, minimize, etc.)
getIntent().removeBoolean("openExternalLink");
P.S. signatures of methods could be littlebit different, but general idea is here

How to allow VideoView to continue playing when using Share? Android

I have a video playing full screen in the Android application. I've added a button to allow the user to bring up the "Share" dialog to share to social media, texting, or e-mail. However, when the user selects, for example Facebook, the video will stop playback. Is it possible to force the video to continue playing/rendering in the background with sound ON while the user is performing the share?
The code to Share is below:
public static void share(Activity activity, String subject, String body, String title)
{
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, body);
activity.startActivity(Intent.createChooser(shareIntent, title));
}
VideoView is extremely finicky when it comes to lifecycle, and it stops the video the moment the activity hosting it is paused. Showing an intent chooser dialog effectively pauses your activity.
The preferred method (for both user experience and to handle your issue) would be to use ShareActionProvider to allow the user to select the sharing destination. This is a pop-up list, typically anchored to the action bar, which is a more modern method of showing these options. As an added benefit, the pop-up won't cause your activity to pause, so the video should not stop.
If you must still use the external dialog method, you will have to move away from VideoView and implement the video surface yourself with MediaPlayer. This isn't as scary as it sounds, you can see from the source code that most of VideoView is just wiring it up to MediaPlayer callbacks and attempting to manage state when the surface is created or destroyed.

Video automatically resumed when using Intent Chooser

I have an application which opens different videos using Intent chooser. So you can imagine user clicks on the list item which contains many elements with name of the video.
Now, the thing is working fine with this,
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uriPath, "video/mp4");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
but when the user again clicks on the same item, the intent chooser is shown. Selecting any video player in the device resumes the video.
So the question is, Is there any flag or some way in which the video can be started from the beginning ?
This thing not only happens with video but with pdf's. If an pdf was open and scrolled to the last page, then again opening the pdf again from my application opens it with the last page.
hi hardikI had a similar application to be developed where different
videos should be played when select different items from a list view.
I tried to test my application for the behavior that the video resumes
from last played frame if launch again from the app.
For me it always starts from the beginning. I have tested this on 4.2.2
and 4.4 versions only
The code to start the video was something like this:
Intent in = new Intent(Intent.ACTION_VIEW);
in.setDataAndType(uripath, "video/*");
startActivity(in);
This question is answered here already Android play video intent with start position, particularly in this comment from CommonsWare
Even if there were, they would be on a per-app basis.
If you need this level of control over the video playback,
handle the video playback yourself within your own app
(e.g., VideoView), rather than pass control to third party apps
See http://developer.android.com/reference/android/widget/VideoView.html#seekTo(int) and Playing a video in VideoView in Android
When you delegate to another application with an intent, you get what you get. You should control this within your app using the video player, and if you probably need a library for a pdf view widget. Something like http://code.google.com/p/apv/.
As you use an intent to play the video, it is really up to the acting application how to interpret and handle the intent. Unless the acting application (the one handling the intent you're firing) accepts extra parameters to choose between start-from-beginning and resume, there is really not much you can do.
For full control, use your own video player.
Use VideoView to resolve your problem.
private VideoView myVideoView;
VideoView has an inbuilt method start() which will start your video from the beginning like this:
#Override
public void onStart(){
super.onStart();
myVideoView.start();
}
You can also use the myVideoView.start() on onResume method as well.
Hope this would help. :)
I am completely unsure of this. I also agree on the other views that it is the responsibility of the called Application to handle the intent as it wishes.
However, a wild guess would be to change the uri:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(
uriPath.buildUpon()
.appendQueryParameter("t", String.valueOf(System.currentTimeMillis()))
.build(),
"video/mp4"
);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Note: I did not have the problem that you are mentioning. The video always restarted.

Android - Hide calling phone number when call is made through my app?

I have button in my Android app to call a desired phone number.
I want to hide this number from callers call log (Yes, I want to hide it from displaying androids call log) is there any possible way for me to display calls made by my app as strings (Eg. if you call 12345678; i want to display it as "my numb")
This is my current code to make the call
public void callUsNow(View v) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:1234567890"));
con.startActivity(callIntent);
}
Could anyone suggest on this?
Thanks.
No.
If you pass Intent by calling Intent.ACTION_CALL, after con.startActivity(callIntent) other activity will have control over it.

Intent from Android Notepad example

action: android.intent.action.PICK
data: content://com.google.provider.NotePad/notes
Asks the activity to display a list of the notes under content://com.google.provider.NotePad/notes. The user can then pick a note from the list, and the activity will return the URI for that item back to the activity that started the NoteList activity.
action: android.intent.action.GET_CONTENT
data type: vnd.android.cursor.item/vnd.google.note
Asks the activity to supply a single item of Note Pad data.
The above is directly from Android Notepad example.
My question is, why have they defined two intent actions that perform the same task?? When will one or the other action be performed?
Also in the code, they have defined
String action = getIntent().getAction();
if (Intent.ACTION_PICK.equals(action) || Intent.ACTION_GET_CONTENT.equals(action)) {
Could someone please clarify, when will an action be set and on ListItemClick how will getAction resolve to either ACTION_PICK or ACTION_GET_CONTENT
Thanks in advance
My question is, why have they defined two intent actions that perform the same task?
They are not the same task. Quoting the documentation for ACTION_GET_CONTENT:
This is different than ACTION_PICK in that here we just say what kind of data is desired, not a URI of existing data from which the user can pick. A ACTION_GET_CONTENT could allow the user to create the data as it runs (for example taking a picture or recording a sound), let them browser over the web and download the desired data, etc.
When will one or the other action be performed?
When somebody calls startActivity() with an Intent containing one of those two actions, plus a content Uri pointing to this application (in this case).
Could someone please clarify, when will an action be set
It is set in the Intent used with the startActivity() call that was used to start the activity.
how will getAction resolve to either ACTION_PICK or ACTION_GET_CONTENT
By executing the code you included in your question.

Categories

Resources