Android SDK WebView call Activity - android

I'm trying to launch an Activity when clicking a link inside a WebView component.
My Webview is loaded inside Main.java and I would like to launch SubActivity.java when clicking a link inside the Website which is in Main.java?
Also, how can I pass parameters to this activity?
Example: inspection://Project/1
"Inspection" is the name of my application, inspection is the Activity I would like to launch and 1 is the ID I would like to have.

You could use WebView's addJavaScriptInterface to allow JavaScript to control your application (in this case, to allow JavaScript to fire an Intent when a link is clicked).
To do this you need to pass a class instance to bind to JavaScript, this could be something like the following:
private final class JsInterface {
public void launchIntent(final String payload) {
Activity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
// use the payload if you want, attach as an extra, switch destination, etc.
Activity.this.startActivity(new Intent(Activity.this, SomeOtherActivity.class));
}
});
}
}
Then you add that to the WebView with something along these lines:
webView.addJavascriptInterface(js, "Android");
Then in JavaScript from the WebView you just use your new "Android" object's "launchIntent" method.

One option is to override the URL loading of the web view and pick up the click you want to launch the new activity. Check out the use of a subclassed WebViewClient in the docs:
http://developer.android.com/resources/tutorials/views/hello-webview.html
Another option is to bind your activity to a custom URL scheme. It's pretty common in doing client side OAuth, frequently through the browser instead of a WebView, but it should work similarly enough. There's a full example here:
https://github.com/brione/Brion-Learns-OAuth
Which shows how to bind the url scheme handler as well as handling getting launched via the Intent it generates (see onResume() in OAUTH.java).

