How to get data from Movement Method on TextView in Android - android

I've read the post that everyone links to about link handling clicks with TextViews.
I have a textview with a Movement method set and it seems to be working correctly. When I click on the link it opens a browser and displays the correct information.
My FormatUtils.generateHtml class takes markdown from submission.getContent() and creates usable links as well as formates the text to bold and italics and such.
For some reason I am stumped on how to get that info to load into my own webview.
My manifest for the activity
<activity android:name="com.matteo.example.view.activities.SubmissionDetailActivity">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.VIEW" />
<data android:scheme="com.matteo.example" />
</intent-filter>
</activity>
Linked Textview
CharSequence string = FormatUtils.generateHtml(submission.getContent());
formattedContent.setText(string);
formattedContent.setMovementMethod(LinkMovementMethod.getInstance());
in my Activity with the webview. These are my attepts to get the data in the intent. Am I even close here?
mywebview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return true;
}
});
#Override
public void startActivity(Intent intent) {
if (TextUtils.equals(intent.getAction(), Intent.ACTION_VIEW)) {
Bundle extras = getIntent().getExtras();
intent = getIntent();
String extra = intent.getStringExtra();
Uri data = getIntent().getData();
mywebview.loadUrl(intent.toString());
Toast.makeText(this, "THIS IS INTENT DATA~~ " + intent.getDataString() , Toast.LENGTH_LONG).show();
So far it seems eveything is null.
How do I get the data from the Intent so I can handle it with my webview?

Related

Deep linking the right page inside the webview

I made a android webview app for our website. Now i want to add deep links to the app, like when somebody clicks a link of our website, you can open it with the webview app instead of chrome web browser. I added these to my manifest:
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https"
android:host="www.example.com" />
Now it opens the webview app when I click a link which is related to us, but it doesnt open the right page, its just starting the app again. But I want to open the directly the right page inside the webview app.
EDIT:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//added
Intent intent = getIntent();
String action = intent.getAction();
Uri data = intent.getData();
But it seems that the variables action and data are not used.
I load the webview like this (for three languages).
//loads the main website
//sets the matching language
web2.clearCache(true);
String loc = Locale.getDefault().toString();
if (loc.startsWith("de")) {
web2.loadUrl("https://www.example.de");
} else if (loc.startsWith("nl")) {
web2.loadUrl("https://www.example.nl");
} else {
web2.loadUrl("https://www.example.com");
}
You must use the intent data that your activity receives when its opened via deep link. Example in Kotlin:
class WebViewActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_webview)
val uri = intent.data
webView.loadUrl(uri.toString())
}
Now I did it like this. That is the solution.
Intent intent = getIntent();
Uri data = intent.getData();
String loc = Locale.getDefault().toString();
if (data==null && loc.startsWith("de")) {
web2.loadUrl("https://www.example.de");
} else if (data==null && loc.startsWith("nl")) {
web2.loadUrl("https://www.example.nl");
} else if (data==null && !loc.startsWith("de") && !loc.startsWith("nl")){
web2.loadUrl("https://www.example.com");
} else {
web2.loadUrl(data.toString());
}

How to open a url located at a third party app or a browser (like a whatsapp message) directly into my webview app?

I have a blog and I made a app version of it using just Webview. The problem is: I would like to share my posts through other apps, like sending a whatsapp message or an email to someone with a link to a specific post located on my blog (like www.blogspot.myblog.com/2017/postexample), and then have the option to open this with my app, like Facebook does, or even Youtube does.
I made this:
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="mywebsite.com"
android:pathPrefix="/"
android:scheme="http" />
<data
android:host="www.mywebsite.com"
android:pathPrefix="/"
android:scheme="http" />
</intent-filter>
it does launch my app, but (of course) it opens to the app's default page, not the www.blogspot.myblog.com/2017/postexample, so what should I do to open directly into the sent url? Thanks guys, a help would be greatly appreciated.
EDIT
Here's my code now:
<intent-filter >
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http"
android:host="www.blog.com"
android:pathPrefix="/" />
</intent-filter>
</activity>
And the Activity
public class BlogActivity extends AppCompatActivity {
private static final String TAG = "BlogActivity";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
String action = intent.getAction();
Uri data = intent.getData();
//Get the link from that Uri
if (data != null) {
Log.d(TAG, "Data" + data.toString());
WebView WebView2 = (WebView)findViewById(R.id.web1);
WebView2.setWebViewClient(new WebViewClient());
WebView2.loadData(TAG, "text/html; charset=utf-8", "UTF-8");
WebSettings webSettings = WebView2.getSettings();
webSettings.setJavaScriptEnabled(true);
}
}
}
Have a look at: https://developer.android.com/training/app-indexing/deep-linking.html
From the above link:
Once the system starts your activity through an intent filter, you can use data provided by the Intent to determine what you need to render. Call the getData() and getAction() methods to retrieve the data and action associated with the incoming Intent. You can call these methods at any time during the lifecycle of the activity, but you should generally do so during early callbacks such as onCreate() or onStart().
Here’s a snippet that shows how to retrieve data from an Intent:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = getIntent();
String action = intent.getAction();
Uri data = intent.getData();
//Get the link from that Uri
if(data != null) {
Log.d(TAG, "Data" + data.toString());
}
}

Android redirect uri

