Reputation: 17269
I tried to open link in a browser in JavaScriptInterface but it seems it didn't work. What the problem with the code below:
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
public class JavaScriptInterface extends Activity{
Context mContext;
JavaScriptInterface(Context c) {
mContext = c;
}
public void openLink(){
Uri uri = Uri.parse("http://www.google.com");
startActivity(new Intent(Intent.ACTION_VIEW, uri));
}
}
I already have the required permission:
<uses-permission android:name="android.permission.INTERNET" />
Upvotes: 0
Views: 114
Reputation: 913
I don't know why are you extending an Activity
. Though you are initializing mContext
in the constructor, it will never be used for startActivity()
.
You may want to code like:
public class JavaScriptInterface {
Context mContext;
JavaScriptInterface(Context c) {
mContext = c;
}
public void openLink(){
Uri uri = Uri.parse("http://www.google.com");
mContext.startActivity(new Intent(Intent.ACTION_VIEW, uri));
}
}
(not extending an Activity, and calling startActivity()
in mContext
)
BTW, you don't need the permission android.permission.INTERNET
to invoke this intent. You need it for a WebView
, may be.
Upvotes: 1