How am i suppose to use this
android.intent.category.APP_MUSIC
to launch the music player?
it doesn't work if i call makeMainSelectorActivity
Intent intent = new Intent();
intent.makeMainSelectorActivity(intent.ACTION_MAIN,
"android.intent.category.APP_MUSIC");
startActivity(intent);
This solution might help someone looking to launch default Music Player:
if(android.os.Build.VERSION.SDK_INT>=15){
Intent intent=Intent.makeMainSelectorActivity(Intent.ACTION_MAIN,
Intent.CATEGORY_APP_MUSIC);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//Min SDK 15
startActivity(intent);
}else{
Intent intent = new Intent("android.intent.action.MUSIC_PLAYER");//Min SDK 8
startActivity(intent);
}
Used below code :
Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
or
Intent intent = new Intent("android.intent.action.MUSIC_PLAYER");
startActivity(intent);
Ans also prefer url :
Launching the default music player
Related
Im making a quiz game with questions on diferent topics
For Example i have activities for these topics: Flags, Capitals, Population, Economy, Continent, etc.
And i have one single ResultActivity to obtain the Score of the quiz.
The ResultActivity has a PLAY AGAIN button.
On the FlagsActivity i have this code:
Intent intent = new Intent(getApplicationContext(),ResultActivity.class);
intent.putExtra("RIGHT_ANSWER_COUNT", score);
intent.putExtra("NAME_ACTIVITY", "FlagsActivity");
startActivity(intent);
On the CapitalActivity i have this code:
Intent intent = new Intent(getApplicationContext(),ResultActivity.class);
intent.putExtra("RIGHT_ANSWER_COUNT", score);
intent.putExtra("NAME_ACTIVITY", "CapitalActivity");
startActivity(intent);
etc.....
On the ResultActivity i have this code:
activity = getIntent().getStringExtra("NAME_ACTIVITY");
public void playAgain(View view){
if(activity.equals("FlagsActivity")){
Intent intent = new Intent(getApplicationContext(), FlagsActivity.class);
startActivity(intent);
}
if(activity.equals("CapitalActivity")){
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
if(activity.equals("PopulationActivity")){
Intent intent = new Intent(getApplicationContext(), PopulationActivity.class);
startActivity(intent);
}
if(activity.equals("EconomyActivity")){
Intent intent = new Intent(getApplicationContext(), EconomyActivity.class);
startActivity(intent);
}
if(activity.equals("ContinentActivity")){
Intent intent = new Intent(getApplicationContext(), ContinentActivity.class);
startActivity(intent);
}
}
Basically im sending an Intent with a String containing the name of the activity, then on the Result Activity evaluating with "if" the String = That activity name, start the activity.
What i want to do is someting like this:
On the Flags Activity:
Intent(getApplicationContext(),ResultActivity.class);
intent.putExtra(FlagsActivity.class);
startActivity(intent);
On the CapitalActivity:
Intent(getApplicationContext(),ResultActivity.class);
intent.putExtra(CapitalActivity.class);
startActivity(intent);
On the Result Activity:
activity = getIntent();
public void playAgain(View view){
Intent intent = new Intent(getApplicationContext(), activity);
startActivity(intent);
}
So that i can create as many quiz activities without having to create an "if" statement on the ResultActivity for it to work.
You can pass the class name as a string and use reflection to look up a class object for that type. Something like this:
Class classToLoad = Class.forName(getIntent().getStringExtra("NAME_ACTIVITY"));
Intent intent = new Intent(getApplicationContext(), classToLoad);
startActivity(intent);
I would pass the fully qualified class name to avoid errors.
You could pass the String of activity name to the Result activity, and get its corresponding class name using reflection:
String strActivity = "com.package.FlagsActivity";
Intent intent = new Intent(getApplicationContext(),ResultActivity.class);
intent.putExtra("activity_name", strActivity );
startActivity(intent);
String activityName = getIntent().getStringExtra("activity_name");
Class<?> myClass = Class.forName(activityName );
Intent myIntent = new Intent(getApplicationContext(), myClass);
Something like this.
In the Results Activity
public static void toActivity(Context context, final Class ActivityToOpen){
//whatever awesome code you would like to perform
Intent intent = new Intent(context, ActivityToOpen);
startActivity(intent);
}
In the originating Activity (Flags Activity for example)
ResultsActivity.toActivity(FlagsActivity.this, FlagActivity.class);
Check for typos... I kinda winged this :)
You should be able to call from any originating Activity without if statement.
When you receive the data
String activity;
Bundle bundle = getIntent().getExtras();
if (bundle != null){
activity = bundle.getString("NAME_ACTIVITY");
}
I developed an app that can reverse search by image. I wish user can use image and keyword at the same time to reverse search.
The method getImgurContent() works. It returns image's URL on the internet. But I don't know how to reverse search image with keyword at the same time.
uri = Uri.parse("https://images.google.com/searchbyimage?image_url=" + getImgurContent());
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
You can try code below:
uri = Uri.parse("https://www.google.com/search?tbm=isch&q=findSomeImage");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
Or:
uri = Uri.parse("https://www.google.com/search?tbm=isch&q="+ getImgurContent());
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
Is it possible to get a list of the installed audio players?
I guess I could do that as following.
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_SEND);
intent.setType("audio/*");
List<ResolveInfo> apps = getPackageManager().queryIntentActivities(intent, 0);
But I got nothing.
Could anyone help me? T_T
Try this:
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI,"1");
intent.setData(uri);
intent.setType("audio/*");
List<ResolveInfo> apps = getPackageManager().queryIntentActivities(intent, 0);
for (ResolveInfo rInfo : apps) {
//process list here
}
This code sniper lets you choose at first from you're installed audio players and then gives opportunity to play music from them.
Intent intent = new Intent(Intent.ACTION_GET_CONTENT, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
intent.setType("audio/mpeg3");
startActivityForResult(intent, REQUEST_CODE);
I need to know how I could make a button that opens the androids web browser and navigates to a specific URL. I'm using eclipse and already know how to make buttons and click listeners.
Thanks for any help.
Just add this code to onClick():
// Launch a browser
Uri uri = Uri.parse("http://www.yahoo.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
Intent websiteIntent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("http://www.android.com");
websiteIntent.setData(uri);
startActivity(websiteIntent);
Try this
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com");
startActivity(intent);
I am trying to use the following intent action, but i get an
ActivityNotFoundException in the first case (startActivity) and nothing in the second case (sendBroadcast).
"com.android.launcher.action.UNINSTALL_SHORTCUT"
I try to use it with the following code:
Intent i = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");
startActivity(i);
Also with the following:
Intent i = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");
sendBroadcast(i);
First you need to have the proper permissions:
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
Then you need to do something like this:
Intent intent = new Intent(this, your main activitiy);
intent.setAction(Intent.ACTION_MAIN);
Intent removeIntent = new Intent();
removeIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
removeIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "HelloWorldShortcut");
removeIntent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
sendBroadcast(removeIntent);