I'm working with the instagram API and I don't understand what I should put in for the redirect api. At the moment I simply put in https://127.0.0.1
But I dont really understand. Also, once I get the allow/cancel screen and I press allow it gives me an error saying that I cant go to that address but I can also see the authorization code appended on to the address. So how can i redirect back from my redirect uri? How can I tell android that after user clicks allow to come back into the app use the code for further authentication?
Im sure you will say something like make my own custom scheme/ intent filters etc but please be a little more supportive im new and I dont understand and I did do research on them.
My on resume method is below
#Override
protected void onResume() {
super.onResume();
// the intent filter defined in AndroidManifest will handle the return from ACTION_VIEW intent
Uri uri = getIntent().getData();
if (uri != null && uri.toString().startsWith(redirectUri)) {
// use the parameter your API exposes for the code (mostly it's "code")
String code = uri.getQueryParameter("code");
if (code != null) {
// get access token
LoginService loginService =
ServiceGenerator.createService(LoginService.class, clientId, clientSecret);
AccessToken accessToken = loginService.getAccessToken(code, "authorization_code");
} else if (uri.getQueryParameter("error") != null) {
// show an error message here
}
}
}
This is my manifest snippet dealing with intent filters:
<activity
android:name=".LoginActivity"
android:label="#string/title_activity_login" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="redirecturi"
android:scheme="your" />
</intent-filter>
</activity>
You have to setup an event listener on the browser view and check for code in URL param if the URL is equal to the redirect_uri, and then make POST request to the auth URL using the code and client_secret as documented in Instagram authentication
something like this:
webView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
String code = url.getQueryParameter("code");
// ...
}
});

Redirect Android webview with intent filter

I have a specific URL that I want redirected to a specific activity in my app from a webview with an intent filter. How to implement my very own URI scheme on Android described how to do this for a browser page, but this same intent filter doesn't work when that URL is accessed through the webview. Is there anything else that needs to be added to this intent filter to catch these webview links?
<intent-filter>
<action android:name="android.intent.action.VIEW"></action>
<category android:name="android.intent.category.DEFAULT"></category>
<category android:name="android.intent.category.BROWSABLE"></category>
<data android:host="myurl.com/stuff" android:scheme="http"></data>
</intent-filter>`
I have not got intent filters and webviews to work together just with declaring the intent on the manifest and I think they are not supposed to. (I wonder why...) I think the way to do it is catching urls when you are trying to open them in the webview and creating an intent then.
Then, for an activity register as follows in the manifest:
<activity android:name=".PretendChat">
<intent-filter>
<action android:name="android.intent.action.VIEW"></action>
<category android:name="android.intent.category.DEFAULT"></category>
<category android:name="android.intent.category.BROWSABLE"></category>
<data android:host="chat" ></data>
<data android:scheme="testing"></data>
<data android:pathPattern=".*"></data>
</intent-filter>
</activity>
You would expect the activity PretendChat opens when you click on a link like the following: "testing://chat" inside the webview. In order for that to happen you
would need the following code on the webview client you are using on the webview. Assume that the activity that starts the webview is called WebviewActivity.
private class TestWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
try {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
WebviewActivity.this.startActivity(intent);
} catch(ActivityNotFoundException e) {
Log.e(LOGTAG,"Could not load url"+url);
}
return super.shouldOverrideUrlLoading(view, url);
}
}
The only way I got it to work was to use a dummy file link and loading the URL with loadDataWithBaseURL and using relative links in the data. The latest WebView only seems to accept valid links, and I couldn't get it to work with custom intents for Activities.
If I tried a different scheme, such as "my-app" instead of "file", the url in shouldOverrideUrlLoading would come up as "about:blank".
I also wanted to pass parameters to the activity to be opened in Bundle.
webView.loadDataWithBaseURL("file:///android_asset", w.Definition, "text/html", "utf-8", null);
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("file")) {
Intent intent = new Intent(DictWordActivity.this, DictWordActivity.class);
intent.putExtra("word", Uri.parse(url).getLastPathSegment());
startActivity(intent);
return true;
} else
return false;
}
});
I created my relative links like this:
"" + word + "");
If anyone has a better solution I would like to know.

Android return from browser to app

I have option in my app to start browser and load imdb website.
I'm using ActionView for this.
Intent intent1 = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse(website));
try {
activity.startActivity(intent1);
} catch (Exception e) {
Toast.makeText(activity, R.string.no_imdb, Toast.LENGTH_SHORT)
.show();
}
The problem occurs when I tap on back button.
When default browser app is launched everything is ok.
When Opera Mini app is launched, when I tap on back button, it seems like my app receives two back actions, and finish my current activity.
How to prevent this?
Try starting the intent in a new task:
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Or
intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Please add this code to your android manifest for activity that you need return
<activity
android:name="YourActivityName"
android:launchMode="singleTask">
<intent-filter>
<action android:name="schemas.your_package.YourActivityName" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.ALTERNATIVE" />
</intent-filter>
</activity>
and add this to your web page
click to load app
because only one app has this action name (schemas.your_package.YourActivityName) on your phone, web page directly return to app
Also You can Use Airbnb DeepLink lib
Example
Here's an example where we register SampleActivity to pull out an ID from a deep link like example://example.com/deepLink/123. We annotated with #DeepLink and specify there will be a parameter that we'll identify with id.
#DeepLink("example://example.com/deepLink/{id}")
public class SampleActivity extends Activity {
#Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
if (intent.getBooleanExtra(DeepLink.IS_DEEP_LINK, false)) {
Bundle parameters = intent.getExtras();
String idString = parameters.getString("id");
// Do something with idString
}
}
}

Categories

Resources