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);
}
}
Related
In Java, I am trying to implement a feature where only the admin (person who knows the device password) can access an information screen and when they click on a button within the app in the MainActivity, the lock screen will appear as a form of authentication and display another activity screen on success. Is this possible?
So far, I noticed that the authentication only displays when I open the app and not when I press the button in an onClickListener. Most of the solutions I've seen are doing it this way. I have the code in MainActivity within an onCreate() method.
MainActivity
ActivityResultLauncher<Intent> activityResultLaunch = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
#Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == Activity.RESULT_OK) {
// There are no request codes
Intent data = result.getData();
} else {
finish();
}
}
});
start_end_button_add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
authScreen(activityResultLaunch);
}
});
private void authScreen(ActivityResultLauncher<Intent> activityResultLaunch) {
KeyguardManager mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
if (!mKeyguardManager.isKeyguardSecure()) {
// Show a message that the user hasn't set up a lock screen.
} else {
Intent intent = mKeyguardManager.createConfirmDeviceCredentialIntent(null, null);
if (intent != null) {
startActivityForResult.launch(intent, REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS);
}
}
Intent intent = new Intent(DetectorActivity.this, SearchActivity.class);
startActivity(intent);
finish();
}
Currently, the app immediately goes to the SearchActivity class without having the lock screen displayed in between. Even if I get into the app after PIN code entered is a success, it still doesn't get into the activityResultLaunch success condition as per the new implementation referenced here by user Martin Zeitler.
Reference:
https://mobile-security.gitbook.io/mobile-security-testing-guide/android-testing-guide/0x05f-testing-local-authentication#:~:text=In%20Android%2C%20there%20are%20two,and%20the%20Biometric%20Authentication%20flow.
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);
}
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);
}
I have a view with few buttons, each buttons suppose to open a different url in firefox, but instead after one of the buttons clicked and the url opens in firefox, all the other buttons just opens firefox but not navigating to their url.
this is the code I am using for the intent:
Intent i = new Intent("android.intent.action.MAIN");
i.setComponent(new ComponentName("org.mozilla.firefox", "org.mozilla.firefox.App"));
Bundle b = new Bundle();
b.putBoolean("new_window", true); //suppose to set the new window
i.putExtras(b);
i.addCategory("android.intent.category.LAUNCHER");
i.setData(Uri.parse(url));
startActivity(i);
How should I code it to open each button click on new tab.
Use the Intent.ACTION_VIEW:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
// ...
openpage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri.parse("your url")));
}
});
Should work
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.