using intent for launching music player - java

im trying to launch the music player but it just doesn't work.
Intent intent = new Intent(MediaStore.INTENT_ACTION_MUSIC_PLAYER);
startActivity(intent);
The app crashes,it suppose to be simple but i have no idea why it doesn't work...

It might be raised ActivityNotFoundException. right?
That intent is deprecated in API Level 15 (ICS MR1).
And I guess your phone manufacturer missed that intent in music app.
Use http://developer.android.com/reference/android/content/Intent.html#CATEGORY_APP_MUSIC instead.

Related

start camera without asking for result

I am looking for an intent to start the camera application without asking for a result.
the only thing i found yet is Camera.ACTION_NEW_PICTURE but this only shoots a photo, I want to display the usual camera app, just without wanting a result from it!
Is there any way to do so?
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
this is basically the relevant code, as this app provides an extension for DashClock, which uses a service and this might be just a wrapper for a pending intent. However, this code causes to infinitely let the camera take pictures on pressing "shoot" and on accept or return button it crashes.
Yeah, try this one:
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivity(cameraIntent);

Android: How to launch call intent using google voice?

How to launch specific intent (such as call) using google voice? How to pass phone number using intent? Following code launches google voice but what value to be passed for making call using google voice as intent extras?
final Intent intent = new Intent();
intent.setComponent(new ComponentName("com.google.android.apps.googlevoice", "com.google.android.apps.googlevoice.activity.conversationlist.ConversationListActivity"));
intent.putExtra("label", "<phone number>");
startActivity(intent);
Here what should i put in label to start the intent that launches a call using google voice?
Any help is appreciated... Thanks in Advance...
NEVER target applications directly like that UNLESS it is in your package. You should be using the Intent filter to catch that particular application. Sometimes you have to target an application like this, but this brings up the risk of change in package name errors.
To handle your particular application, you need to look at how information is being passed into Google voice. this will give you insight and how to target it WITHOUT targeting the exact package name.
What #JoxTraex said makes sense. However some clients need funny features like this, so we have no way but to implement this:
try {
Intent intent = new Intent(Intent.ACTION_DIAL,Uri.parse("tel:" + mobile));
intent.setPackage("com.google.android.apps.googlevoice");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
} catch (ActivityNotFoundException anfe) {
GMHintManager.getInstance().showError(context, "Google Voice not installed");
}
Yes, you should try-catch ActivityNotFoundException.

How to play a few video in youtube player on Java, Android?

I have a few videos for Android. My app plays it by code:
Intent youtube=new Intent(Intent.ACTION_VIEW, Uri.parse(link));
startActivityForResult(youtube, 100);
But it code play 1 video from list. How can I put a few video for Intent in order to standart player play it in series?
Playlists are not supported. You will have to write your own,
create a list of videos
Lauch the new activity using startActivityForResult
In the onActivityResult just launch the same activity with the new video file.

Finish all previous activities

