I'm trying my hand at developing an app for android, and the java is a bit confusing. The code I have so far is....
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButtonClick = (Button) findViewById(R.id.ButtonClick);
final String link = "htttp://www.stackoverflow.com";
ButtonClick.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
URI uri;
try {
uri = new URI("http://www.stackoverflow.com");
} catch (URISyntaxException e) {
e.printStackTrace();
}
browserIntent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(browserIntent);
}
});}}
The issue with the way the code is currently written is when I try to assign the new intent (browserIntent), uri in what I understand to be the "data" part of that object. Android Studio says that uri cannot be converted to a URI, but it's declared as a URI only a couple of lines above!
I also tried to insert a string of text, initialized above as well, which looks like...
browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(link);
This compiles fine, but when I actually click on the button it says that there is no activity to handle the intent.
So my questions in order are:
1) If I declare the URI uri variable earlier in the code, and the second property of the intent is looking for a URI, why is there an error when I simply feed it the variable uri?
2) In the second case, when I am trying to parse the string, where there is no activity to handle the intent, is this issue with ACTION_VIEW? It seems that its able to find the URI fine, however it can't actually go through with opening a browser. Perhaps
3) What is the simplest java api or java tutorial that would cover this issue that you have found or know of?
AS must be saying something like "uri is not initialized", because due to try-catch block that isn't guaranteed. So, we change the code as following:
EDIT:
Silly me, we need to use the Uri class instead of URI class as there is no constructor new Intent(String action, URI uri) but there is one for new Intent(String action, Uri uri). For differences between URI and Uri, visit here.
ButtonClick.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Uri uri;
try {
uri = Uri.parse("http://www.stackoverflow.com");
browserIntent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(browserIntent);
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
});}}
Related
I am getting this warning on resolveActivity line while using implicit intents and I am unable to open any website because of that.
btnWeb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String v = et.getText().toString();
Uri uri = Uri.parse(v);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
if (intent.resolveActivity(getPackageManager())!=null){
startActivity(intent);
}
}
});
Get rid of resolveActivity(). Just call startActivity(), but do so in a try/catch, so you can catch the ActivityNotFoundException if there is no Web browser available.
My app contains a simple Fragment that is used to open external web pages, with:
Intent intent = new Intent(Intent.ACTION_VIEW, externalUrl); // Uri
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Intent chooserIntent = Intent.createChooser(intent, "Open URL...");
startActivityForResult(chooserIntent, RC_OPEN_URL);
And, when the result is returned (in my Fragment's onActivityResult(...)), the Fragment is popped off the backstack.
But, I'm now getting the new deprecation warning:
startActivityForResult(Intent,int) in Fragment has been deprecated
I've read the corresponding Getting a result from an activity documentation, but I'm not sure how to adapt the example they provide for my particular case.
I have discovered the ActivityResultContracts.StartActivityForResult class, but can't figure out how to pass my chooserIntent to it.
All the online examples for the class seem to be in Kotlin and I've not had any joy trying to decompile them to Java. So a Java example of how to use the new registerForActivityResult() method to open an external URL would be much appreciated.
There's no reason to use startActivityForResult() at all for createChooser() - you can use startActivity and run your code from onActivityResult() immediately after you call to startActivity:
Intent intent = new Intent(Intent.ACTION_VIEW, externalUrl); // Uri
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Intent chooserIntent = Intent.createChooser(intent, "Open URL...");
startActivity(chooserIntent);
// Do your code from onActivityResult() here
However, if you really want to use the Activity Result API, then you can directly adapt the examples in the documentation, replacing the example GetContent contract with the StartActivityForResult contract:
// Create this as a variable in your Fragment class
ActivityResultLauncher<Intent> mLauncher = registerForActivityResult(
new StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
#Override
public void onActivityResult(ActivityResult result) {
// Do your code from onActivityResult
}
});
private void triggerChooser(Uri externalUri) {
Intent intent = new Intent(Intent.ACTION_VIEW, externalUrl); // Uri
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Intent chooserIntent = Intent.createChooser(intent, "Open URL...");
mLauncher.launch(chooserIntent);
}
Below answer may help to someone.... But this is not readymade solution to above question.
I have faced lot of issue when I try to get result from activity to fragment.
Finally I found below solution.
In side the fragment, I created ActivityResultLauncher.
var myActivityResultLauncher: ActivityResultLauncher<Intent> = registerForActivityResult<Intent, ActivityResult>(
ActivityResultContracts.StartActivityForResult(),
ActivityResultCallback<ActivityResult> {
// ToDO:
if (it.resultCode == AppCompatActivity.RESULT_OK) {
}
}
) as ActivityResultLauncher<Intent>
And when I start the activity I used below code.
myActivityResultLauncher.launch(myIntent)
I wrote this code to open a website in the browser from my app in android studio(google):
String google = "http://www.google.com";
Uri webAddress = Uri.parse(google);
Intent goToGoogle= new Intent(Intent.ACTION_VIEW, webAddress);
if(goToGoogle.resolveActivity(getPackageManager()) != null) {
startActivity(goToGoogle);
}
the app just does what I want it to do when I do not put the if statement, otherwise the button does nothing. Why is that?
thanks
The resolveActivity() method returns the Activity Component that is used to handle the Intent, so, if there is an Activity handling the intent, it will return true
Make sure that an Activity is handling your intent, placing this code into a java class of an Activity.
String google = "http://www.google.com";
Uri webAddress = Uri.parse(google);
Intent goToGoogle= new Intent(Intent.ACTION_VIEW);
goToGoogle.setData(webAdress);
if(goToGoogle.resolveActivity(getPackageManager()) != null) {
startActivity(goToGoogle);
}
I know that this question has been asked several times before, I am trying to add caption to image shared to instagram using send intent
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/*");
shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shareIntent.putExtra(Intent.EXTRA_STREAM,uri);
shareIntent.putExtra(Intent.EXTRA_TEXT,"YOUR TEXT TO SHARE IN INSTAGRAM");
shareIntent.setPackage("com.instagram.android");
return shareIntent;
Has someone ever managed to make it work?
Is it not supported or has the support been revoked?
There was an official statement from Instagram (mid-2015) announcing that pre-populated captions would no longer be accepted in the iOS and Android apps:
Beginning today, the iOS Hooks and Android Intents will stop accepting captions passed by third party apps. This is a non-breaking change: existing mobile apps that utilize pre-filled captions will continue to be able to use this flow to share media through the Instagram apps, but now Instagram will ignore the caption text. To create a caption for a photo or video shared by a third party app, users will have to enter a caption manually, the same way they already do when sharing content using the Instagram native apps.
Looking at the Instagram documentation for Android, indeed we see that there's no mention of providing the conventional Intent.EXTRA_TEXT string extra in the intent as is customary for other apps. Their sample is limited to only providing a Uri:
// Add the URI to the Intent.
share.putExtra(Intent.EXTRA_STREAM, uri);
// Broadcast the Intent.
startActivity(Intent.createChooser(share, "Share to"));
I'm sorry to say that it simply isn't possible, and we're at the discretion of Facebook in making this decision.
Until it`s not solved by Instagram, I copy the text to the clipboard and instruct the user to paste it
#Override
public void onSingleImageSelected(Uri uri, String tag) {
fileProfileImage = uri.getPath();
compressProfileImage();
imgShareTosocial.setVisibility(View.VISIBLE);
Glide.with(getApplicationContext()).load(uri).into(imgShareTosocial);
}
#SuppressLint("CheckResult")
private void compressProfileImage() {
File file = new File(fileProfileImage);
new Compressor(this)
.compressToFileAsFlowable(file)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<File>() {
#Override
public void accept(File file) throws Exception {
compressProfileImage = file;
String imagePath = compressProfileImage.getAbsolutePath();
tvSelectMedia.setText(imagePath);
}
}, new Consumer<Throwable>() {
#Override
public void accept(Throwable throwable) throws Exception {
throwable.printStackTrace();
}
});
}
private void shareToInstagram() {
path = tvSelectMedia.getText().toString().trim();
Intent intent = getPackageManager().getLaunchIntentForPackage("com.instagram.android");
if (intent != null) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setPackage("com.instagram.android");
try {
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(), path, "Step Up", "Step Up")));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
shareIntent.setType("image/jpeg");
startActivity(shareIntent);
} else {
// bring user to the market to download the app.
// or let them choose an app?
intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setData(Uri.parse("market://details?id=" + "com.instagram.android"));
startActivity(intent);
}
}
I'm with the same problem. I think is not possible at this time.
In https://instagram.com/developer/mobile-sharing/android-intents/ only talk about Intent.EXTRA_STREAM, so i suppose that it's the only available.
Here is my code:
Intent instagramIntent = new Intent(Intent.ACTION_SEND);
instagramIntent.setType("image/*");
File media = new File(mediaPath);
Uri uri = Uri.fromFile(media);
instagramIntent.putExtra(Intent.EXTRA_STREAM, uri);
instagramIntent.setPackage("com.instagram.android");
PackageManager packManager = getPackageManager();
List<ResolveInfo> resolvedInfoList = packManager.queryIntentActivities(instagramIntent, PackageManager.MATCH_DEFAULT_ONLY);
boolean resolved = false;
for(ResolveInfo resolveInfo: resolvedInfoList){
if(resolveInfo.activityInfo.packageName.startsWith("com.instagram.android")){
instagramIntent.setClassName(
resolveInfo.activityInfo.packageName,
resolveInfo.activityInfo.name );
resolved = true;
break;
}
}
if(resolved){
startActivity(instagramIntent);
}else{
Toast.makeText(PromocionarMain.this, "Instagram App is not installed", Toast.LENGTH_LONG).show();
}
Instagram have stopped accepting pre-populated capitions to increase the quality of content in the system. See this post.
http://developers.instagram.com/post/125972775561/removing-pre-filled-captions-from-mobile-sharing
I have a link to some page (Instagram), when I click on the link, it takes me to the link and the username to that like in the mobile browser, what should I type in order to work it in Instagram application in the mobile? If it doesn't exist, it will go to play.google/instagram in the play store
This is the link but it's not what I want
blah.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.instagram.com/blahhhh"));
startActivity(browserIntent);
}
});
try {
Intent LaunchIntent =
getPackageManager().getLaunchIntentForPackage("com.instagram.android");
startActivity(LaunchIntent);
//Here you can add more information, such as the specific profile that you want to launch, in Intent format
}
catch (Exception myIntent) {
//TODO Auto-generated catch block
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=com.instagram.android"));
startActivity(browserIntent);
}