Java, android WebView - java

I have built a WebView application in java.
Basically what i did was Created a WebView loaded a url, but what happened is when i click on the Uri tel:, the app crashes i have no idea what to do.
I have tried to override the Web. I did override WebViewClient.
App always crashing if uri = tel something by this code:
public class MyAppWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(Uri.parse(url).getHost().endsWith("html5rocks.com")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
}

Try this:
if (url.startsWith("tel:")) {
Intent intent = new Intent(Intent.ACTION_DIAL,
Uri.parse(url));
startActivity(intent);
}
You need ACTION_DIAL and not ACTION_VIEW.

Related

android java endswith keeps crashing

I'm having a error with my code when i run it...All i want to do to make a webview in order to show a webpage in my app and to check the url if it ends with .m3u8 and then if it ends with .mp4 to open with the mxplayer(it's a media player app from the play store). I got the API of mxplayer from their site and added into my code but the problem is that when the link ends with .mp4 it crashes. Here's my code :
public class MainActivity extends AppCompatActivity {
private WebView myWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView = (WebView) findViewById(R.id.web1);
myWebView.getSettings().setLoadsImagesAutomatically(true);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
myWebView.getSettings().setDomStorageEnabled(true);
myWebView.loadUrl("url");
myWebView.setWebViewClient(new WebViewClient() {
// Api < 24
#Override
public boolean shouldOverrideUrlLoading(WebView myWebView, String url) {
if (url.endsWith(".mp4")) {
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri videoUri = Uri.parse("http://techslides.com/demos/sample-videos/small.mp4");
intent.setDataAndType( videoUri, "application/x-mpegURL" );
intent.setPackage( "com.mxtech.videoplayer.pro" );
startActivity( intent );
return true;
} else {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
}
}
});
}
}

I am getting Cannot find symbol method startActivityForResult(Intent,int)

I want to stream an URL in the MX video player and in VLC. When the URL ends with comx, the MX Video Player handles the work well but when it ends with covlc, it gives me an error. The error is:
cannot find symbol method startActivityForResult(Intent,int)
Here is the class:
public class MyAppWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String str=new String(url);
if(str.endsWith("comx")){
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
Uri videoUri = Uri.parse(url.replace("?comx", ""));
intent.setClassName("com.mxtech.videoplayer.ad","com.mxtech.videoplayer.ad.ActivityScreen");
intent.setDataAndType(videoUri, "application/x-mpegURL");
intent.putExtra("secure_uri", true);
intent.putExtra("title", "Kung Fury");
intent.setPackage("com.mxtech.videoplayer.ad");
view.getContext().startActivity(intent);
//view.goBack();
return true;
}
else {
if(str.endsWith("covlc")){
Uri uri = Uri.parse(url.replace("?covlc", ""));
int vlcRequestCode = 42;
Intent vlcIntent = new Intent(Intent.ACTION_VIEW);
vlcIntent.setComponent(new ComponentName("org.videolan.vlc", "org.videolan.vlc.gui.video.VideoPlayerActivity"));
vlcIntent.setDataAndType(uri, "video/*");
vlcIntent.putExtra("title", "Kung Fury");
vlcIntent.putExtra("from_start", false);
vlcIntent.putExtra("subtitles_location", "/sdcard/Movies/Fifty-Fifty.srt");
view.getContext().startActivityForResult(vlcIntent, vlcRequestCode);
return true;
}
else {
//Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
//view.getContext().startActivity(intent);
return false;
}
}
}
}
startActivityForResult() is a method on Activity, not Context. Probably the Context returned by getContext() is an Activity, so you can cast it and call startActivityForResult():
((Activity)view.getContext()).startActivityForResult(vlcIntent, vlcRequestCode);
It would be safer to pass the Activity into the MyAppWebViewClient more directly (e.g., constructor parameter) and use it that way.

How can I open play store market:// in webview [duplicate]