My application has the following flow screens :
Home->screen 1->screen 2->screen 3->screen 4->screen 5
Now I have a common log out button in each screens
(Home/ screen 1 / screen 2 /screen 3/ screen 4 / screen 5)
I want that when user clicks on the log out button(from any screen), all the screens will be finished and a new screen Log in will open .
I have tried nearly all FLAG_ACTIVITY to achieve this.
I also go through some answers in stackoverflow, but not being able to solve the problem.
My application is on Android 1.6 so not being able to use FLAG_ACTIVITY_CLEAR_TASK
Is there any way to solve the issue ?
Use:
Intent intent = new Intent(getApplicationContext(), Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
This will clear all the activities on top of home.
Assuming you are finishing the login screen when the user logs in and home is created and afterward all the screens from 1 to 5 on top of that one. The code I posted will return you to home screen finishing all the other activities. You can add an extra in the intent and read that in the home screen activity and finish it also (maybe launch login screen again from there or something).
I am not sure but you can also try going to login with this flag. I don't know how the activities will be ordered in that case. So don't know if it will clear the ones below the screen you are on including the one you are currently on but it's definitely the way to go.
You may try Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK. It will totally clears all previous activity(s) and start new activity.
Before launching your new Activity, simply add the following code:
finishAffinity();
Or if you want it to work in previous versions of Android:
ActivityCompat.finishAffinity(this);
When the user wishes to exit all open activities, they should press a button which loads the first Activity that runs when your application starts, clear all the other activities, then have the last remaining activity finish. Have the following code run when the user presses the exit button. In my case, LoginActivity is the first activity in my program to run.
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);
The above code clears all the activities except for LoginActivity. Then put the following code inside the LoginActivity's onCreate(...), to listen for when LoginActivity is recreated and the 'EXIT' signal was passed:
if (getIntent().getBooleanExtra("EXIT", false)) {
finish();
}
Why is making an exit button in Android so hard?
Android tries hard to discourage you from having an "exit" button in your application, because they want the user to never care about whether or not the programs they use are running in the background or not.
The Android OS developers want your program to be able to survive an unexpected shutdown and power off of the phone, and when the user restarts the program, they pick up right where they left off. So the user can receive a phone call while they use your application, and open maps which requires your application to be freed for more resources.
When the user resumes your application, they pick up right where they left off with no interruption. This exit button is usurping power from the activity manager, potentially causing problems with the automatically managed android program life cycle.
I guess I am late but there is simple and short answer.
There is a finishAffinity() method in Activity that will finish the current activity and all parent activities, but it works only in Android 4.1 or higher.
For API 16+, use
finishAffinity();
For below 16, use
ActivityCompat.finishAffinity(YourActivity.this);
Hope it helps!
Intent intent = new Intent(this, classObject);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
This Will work for all Android versions. Where IntentCompat the class added in Android Support library.
Use the following for activity
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
remove CLEAR_TASK flag for fragment use.
I hope this may use for some people.
On a side note, good to know
This answer works (https://stackoverflow.com/a/13468685/7034327)
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
this.finish();
whereas this doesn't work
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
.setFlags() replaces any previous flags and doesn't append any new flags while .addFlags() does.
So this will also work
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
From developer.android.com:
public void finishAffinity ()
Added in API level 16
Finish this activity as well as all activities immediately below it in the current task that have the same affinity. This is typically used when an application can be launched on to another task (such as from an ACTION_VIEW of a content type it understands) and the user has used the up navigation to switch out of the current task and in to its own task. In this case, if the user has navigated down into any other activities of the second application, all of those should be removed from the original task as part of the task switch.
Note that this finish does not allow you to deliver results to the previous activity, and an exception will be thrown if you are trying to do so.
If your application has minimum sdk version 16 then you can use finishAffinity()
Finish this activity as well as all activities immediately below it in the current task that have the same affinity.
This is work for me In Top Payment screen remove all back-stack activits,
#Override
public void onBackPressed() {
finishAffinity();
startActivity(new Intent(PaymentDoneActivity.this,Home.class));
}
http://developer.android.com/reference/android/app/Activity.html#finishAffinity%28%29
In my case I use finishAffinity() function in last activity like:
finishAffinity()
startHomeActivity()
Hope it'll be useful.
A solution I implemented for this (I think I found it on Stack Overflow somewhere, but I don't remember, so thanks to whoever did that in the first place):
From any of your activities do this:
// Clear your session, remove preferences, etc.
Intent intent = new Intent(getBaseContext(), LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Then in your LoginActivity, overwrite onKeyDown:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
moveTaskToBack(true);
return true;
}
return super.onKeyDown(keyCode, event);
}
For logout button on last screen of app, use this code on logout button listener to finish all open previous activities, and your problem is solved.
{
Intent intent = new Intent(this, loginScreen.class);
ntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
Intent i1=new Intent(getApplicationContext(),StartUp_Page.class);
i1.setAction(Intent.ACTION_MAIN);
i1.addCategory(Intent.CATEGORY_HOME);
i1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
i1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i1);
finish();
i have same problem
you can use IntentCompat , like this :
import android.support.v4.content.IntentCompat;
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
this code work for me .
Android api 17
instead of using finish() just use finishAffinity();
Log in->Home->screen 1->screen 2->screen 3->screen 4->screen 5
on screen 4 (or any other) -> StartActivity(Log in) with FLAG_ACTIVITY_CLEAR_TOP
for API >= 15 to API 23
simple solution.
Intent nextScreen = new Intent(currentActivity.this, MainActivity.class);
nextScreen.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(nextScreen);
ActivityCompat.finishAffinity(currentActivity.this);
If you are using startActivityForResult() in your previous activities, just override OnActivityResult() and call the finish(); method inside it in all activities.. This will do the job...
When user click on the logout button then write the following code:
Intent intent = new Intent(this, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
And also when after login if you call new activity do not use finish();
I guess I am late but there is simple and short answer. There is a finishAffinity() method in Activity that will finish the current activity and all parent activities, but it works only in Android 4.1 or higher.
For API 16+, use
finishAffinity();
For below 16, use
ActivityCompat.finishAffinity(YourActivity.this);
Hope it helps!
shareedit
answered May 27 '18 at 8:03
Akshay Taru
Simply, when you go from the login screen, not when finishing the login screen.
And then in all forward activities, use this for logout:
final Intent intent = new Intent(getBaseContext(), LoginScreen.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent);
It works perfectly.
If you log in the user in screen 1 and from there you go to the other screens, use
Intent intent = new Intent(this, Screen1.class);
intent.addFlags(FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
I found this way, it'll clear all history and exit
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
Intent intent = new Intent(getApplicationContext(), SplashScreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
System.exit(0);
I found this solution to work on every device despite API level (even for < 11)
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
ComponentName cn = intent.getComponent();
Intent mainIntent = IntentCompat.makeRestartActivityTask(cn);
startActivity(mainIntent);
Best way to close all the previous activities and clear the memory
finishAffinity()
System.exit(0);
I have tried the flags on my end and still haven't worked. I have an application with a very similar design and I have a possible solution in terms of logic. I have built my Login and Logout using shared preferences.
If I logout, data in my shared preferences is destroyed/deleted.
From any of my activities such as Home.java I check whether shared preferences has data and in this case it won't because I destroyed it when I logged out from one of the screens. Therefore logic destroys/finishes that activity and takes me back to the Login activity. You can replicate this logic in all your other activities.
However remember to perform this check inside onPostResume() because this is what is called when you go back to Home.java
Code Sample Below:
#Override
protected void onPostResume() {
SharedPreferences pref = this.getSharedPreferences("user_details", Context.MODE_PRIVATE);
if (pref.getAll().isEmpty()){
//Shared Preferences has no data.
//The data has been deleted
Intent intent = new Intent(getApplicationContext(), Login.class);
startActivity(intent);
finish();
//Finish destroys that activity which in our case is the Home.java
}
super.onPostResume();
}
In Kotlin this way:
in Another Activity (with some classes), under Imports
var activity:Activity?=null
get() = field
set(value) {
field = value
}
Then, under onCreate
activity=this
in MainActivity now:
activity?.finish()

Debugging Intents

I asked a question previously about what shows up in the IntentChooser when I send an Intent with ACTION_SEND and MIME type "image/png". The problem is that some phones come with a default messaging app that is not showing up in the list, mine included (myTouch 4G) as well as a user that I speak with over email (using an HTC EVO). If I choose a Send or Share option from the built in gallery app or another application on the same image I'm saving and attempting to send directly from my app, Messages shows up in the list. From my app it does not. Other phones don't have this problem, so it's clearly a platform specific thing. But that doesn't mean I should just ignore the problem.
So, I go to troubleshooting the issue. I register one of the activities in my app to receive the the same type of intent, and then hit a breakpoint to analyze the Intent object being sent from the two different ways of sending it.
The problem is, the intent I'm sending and the intent being sent from Gallery or AndroZip (where Messages does show up in the chooser) seem to be the same. They both have the same action, same categories, same flags, same mime type. What else can I inspect on the Intent from Gallery or AndroZip to tell if there's some more information I can add to my Intent to get the default messaging app to show up in the chooser in cases where it is not?
The problem is specific to HTC Sense phones, and it arises because their Gallery and Messaging apps are different to the stock ones.
Specifically the Intent sent from Gallery to Messaging has the action android.intent.action.SEND_MSG which is different to android.intent.action.SEND. The Sense messaging app doesn't handle SEND, unlike the stock messaging app.
So the question becomes, how is the Sense Gallery app creating an activity chooser dialog which combines both SEND and SEND_MSG ?
I've done some research and got mostway there... the code below works, but the "Messages" entry in the dialog appears at the top rather than in alphabetical order as per Gallery. Doubtless some more research into intents would correct that, but at least this works:
// Create a chooser for things that can ACTION_SEND images
Intent intent = new Intent(Intent.ACTION_SEND);
Uri data = Uri.parse("content://media/external/images/media/98");
intent.putExtra(Intent.EXTRA_STREAM, data);
intent.setType("image/jpeg");
Intent chooser = Intent.createChooser(intent, "Blah");
// Add the stupid HTC-Sense-specific secondary intent
Intent htcIntent = new Intent("android.intent.action.SEND_MSG");
htcIntent.putExtra(Intent.EXTRA_STREAM, data);
htcIntent.setType("image/jpeg");
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { htcIntent });
// Show the chooser
startActivity(chooser);
First of all, +1 to Reuben, he is the genius, not me. But I had to modify his code a bit to get it to work. Basically I had to putExtra() on the htcIntent or the image never got stuck to the Intent.
Tested and validated on a Droid X and HTC Incredible (which had the same problem until now thanks to Reuben).
Uri uri = Uri.fromFile(new File(mFile));
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.setType("image/png");
Intent htcIntent = new Intent("android.intent.action.SEND_MSG");
htcIntent.setType("image/png");
htcIntent.putExtra(Intent.EXTRA_STREAM, uri);
Intent chooser = Intent.createChooser(intent, "Send Method");
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { htcIntent });
startActivity(chooser);
Edit: I realize I'm putting the image on two Intents now, but I couldn't get it to work any other way.
Instead of debugging the intents, why not try to compare how your starting the chooser with how the gallery is doing it. It is open source after all, so instead of trying to guess at the issue with the result, you can debug from the cause.
https://android.googlesource.com/platform/packages/apps/Gallery3D

Categories

Resources