Android Implicit Intent for Viewing a Video File - java

In my Android app, I have a button that when clicked, launches the external application of my choice to play a video (I gather that this is called an "implicit intent"). Here is the relevant Java code from my onCreate method.
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener
(
new Button.OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.parse("https://youtu.be/jxoG_Y6dvU8"), "video/*");
startActivity(i);
}
}
);
I expected this to work, since I've followed tutorials and the Android developers documentation pretty closely, but when I test my app in the AVD, instead of prompting a menu of external applications where I can view my video, the app crashes.
What is causing my app to crash?

Change your onClick method to below code. You should give the option to choose the external player.
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("https://youtu.be/jxoG_Y6dvU8"), "video/*");
startActivity(Intent.createChooser(intent, "Complete action using"));
}

Change your code to add this check:
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.parse("https://youtu.be/jxoG_Y6dvU8"), "video/*");
// Check there is an activity that can handle this intent
if (i.resolveActivity(getPackageManager()) == null) {
// TODO No activity available. Do something else.
} else {
startActivity(i);
}

Related

App crashes when switching from activity 1 to activity 2

In my app, there are so many activities which are referred to as levels. And one activity is Reward activity. when i win level-1, reward activity opens. Now i want to replay the level-1. For this i have used getExtra(). My app crashes when i click the replay button.
Houselevel1.java
public void getReward(){
if(count == 3) {
Intent intent = new Intent("com.creatives.arfa.revealthesecretsgame.Reward");
intent.putExtra("activity", "level1");
startActivity(intent);
}
}
HouseLevel2.java
public void getReward(){
if(count == 3) {
Intent intent = new Intent("com.creatives.arfa.revealthesecretsgame.Reward");
intent.putExtra("activity", "level2");
startActivity(intent);
}
}
Reward.java
public void replayLevel() {
replay = (ImageButton) findViewById(R.id.replay);
Intent intent= getIntent();
activity = intent.getStringExtra("activity");
replay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View paramView) {
if(activity.equals("level2")){
Intent intent = new Intent("com.creatives.arfa.revealthesecretsgame.HouseLevel2");
startActivity(intent);
}
if(activity.equals("level1")){
Intent intent = new Intent("com.creatives.arfa.revealthesecretsgame.Houselevel1");
startActivity(intent);
}
}
});
}
With the java code you've posted, in the Reward.java file, you're trying to create another Intent Object with the same name as the one declared in the scope right above it. Because of this, the build will never be successful.
Also, when you declare intents, you MUST pass on the activity_name.class file.
Something you can try:
1) HouseLevel1.java
public void getReward(){
if(count == 3) {
Intent intent = new Intent(getApplicationContext(), com.creatives.arfa.revealthesecretsgame.Reward.class);
intent.putExtra("activity", "level1");
startActivity(intent);
}
}
2) HouseLevel2.java
public void getReward(){
if(count == 3) {
Intent intent = new Intent(getApplicationContext(), com.creatives.arfa.revealthesecretsgame.Reward.class);
intent.putExtra("activity", "level2");
startActivity(intent);
}
}
3) Reward.java
public void replayLevel() {
replay = (ImageButton) findViewById(R.id.replay);
Intent intent= getIntent();
activity = intent.getStringExtra("activity");
replay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View paramView) {
if(activity.equals("level2")){
Intent intent = new Intent(getApplicationContext(), com.creatives.arfa.revealthesecretsgame.HouseLevel2.class);
startActivity(intent);
}
else if(activity.equals("level1")){
Intent intent = new Intent(getApplicationContext(), com.creatives.arfa.revealthesecretsgame.Houselevel1.class);
startActivity(intent);
}
}
});
}
Also, if you're simply using the Reward.java file to get the previous intent's data, perform some calculation, and send some data back to the calling, or parent activity, then you can simply use the startActivityForResult() method, which takes care what what you're trying to do manually.
Here's a small article that might be able to help you with the problem
http://www.vogella.com/tutorials/AndroidIntent/article.html#retrieving-result-data-from-a-sub-activity
If all you want is go from Activity 1 or to 2 to a Reward activity grab something and send that something back to either activity.
What you do is startActivityForResult You pass an Id (constant number) do what you do on the Reward activty, pack what you need to return in a Bundle, and set ActivtyResult to OK and close your activity.
Your app will go back to the Activity1 or 2 whoever call it. On those activties you override the method onActivityResult There you check if the id on which the result is coming from is the Id you sent on the startActivityForResult and if the status is OK.
Then you have whatever was set on the Reward activity. The Reward activity don't need to know from where it came from if only will grab some data. So you can later have an Activity3 that calls the Reward activity and you do not need to modify the Reward activity.
It is explain here check the accepted answer.
How to manage `startActivityForResult` on Android?

Intent ACTION_SEND doesn't show every browser