I have some links in my webview that are market:// links. When my users tap on them, it gives them a page cannot be found error.
How can I allow all links that begin with market:// to automatically open the Google play store when they are tapped? I tried:
final Intent intent = new Intent("android.intent.action.VIEW");
intent.setData(Uri.parse("market://details?id="));
startActivity(intent);
}
but that didn't seem to do anything. I am pretty new to this so any help would be appreciated. Also, FYI, I cannot change the market:// links to play.google.com myself. They are from my advertiser.
Is there anyway I can include it in this code:
public boolean shouldOverrideUrlLoading(WebView paramWebView, String paramString) {
if (DEBUG)
Log.e("shouldOverride", paramString);
if (Uri.parse(paramString).getHost()!=null && (!Uri.parse(paramString).getHost().equals("market.android.com")) && (!paramString.contains("facebook.com")) && (!Uri.parse(paramString).getHost().contains("twitter.com")) && (!Uri.parse(paramString).getHost().equals("play.google.com"))
&& (!Uri.parse(paramString).getHost().contains("bit.ly")) && (!Uri.parse(paramString).getHost().contains("plus.google.com")) && (!Uri.parse(paramString).getHost().contains("youtube.com"))){
if(isAppOrGamePage(paramString)){
final Intent intent = new Intent(MainActivity.this, PageActivity.class);
intent.putExtra("app_url", paramString);
startActivity(intent);
} else
return false;
} else {
final Intent intent = new Intent("android.intent.action.VIEW", Uri.parse(paramString));
startActivity(intent);
}
return true;
}
}
You can decide what to do by looking the scheme of the url, if Google Play Store app is installed you can open the detail page in Play Store app, else you can show Google Play web page of the application
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getScheme().equals("market")) {
try {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
Activity host = (Activity) view.getContext();
host.startActivity(intent);
return true;
} catch (ActivityNotFoundException e) {
// Google Play app is not installed, you may want to open the app store link
Uri uri = Uri.parse(url);
view.loadUrl("http://play.google.com/store/apps/" + uri.getHost() + "?" + uri.getQuery());
return false;
}
}
return false;
}
});
you can use this code like this also if its help you:
// It will not work in android simulator as it does not have Google Play Store
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id="+APP_ID)));
if (url.startsWith("market://")||url.startsWith("vnd:youtube")||url.startsWith("tel:")||url.startsWith("mailto:"))
{
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
return true;
}

Android new activity not starting

I wrote this code following the skeleton of Reto Meier's "Professional Android 4 Application Development" and some slide of my professor, but i can't understand why the new activity (PreferencesActivity, fully coded) is not starting and is not raising any kind of errors: in the VM it just won't do anything when i press "Preferences" in the standard android menu I created.
I added the new activity in app's manifest correctly (just name, label, theme and screen orientation).
Here's the code
public class MainActivity extends Activity implements OnClickListener, OnValueChangeListener {
static final private int MENU_PREFERENCES = Menu.FIRST+1;
...
#Override
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
menu.add(0, MENU_PREFERENCES, Menu.NONE, "Preferences");
return true;
}
public boolean onOptionsitemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch(item.getItemId()) {
case (MENU_PREFERENCES): {
Intent i = new Intent(this, PreferencesActivity.class);
startActivity(i);
return true;
}
}
return false;
}
...
}
The only strange thing I get is this warning in Logcat
06-20 14:50:49.760: W InputManagerService(699): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy#41219950
You can use both of them
Intent i = new Intent(getApplicationContext(), PreferencesActivity.class);
Intent i = new Intent(MainActivity.this, PreferencesActivity.class);
But it's better to use 1st one because in 2nd one memory leakage problem may occour and also just add this line in your manifest file.
<activity android:name=".PreferencesActivity" />
Your Code :
Intent i = new Intent(this, PreferencesActivity.class);
startActivity(i);
return true;
Instead of this you need to pass MainActivity.this
Intent i = new Intent(MainActivity.this, PreferencesActivity.class);
startActivity(i);
return true;
Issue is Proper context is not passing so its not starting Activity.
Instead of using this you could use getApplicationContext(), it gets you the context of the application object for the currents process.
Try this....
Intent i = new Intent(getApplicationContext(), PreferencesActivity.class);
startActivity(i);
This May Help You..
You need to pass MainActivity
Intent i = new Intent(MainActivity.this, PreferencesActivity.class);
startActivity(i);
return true;
Better to use menu
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Log.d(TAG, "onOptionsItemSelected()");
switch (item.getItemId()) {
case android.R.id.yourId:
finish();
return true;
case R.id.Yourid:
return true;
default:
return super.onOptionsItemSelected(item);
You can also write
startActivity(new Intent(getApplicationContext(),NextActivity.class));
write your activity name in NextActivity.class

How to open browser in android app?

i'm doing a project where i have to call a browser via my android app, but when i call it, the app stops.
the code can be found here: https://github.com/coppetti/android-pulsometer
but for fast view, i have a "Pulsometro" class where
public void onPreviewFrame(byte[] data, Camera cam) {
...
Browser browser = new Browser();
browser.callBrowser(beats);
return;
...
}
and a Browser class where:
public class Browser extends Activity{
public void callBrowser(int beats){
String url = "http://www.higia.info/?q="+beats;
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
return;
}
}
There's a way to call a browser and my app doesn't breaks?
Try this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onOpenWebBrowser(View v)
{
Intent webPageIntent = new Intent(Intent.ACTION_VIEW);
webPageIntent.setData(Uri.parse("https://www.google.co.in/"));
try {
startActivity(webPageIntent);
} catch (ActivityNotFoundException ex) {
}
}
Do not just randomly choose superclasses. Do not just create some subclass of Activity and expect it to work.
Move your callBrowser() method into some real Activity implementation, and get rid of Browser entirely.
Or, remove the superclass from Browser, have callBrowser() take a Context as a parameter, and call startActivity() on that Context.
Try this class. Call the callBrowser method and give it and Activity for the context parameter.
public class Browser{
public void callBrowser(Context context, int beats){
String url = "http://www.higia.info/?q="+beats;
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
context.startActivity(i);
}
}

Categories

Resources