Preference: OnClick directing towards social network - java

I created a Preference Activity where I put various Preference for Social Networks (Facebook, Twitter, Google+), I would like clicking on these preference you may be directed to the respective pages, how can I?

It will open browser with given URL:
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.facebook.com"));
startActivity(browserIntent);
If you want to open app, try this:
Intent intent = new Intent("android.intent.category.LAUNCHER");
intent.setClassName("com.facebook.katana", "com.facebook.katana.LoginActivity");
startActivity(intent);

Related

How do I verify if my share app link is working in android studio before publshing app?

I have added a share app button and link to my app made in android studio but I'm not sure if this is the way it should work as I have checked other apps' share options and they tend to give the user an option on where exactly they'd like to share their application, while my link sends the user straight to the app page. I found this method on another thread made on this website but I'm not sure if it works properly as I haven't published my app yet. Can someone verify if this is the most popular method to share apps and if not can someone redirect me to where I can find the best solution for a share app option.
segment of my MainActivity.java code regarding the share method:
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_share:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=com.mydomain.tapp"));
startActivity(intent);
break;
}
drawerLayout.closeDrawer(GravityCompat.START);
return true;
}
You are using the wrong Intent Action for your requirement. Your requirement is to share your app's PlayStore link with another person. So, you have to use ACTION_SEND.
ACTION_VIEW is used to display the data to the user.
ACTION_SEND is used to deliver some data to someone else.
For example,
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "Sharing URL");
intent.putExtra(Intent.EXTRA_TEXT, "http://play.google.com/store/apps/details?id=com.mydomain.tapp");
startActivity(intent);

Opening phone apps through button

I have about 12 buttons that each button opens phone apps by it's name.
Now i can open messages and phone call but for other ones i don't know what should i put.
This is my code for phone call:
phone.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent phonecall = new Intent(Intent.ACTION_VIEW);
phonecall.setData(Uri.parse("tel:"));
startActivity(phonecall);
}
});
"tel:" will open phone call, now i want to know what should i put in "tel:" for other apps, Here's my other buttons need to open:
Camera, Contacts, Browser, File Manager, Settings, Gallery, Clock, Telegram application, Instagram application, Whatsapp application.
This is a part of Implicit Intent.
If you want the Android system to handle the intent for a particular task, you can use this.
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel:" + Constants.CALL_CENTER_NUMBER));
startActivity(callIntent);
Example: You can fire an Intent.ACTION_DIAL or Intent.ACTION_CALL without specifying any particular package name to handle this and Android system will handle this intent.
If you want your intent to be handled by a particular application then you can specify the package name of the application.
Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.whatsapp");
startActivity( launchIntent );

Android data turning on android data settings on button click

I have an app that does a check if data services is turned on and if it is not turned let display and alert dialog demanding the user to turn the app on clicking ok. This is my code that is supposed to show the settings of the android phone
final Intent intent=new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
final ComponentName cn = new ComponentName("com.android.phone","com.android.phone.Settings");
intent.setComponent(cn);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
The above code does not display the option to turn on the wifi of the user
Please why the wifi data not turned on, on clicking the button
To enable the option to allow the user to turn on data services such wifi and others.
startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), 0);
Just call the generic Settings screen (ACTION_SETTINGS)
Reference link http://developer.android.com/reference/android/provider/Settings.html
That will do the trick

intent to youtube app profile/channel

I am able to intent to the youtube app to view a video easily enough, but how about getting to a profile / channel?
public void YouTube(String id) {
// Play Youtube Video
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:"+id));
mContext.startActivity(i);
}
I.. just don't know where to really begin here? Is there a specific Uri to parse? I've tried scouring the internet of course and I'm coming up dry for answers. Is it even possible in the first place?
Thanks guys!
By doing the following ,one can launch Youtube App to display channel directly
Intent intent=null;
try {
intent =new Intent(Intent.ACTION_VIEW);
intent.setPackage("com.google.android.youtube");
intent.setData(Uri.parse(url));
startActivity(intent);
} catch (ActivityNotFoundException e) {
intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
}
And in order to display channel,keep in mind to give url in format http://www.youtube.com/user/channelName
As of now, there is no specific URI scheme for channels that would trigger the YouTube application directly. The vnd.youtube scheme is defined only for the activity that plays a single video. So, you have to specify the canonical YouTube URL for the channel page, and typically let the user pass through the application chooser dialog - assuming that the device has the YouTube application installed, the dialog would display at least two entries, the second being for the browser.

Android open browser from service avoiding multiple tabs

I am trying to open the browser window from my service with a link that opens in the current tab of the browser. when I use
Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.google.com"));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(intent);
If the browser was opened and in the foreground before my service starts the intent and the browser opens the link in the same window/tab. If the Browser was minimized and the service starts the intent, the browser opens the webpage in a new window/tab.
I cannot use a web-view as it needs to be in the web browser and it will only be dealing with the default browser. I have checked out the thread at Open an url in android browser, avoid multiple tabs but it did not have an answer. I also have tried force closing the browser but that is also does not work.
Thank you for any help!
If you want to use your app to fire that intent .. so call your app directly through package name
check the following code:
Intent in = getPackageManager().getLaunchIntentForPackage("com.package.myappweb");
in.putExtra("myURL","http://www.google.com");
in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity( in );
you can pass the URL through intent and get it in your app by:
String url=getIntent().getStringExtra("myURL");
if(url!=null){
/// launch app and use one tab for showing the webpage
}else{
//launch app normally.
}
the code you mentioned used for using app as web-app ..that will show popup window to use to pick up any browser to deal with the link .. not that would be for any app wanna open link
good luck,
The way to avoid multiple tabs is as follows:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(uri);
intent.putExtra(Browser.EXTRA_APPLICATION_ID, getPackageName());
startActivity(intent);
The EXTRA_APPLICATION_ID will contain an ID of your app, and the browser will recycle a tab that was opened by your application, rather than opening a new tab. In this way, all the pages opened by your application will share the same tab, and you will not have "tab inflation" in the browser.
None of the solutions above involving Browser.EXTRA_APPLICATION_ID work.
However, Google's Custom Tabs which is now the standard for opening URL's was easy to implement and works like a charm. Here's all you need to open a tab:
implementation "androidx.browser:browser:1.4.0"
String url = "https://google.com/";
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(this, Uri.parse(url));
More can be found here.

Categories