First Add javascript method on your webpage's button onclick()
The function defined below..
function showAndroidToast(toast) {
AndroidFunction.showToast(toast);
}
Android Code is here for javascript Interface..
public class MyJavaScriptInterface {
Context mContext;
private Activity activity;
public MyJavaScriptInterface(Activity activiy) {
this.activity = activiy;
}
#JavascriptInterface
public void showToast(String toast){
Intent i=new Intent(getApplicationContext(), Subactivity.class);
i.putExtra("data", "tosecondActivity");
startActivity(i);
}

Related

Can't get a reference to my Activity inside private inner class in a Webview App

I am creating an android app for a client using a WebView.
There are some links on the client's website that play media. In order to conserve battery, I am trying to call the "FLAG_KEEP_SCREEN_ON" when a user clicks on this media link within the website. My webview uses a new MywebViewClient() in order to handle the loading of webpages inside the webview. MywebViewClient is actually a private class that overrides shouldOverrideUrlLoading(). Since this method detects when the user clicks on the link that plays media files, I tried to apply "FLAG_KEEP_SCREEN_ON". If it is not one of the pages that plays media files , I clear "FLAG_KEEP_SCREEN_ON".
The problem I am having is that since this method is inside a private class, I can't seem to get a reference for my Activity. Perhaps the call: getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); doesn't know which window to apply this flag to. I am sure its probably something minor, but I just cant seem to get the Activity that hosts the windows from inside this inner class. Here is my code so far:
Webview w;
//inside onCreate
{
w.setWebViewClient(new WebViewClient());
}
private class MyWebViewClient{
if (Uri.parse(url).getHost().equals("WebsitesHost")) {
if(w.getUrl().equals("LinkThatPlaysMediaSound" &&
!getWindow.hasFeature(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)){
getWindow.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
if(!w.getUrl().equals("LinkThatPlaysMediaSound")
getWindow.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
Add a constructor to the MyWebViewClient which takes Activity instance.
private MyWebViewClient(Activity activity)
{
this.activity = activity;
}
Pass the activity instance while creating the MyWebViewClient
new MyWebViewClient(this);
If your inner class is not static(but you consider using static), you could simple use, YourActivityName.this for activity instance.

How can i make a relation between an image button and a function or activity?

I must write a program with android which can find ssid and show it. My problem is how can i make a relation between an image button in first page and an activity or function in other page.
Buy the way i'm beginner and download the code of searching method because of that i can not recognize which one is the main method or function for pass it to setonclick method that i write for image button in first page?
please answer as soon as you can i need it immediately.
If you are trying to call another activity's method from your existing activity then you should use these steps.
First save the context or instance of the callee activity; for this you can use a class that holds your global data. In this class make the object of this activity.
FirstActivity first = null;
When the callee activity is first created, initialize this instance.
public void setFirstActivity(FirstActivity factivity)
{
first = factivity;
}
And when you do need to call this callee activity's method then access this instance from this global class and with the help of this instance, you will have access to the methods of the callee activity.
public FirstActivity getFirstActivity() {
return first ;
}
use it to get access to the methods or instances of the activity.
Click on the ImageButton, and add the name of the function in the On Click property (Simply the name, like myFunction)
In your code, copy-paste this function and replacemyFunction by the name of the function you chosed
public void myFunction(View v)
{
String ssid = ((EditText)findViewById(R.id.EditTextID)).getText().ToString();
Intent intent = new Intent(this, SecondView.class);
intent.putExtra("SSID_KEY", ssid);
startActivity(intent);
}
And replace SecondView by the class that displays your second page and EditTextName by the ID of your EditText.
In the OnCreate function of your second class, you can get your ssid using the following code
#Override
protected void onCreate(Bundle savedInstanceState) {
String ssid = getIntent().getStringExtra("SSID_KEY");
//Do other work here.
}
Also, on top of the class containing the ImageView, don't forget to add these lines:
import android.view.View;
import android.widget.EditText;

Can you Parameterize an intent creation function?

In order to simplify some code, I'm wondering if it is possible to parameterize code that launches activities in an android application, so that instead of having 5
public void showSettings(View view) {
Intent SettingsActivity = new Intent(MainActivity.this, Settings.class);
startActivity(SettingsActivity);
I can do something like the following
public void showActivity(View view, String ActivityName) {
Intent ActivityName = new Intent(MainActivity.this, ActivityName.class);
startActivity(ActivityName);
Then, for each button in the UI, I simply apply the following to the "onclick" event
showActivity(Settings);
or
showActivity(domains);
This would save about 40-50 lines of code in my app. Obviously I know the above code is incorrect, but I'm not sure if it's possible to do what I'm trying to accomplish.
How about something like:
public <T> void showActivity(View view, Class<T> activity) {
Intent activityName = new Intent(MainActivity.this, activity);
startActivity(activityName);
}
You can invoke it with
showActivity(Settings.class);
I'd recommend use ACTIONs (String) instead of specifying exactly context and class. This way you even can share activities among applications, and if you decide to switch to different activity class, you can edit only android manifest, instead of editing all java source code calls this activity.

Open pop up/external site link in current webview

I am currently writing a webview, it first loads a twitter page (say,
NHL, http://twitter.com/nhl)
as you can see, you can find the tweet for NHL, and each NHL tweet has
another link for user to click, e.g. bit.ly/ujcNZo
in my webview, if i click that link (i.e. bit.ly/ujcNZo), my webview,
after 1 second, doesn't display anything but a twitter icon on a white
color background, it should load the content but it didn't.
after some time of investigation, i think it has to do with the fact
that the link in the tweet (i.e. bit.ly/ujcNZo) actually opens the
link in a separate window (pop up?) and not the current page where the
link is posted, i verified this by going to NHL's twitter page on a
browser in my laptop.
My Question is,
1. is there a way i can load the content of the external link (bit.ly/
ujcNZo, for instance) in my current webview?
You can control this through the WebViewClient class. Simply extend it and override the default implementation to get the desired configuration then set it as the client for your current webview.
Activity
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set the webViewClient to a new instance of your custom WebViewClient
webview.setWebViewClient(new WebActivityClient( this ));
}
Custom Client
/** WebViewClient class for WebView in WebActivity */
private class WebActivityClient extends WebViewClient {
public static final String TAG = "WebActivityClient";
private Context context;
/** Constructor used to grab the context */
public WebActivityClient( Context context ) {
this.context = context;
}
/** Override to load every link within the page inside this webview instead of using
* android's default application
* #7 http://developer.android.com/resources/tutorials/views/hello-webview.html */
#Override
public boolean shouldOverrideUrlLoading( WebView view, String url ) {
view.loadUrl(url);
return true;
}
}
Hope this helps!

New Activity nullpointerexception

I have a beginners problem. Here is my situation:
I want to start a new activity from the main activity. The code to launch the new activity is found in a separate class file. I seem to be passing the wrong arguments and I am ending up in a nullpointerexception when trying to launch the new activity. The new activity launches fine when I place the code in the main activity class file, therefore the second activity and the manifest are fine. Here is a sample of my code:
In my main activity class where I instanciate the second class (THIS IS MY MAIN ACTIVITY. I OMITTED THE REST BECAUSE I DO NOT THINK IT IS RELATED TO THE PROBLEM):
Tester mytest = new Tester();
mytest.test(this);
In my second class file (THIS IS NOT AN ACTIVITY; IT IS A CLASS THAT IS INSTANTIATED IN THE ACTIVITY):
public class Tester extends Activity {
Intent myIntent;
public void test (Context context) {
myIntent = new Intent (Intent.ACTION_VIEW);
myIntent.setClass(context, newActivity.class);
thebutton.setOnClickListener(
new OnClickListener() {
public void onClick(View v) {
startActivity(myIntent);
}
}
):}
When I perform the click I receive a nullpointerexception at startactivity. Can anyone enlighten me on this please?I am sure that I am wrongly using the context.
Activities are started with Intents. Please read the Android Application Fundamentals first and try the Hello World app :)
I understood that you will use your separate Tester class at all cost ;) so I'm trying to adapt and help you out there.
First of all, don't let your class inherit from Activity. This won't help you, cause this calls will probably not have any valid context. Activity somehow implements the template pattern, providing you key method like onCreate(...), onPause(...) etc and is instantiated by the Android OS.
If you still want to use the class, you have to pass in the context. Probably you're aiming for some MVC/MVP pattern structure, anyway.
public class Tester {
private Context context;
public Tester(Context context){
this.context = context;
}
public void test () {
final Intent myIntent = new Intent(context, NewActivity.class);
//guess this comes from somewhere, hope through a findViewById method
thebutton.setOnClickListener(
new OnClickListener() {
public void onClick(View v) {
context.startActivity(myIntent);
}
}
)};
}
}
This would be a proposed solution from my side. A problem I still see here is on how you retrieve the button in that test() method. In order to have that work properly you have to retrieve it from some View class (with view.findViewByid(R.id.myButton)) or to create it dynamically and associate it with the view during the onCreate(...) of your Activity (probably using an Inflater).

Categories

Resources