I have a button which fires a ACTION_SEND intent when clicked as below:
private static final String WEB_URL = "https://www.google.ca/";
#Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, WEB_URL);
intent.setType("text/plain");
startActivity(Intent.createChooser(intent, "CHOOOOOSE"));
}
Currently, it has some other apps, 'Copy to clipboard', and 'Add to Firefox' which can be used to open the link in Firefox. However, I want to let a user to decide which browser app to use to open the link but 'Add to Firefox' seems to be the only option right now when this device I am using has 'Chrome' and 'Internet' applications as well.
Ultimately, what I want is a share button, and on a click event, it shows all installed browser app like chrome, firefox, 'internet', etc., 'Copy to clipboard', and any other apps(<- these are not necessary, though).
Showing all browser apps, and a button to 'Copy to clipboard' is what I essentially want.
The 'WEB_URL' string is always going to be a proper url.
How do I achieve this?
EDIT
To sum up:
I want to have a list of apps shown by 'Intent.createChooser()' whose list consist of ALL browser apps AND a 'Copy to clipboard' option.
I tried using Intent.ACTION_VIEW with intent.setData(Uri.parse(url)) but then in this case, it doesn't have the 'Copy to clipboard' option.
Solution
I was able to achieve what I wanted above after some research.
The key is to use 'Intent.EXTRA_INITIAL_INTENTS' and a custom Activity.
SomeActivity's onClick Event
private static final String WEB_URL = "https://www.google.ca/";
#Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse(WEB_URL));
Intent clipboardIntent = new Intent(SomeActivity.this, CopyToClipboardActivity.class);
clipboardIntent.setData(Uri.parse(WEB_URL));
Intent chooserIntent = Intent.createChooser(intent, "Custom Title...");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {clipboardIntent});
startActivity(chooserIntent);
}
Add 'CopyToClipboardActivity' to Manifest
<activity android:name=".activities.CopyToClipboardActivity"
android:exported="false"
android:icon="#drawable/someIcon"
android:label="#string/copy_to_clipboard"
android:theme="#android:style/Theme.NoDisplay"/>
CopyToClipboardActivity.java
public class CopyToClipboardActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Uri uri = getIntent().getData();
if (uri != null) {
copyTextToClipboard(uri.toString());
Toast.makeText(this, "Link copied to clipboard", Toast.LENGTH_SHORT).show();
}
// Finish right away. We don't want to actually display a UI.
finish();
}
private void copyTextToClipboard(String url) {
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("URL", url);
clipboard.setPrimaryClip(clip);
}
}

How to avoid complete action using dialog to read a contact or to do a call phone

In my app i have a button to select a contact from contacts phone and a button to start a call phone to this number. So when i click on the button to select the contact, the complete action using dialog appears with more apps to choose as well as when i click on the button to star the call phone. How can i avoid the dialog to access contacts and to do a call phone directly?
Partial code of my activity:
contacts.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, 0);
}
});
start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String numeroDiTelefono = dati.getString("numeroDiTelefono");
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + numeroDiTelefono));
callIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(callIntent);
}
});
Make sure you have added the permission in the Manifest file:
<uses-permission android:name="android.permission.CALL_PHONE" />
And all you should need for the Intent is:
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:"+numeroDiTelefono));
startActivity(callIntent);
Basically that dialog means you have more than one contacts app installed on your phone. This is a default Android system behavior when you call any kind of common intent actions.
What you can do is make the intent more specific to the app you're looking for.
By specifing
a) the specific data uri
b) the package name
c) set the content type, etc
Also try this.
Intent i = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(i, PICK_CONTACT);
public void onClick(View view) {
String number = String.valueOf(bundle.getLong("phone"));
Uri call = Uri.parse("tel:" + number);
Intent intent = new Intent(Intent.ACTION_DIAL, call);
startActivity(intent);
}

Intent betwen 2 applications

I ask if I can pass to an other application some data using intent. If it's possible, how can I do clicking a button and passing to an other application?
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intention = new Intent(?????);
startActivity(intention);
}
});
To pass data from one activity to another you need to add it to the Intent. E.g,
Intent intention = new Intent(this, DestClass.class);
int value = 10;
intention.putExtra("KEY", value);
startActivity(intention);
and in your DestClass's onCreate(), you get it from the Intent with,
Bundle extras = getIntent().getExtras();
if (extras!= null) {
extras.getInt("KEY");
}
To send it to another application. Similarly you create an Intent as shown in the Android docs.
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(sendIntent);
Note that in this case the Android system allows apps to register to receive Intents and if there are multiple apps that have registered for these Intents then the system lets the user choose which App they would like to handle the Intent.

How to clear an activity from the second activity going back to home screen in android?

example scenario is:
from login screen - main screen - then when i clicked a hide button the app will go to home screen, and when im going to click the app again the main screen would be called
Fire an intent when you want to display the home screen
Intent setIntent = new Intent(Intent.ACTION_MAIN);
setIntent.addCategory(Intent.CATEGORY_HOME);
setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(setIntent);
So this will be fired on the pressing of your hide button
I think you can use you can use FLAG_ACTIVITY_CLEAR_TOP
FirstActivity is the first activity in the application:
public static void home(Context ctx) {
if (!(ctx instanceof FirstActivity)) {
Intent intent = new Intent(ctx, FirstActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
ctx.startActivity(intent);
}
}
And If you want to exit from the whole application,this help you in that.
public static void clearAndExit(Context ctx) {
if (!(ctx instanceof FirstActivity)) {
Intent intent = new Intent(ctx, FirstActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Bundle bundle = new Bundle();
bundle.putBoolean("exit", true);
intent.putExtras(bundle);
ctx.startActivity(intent);
} else {
((Activity) ctx).finish();
}
}
i Really Hope this helps.

Categories

Resources