[Q] Runtime.getRuntime().exec dosn't work - Android Q&A, Help & Troubleshooting

Hi i'm trying to make an app with a button that free cache of the phone.. This is the code:
Code:
package com.mkyong.android;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import java.io.IOException;
import com.example.toast.R;
public class MainActivity extends Activity {
private Button button;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab1);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@SuppressLint("SdCardPath")
@Override
public void onClick(View arg0) {
final Runtime runtime = Runtime.getRuntime();
try {
runtime.exec("su -c 'echo 3 > /proc/sys/vm/drop_caches'");
Toast.makeText(MainActivity.this, "Script lanciato con `successo, memoria svuotata.", Toast.LENGTH_LONG).show();`
}
catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
Well i've got no errors but nothing happen.. When i press the button the free ram memory rest the same. (to see the free memory i open the terminal emulator and i write the "free" command"). Someone can help me? What's wrong?

Related

[Q] [Dev] App Crashes instantly [problem solved]

Whats Wron with this? If I start the app it crashes instantly
It's only a Test app the name only is about what I plan to do in the future^^
Code:
package de.cwsurf.sorry.improvedBattery;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class ImprovedBatteryActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText eT = (EditText) findViewById(R.id.editText1);
eT.setText("Test");
eT.setText(eT.getText()+"\nTest1\nTest2\nTest3\nTest4");
setContentView(eT);
final Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
EditText eT = (EditText) findViewById(R.id.editText1);
eT.setText(eT.getText()+"\nTest1\nTest2\nTest3\nTest4");
setContentView(eT);
}
});
}
}
problem solved. i only need to leave this away: setContentView(eT);

[Q] Creating Translator App

Hi all, ive just started lookin up to Java and creating app, i know i should start and improove slowly, but i need quickly some helps with makin a Translator.apk using Bing API translator, cause google one are now only aviable if u pay them.
i saw a litle exemple where i downloaded the Jar file(which i put it in /libs and mine came out like this:
import com.memetix.mst.language.Language;
import com.memetix.mst.translate.Translate;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements OnClickListener {
EditText in, out;
Button trns;
String inPut;
@Override
protected void onCreate() {
// TODO Auto-generated method stub
Translate.setClientId("hidden");
Translate.setClientSecret("hidden");
super.onCreate();
setContentView(R.layout.activity_main);
trns = (Button) findViewById(R.id.button1);
in = (EditText) findViewById(R.id.editText1);
out = (EditText) findViewById(R.id.editText2);
inPut = in.getText().toString();
trns.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
String translatedText = Translate.execute(inPut, Language.GERMAN,
Language.FRENCH);
out.setText(translatedText);
} catch (Exception e) {
e.printStackTrace();
}
}
}
problaby its all wrong i know, thats why i need help
Guess i have to add Internet permission to the manifest(how ? and do i have to make some controll after?)
I studied at school some basic C# so im used to the grouBox which i didnt find with eclipse. What's the nearest thing with it where i can put a list of all selectable language to translate?
Hope somebody could help me, ty

[Q] Need help developing Web Browser usng WebView

Hello guys, im sorry if this is not the right section, please move if necessary.
Im developing my own Chrome like Web Browser in Eclipse using WebView, so far its just a TextEdit that act as a url bar, a GO button to take WebView to whatever URL is typed, and of course the WebView showing this website, now i have some issues, and i been up and down everywhere and cant find the answers for these issues, maybe someone here can shed some light.
1- when i go lets say msn.com, it goes fine but if i click -- news -- , it goes fine too, but the "URL bar" still show msn.com , it doesn't show the current URL im in, so its there a way to write to to the TextEdit or create a real url bar?
2- when i go to YouTube, if i hit play on a video it plays fine on WebView, but if i turn the device sideways, video shows liek to hit play again but you can still hear it in the background, if i hit play it just stop and wont play again any other videos, ( i found the YouTube API player and i followed what supposedly is how to use it but it never worked, i cant really find instructions on how to merge it to the app, supposedly just moved the .jar file but still playing in the browser) or how can i do in webview so when a user press play it ask you want to use browser or you want to launch youtube app, cant manage how to set up in intent for this activity.
Or how can i lunch youtube app from a button on my layout?
3- How to make android , in this case 4.0.4 recognize my app as a browser so whenever any app ask me to launch a browser, mine is on the list.
4- on my case ( galaxy s2 ) in chrome when i press menu, it pop this menu, on my app, menu key pop a little menu with settings only, but nothing happens ( of course ), how can i add items to this main menu, like About, Contact Me, Back , Forward and add stuff in settings too?
5- launch an app from a button on the app.
I know its a lot but any help would be greatly appreciated it
I'm not an expert, but i'll try to answer your questions...
cubanito08 said:
1- when i go lets say msn.com, it goes fine but if i click -- news -- , it goes fine too, but the "URL bar" still show msn.com , it doesn't show the current URL im in, so its there a way to write to to the TextEdit or create a real url bar?
Click to expand...
Click to collapse
Get the url for the current page using getURL() and set it as content to your URL Bar (using its setText method).
cubanito08 said:
2- ... or how can i do in webview so when a user press play it ask you want to use browser or you want to launch youtube app, cant manage how to set up in intent for this activity.
Click to expand...
Click to collapse
Check here.
cubanito08 said:
3- How to make android , in this case 4.0.4 recognize my app as a browser so whenever any app ask me to launch a browser, mine is on the list.
Click to expand...
Click to collapse
Add this to your AndroidManifest:
Code:
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="http"/>
</intent-filter>
Source.
cubanito08 said:
4- ... how can i add items to this main menu, like About, Contact Me, Back , Forward and add stuff in settings too?
Click to expand...
Click to collapse
Check this, a well-explained tutorial.
cubanito08 said:
5- launch an app from a button on the app.
Click to expand...
Click to collapse
Add a listener to your button, and launch and intent. For example, to launch YouTube:
Code:
final Button YouTube = (Button) findViewById(R.id.yourButtonID);
YouTube.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent();
PackageManager manager = getPackageManager();
i = manager.getLaunchIntentForPackage("com.google.android.youtube"); //com.google.android.youtube is the Package Name for youtube
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
}
});
Source.
How can you get the package name for other apps? It depends of the app. If it is hosted in Play Store, it's too easy: go to Play Store and search the app. Check the url, will be something like this:
Code:
https://play.google.com/store/apps/details?id=com.google.android.youtube&feature=nav_result...
The text between id= and &feature is the package name. So easy as that.
Otherwise, if you wish to open a system app (settings, stock browser, or something like that) I recommend you to use DDMS (it's a debug app included in Android SDK. It's located at AndroidSDKPath\tools). Simply open it with your phone connected, and open any app from your phone. DDMS will show you some debug info, included the package name of the open apps.
thanks rober is been really helpful, but one question where do i set the geturl() on my main.java? also for the youtube i tried those and i cant manage, can you take a look at my main.java please?
package com.foka.icecold;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.FactoryConfigurationError;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.MediaController;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.VideoView;
import android.widget.LinearLayout.LayoutParams;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;
@SuppressWarnings("unused")
public class Main extends Activity implements View.OnClickListener{
private static final String TAG = "my webview 2";
private WebView mWebView;
EditText url; ------------------------------------------------------------ this is my " url bar"
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.i(TAG, "onCreate");
String extra = getIntent().getStringExtra("VideosId");
mWebView = (WebView) findViewById(R.id.mywebview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setUseWideViewPort(true);
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setSupportZoom(true);
mWebView.getSettings().setPluginsEnabled(true);
mWebView.getSettings().setPluginState(PluginState.ON);
mWebView.loadUrl("http://www.google.com");
mWebView.setWebViewClient(new IceColdClient());
mWebView.getTouchables();
url = (EditText) findViewById(R.id.web);
Button go = (Button) findViewById(R.id.go);
Button Back = (Button) findViewById(R.id.back);
Button foward = (Button) findViewById(R.id.foward);
Button refresh = (Button) findViewById(R.id.refresh);
Button test = (Button) findViewById(R.id.testbutton);
Button fb = (Button) findViewById(R.id.fb);
Button tw = (Button) findViewById(R.id.tw);
Button yt = (Button) findViewById(R.id.yt);
Button map = (Button) findViewById(R.id.maps);
Button weat = (Button) findViewById(R.id.weather);
Button news = (Button) findViewById(R.id.news);
go.setOnClickListener(this);
Back.setOnClickListener(this);
foward.setOnClickListener(this);
refresh.setOnClickListener(this);
test.setOnClickListener(this);
fb.setOnClickListener(this);
tw.setOnClickListener(this);
yt.setOnClickListener(this);
map.setOnClickListener(this);
weat.setOnClickListener(this);
news.setOnClickListener(this);
--------------------------------------------------------------------------- i tried inserting here the intent here for youtube app but it launches the youtube app when i launch my app
final Activity activity = this;
mWebView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
// Activities and WebViews measure progress with different scales.
// The progress meter will automatically disappear when we reach 100%
activity.setProgress(progress * 1000);
}
});
mWebView.setWebViewClient(new WebViewClient());
mWebView.setWebViewClient(new WebViewClient());
}
public void onClick(View v) {
switch (v.getId()){
case R.id.go:
String theWebsite = url.getText().toString();
if(!theWebsite.startsWith("www.")&& !theWebsite.startsWith("http://")){
theWebsite = "www."+theWebsite;
}
if(!theWebsite.startsWith("http://")){
theWebsite = "http://"+theWebsite;
mWebView.loadUrl(theWebsite);
}
break;
case R.id.back:
if (mWebView.canGoBack())
mWebView.goBack();
break;
case R.id.foward:
if (mWebView.canGoForward())
mWebView.goForward();
break;
case R.id.refresh:
mWebView.reload();
break;
case R.id.testbutton:
mWebView.loadUrl("http://www.google.com");
break;
case R.id.fb:
mWebView.loadUrl("http://www.facebook.com");
break;
case R.id.tw:
mWebView.loadUrl("http://www.twitter.com");
break;
case R.id.yt:
mWebView.loadUrl("http://www.youtube.com");
break;
case R.id.maps:
mWebView.loadUrl("http://maps.google.com/");
break;
case R.id.weather:
mWebView.loadUrl("http://www.weather.com/");
break;
case R.id.news:
mWebView.loadUrl("http://www.msn.com/");
break;
}
}
private class IceColdClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView webview, String url)
{
webview.loadUrl(url);
return true;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack())
{
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
protected void onStart()
{
super.onStart();
Log.i(TAG, "onStart");
}
@Override
protected void onRestart()
{
super.onStart();
Log.i(TAG, "onRestart");
}
@Override
protected void onPause()
{
super.onPause();
Log.i(TAG, "onPause");
}
@Override
protected void onResume()
{
super.onResume();
Log.i(TAG, "onResume");
}
@Override
protected void onDestroy()
{
super.onDestroy();
Log.i(TAG, "onDestroy");
}
@Override
protected void onStop()
{
super.onStop();
Log.i(TAG, "onStop");
}
@Override
protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
mWebView.saveState(outState);
Log.i(TAG, "onSaveInstanceState");
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
mWebView.restoreState(savedInstanceState);
Log.i(TAG, "onRestoreInstanceState");
}
public static boolean isAppInstalled(String uri, Context context) {
PackageManager pm = context.getPackageManager();
boolean installed = false;
try {
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
installed = true;
} catch (PackageManager.NameNotFoundException e) {
installed = false;
}
return installed;
}
}
Click to expand...
Click to collapse
Ok, here you should put the code to open Youtube:
Code:
case R.id.yt:
//mWebView.loadUrl("http://www.youtube.com");
//Put here the intent
break;
And... I'm not sure, but I think you can use getURL() and set the result to your URL Bar here:
Code:
public void onProgressChanged(WebView view, int progress) {
// Activities and WebViews measure progress with different scales.
// The progress meter will automatically disappear when we reach 100%
activity.setProgress(progress * 1000);
//getURL()... and that stuff here.
}
thanks , i remove all case R.id.xx sicne they were buttons on the layout that looked horrible i just rather wait for user to go to the site and launch the intent when press play, thats where my issues is, and for the geturl() i cant get it how do i write it to the EditText overriding whatever is there already typed by user?
this is my new java and thanks to u i manage to create the menu
package com.foka.icecold;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.FactoryConfigurationError;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.MediaController;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.VideoView;
import android.widget.LinearLayout.LayoutParams;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
@SuppressWarnings("unused")
public class Main extends Activity implements View.OnClickListener{
private static final String TAG = "my webview 2";
private WebView mWebView;
EditText url;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.i(TAG, "onCreate");
String extra = getIntent().getStringExtra("VideosId");
mWebView = (WebView) findViewById(R.id.mywebview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setUseWideViewPort(true);
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setSupportZoom(true);
mWebView.getSettings().setPluginsEnabled(true);
mWebView.getSettings().setPluginState(PluginState.ON);
mWebView.loadUrl("http://www.google.com");
mWebView.setWebViewClient(new IceColdClient());
mWebView.getTouchables();
url = (EditText) findViewById(R.id.web);
Button go = (Button) findViewById(R.id.go);
Button home = (Button) findViewById(R.id.home);
go.setOnClickListener(this);
home.setOnClickListener(this);
final Activity activity = this;
mWebView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
// Activities and WebViews measure progress with different scales.
// The progress meter will automatically disappear when we reach 100%
activity.setProgress(progress * 1000);
}
});
mWebView.setWebViewClient(new WebViewClient()()
mWebView.setWebViewClient(new WebViewClient());
}
public void onClick(View v) {
switch (v.getId()){
case R.id.go:
String theWebsite = url.getText().toString();
if(!theWebsite.startsWith("www.")&& !theWebsite.startsWith("http://")){
theWebsite = "www."+theWebsite;
}
if(!theWebsite.startsWith("http://")){
theWebsite = "http://"+theWebsite;
mWebView.loadUrl(theWebsite);
}
break;
case R.id.home:
mWebView.loadUrl("http://www.google.com");
break;
}
}
private class IceColdClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView webview, String url)
{
webview.loadUrl(url);
return true;
}
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.about_menuitem:
startActivity(new Intent(this, About.class));
return true;
case R.id.help_menuitem:
startActivity(new Intent(this, Help.class));
return true;
case R.id.foward_menuitem:
if (mWebView.canGoForward())
mWebView.goForward();
return true;
case R.id.refresh_menuitem:
mWebView.reload();
return true;
case R.id.home_menuitem:
mWebView.loadUrl("http://www.google.com");
return true;
case R.id.back_menuitem:
mWebView.goBack();
return true;
case R.id.clear_menuitem:
mWebView.clearHistory();
return true;
}
return true;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack())
{
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
protected void onStart()
{
super.onStart();
Log.i(TAG, "onStart");
}
@Override
protected void onRestart()
{
super.onStart();
Log.i(TAG, "onRestart");
}
@Override
protected void onPause()
{
super.onPause();
Log.i(TAG, "onPause");
}
@Override
protected void onResume()
{
super.onResume();
Log.i(TAG, "onResume");
}
@Override
protected void onDestroy()
{
super.onDestroy();
Log.i(TAG, "onDestroy");
}
@Override
protected void onStop()
{
super.onStop();
Log.i(TAG, "onStop");
}
@Override
protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
mWebView.saveState(outState);
Log.i(TAG, "onSaveInstanceState");
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
mWebView.restoreState(savedInstanceState);
Log.i(TAG, "onRestoreInstanceState");
}
public static boolean isAppInstalled(String uri, Context context) {
PackageManager pm = context.getPackageManager();
boolean installed = false;
try {
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
installed = true;
} catch (PackageManager.NameNotFoundException e) {
installed = false;
}
return installed;
}
}
Click to expand...
Click to collapse
Something like this:
Code:
...
activity.setProgress(progress * 1000);
url.setText(mWebView.getURL()) //You can use getOriginalURL() instead getURL()
}
...
//If that works, but you can't edit the URL anymore, use this:
...
activity.setProgress(progress * 1000);
url.setText(mWebView.getURL(),TextView.BufferType.EDITABLE)
}
...
ill try it when i get home, this is driving me crazy, also the youtube thing, video even play fine in webview but when u turn it landscape then it goes crazy, so i want to give the user the chance to choose either youtube app or browser but that they can turn and video either stop and let them play again or that keep playing, but i cant find where to insert the intent for it and how does it knows the user pressed play on the video
Yeah, looks difficult (at least to me).
I have an idea (pretty elegant... haha ). You can analyze the URL to know if there is a video on the screen. If so, add a new option in the Menu (something like Play on Youtube App), so the user can play the video directly on your browser, or using the Youtube app.
omg thanks so much url works fine now !!!!!!!!!!!!. but lets say if i input www.yahoo.com , while the browser go there it replaces the text with the current url www.google.com while it load the next one, anyway to stop this from happening so when i write yahoo.com it stays there and only override when page change, cause sometimes i try to write a url and since current page still something loading it writes me back the url of the current page, its so annoying, i just want to write the url when the page start loading , not when its loading to a new one too
i have this
WebView web=new WebView(this);
web.setWebViewClient(new WebViewClient(){
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
// can get Here
view.getUrl();
//Now set to TExtView
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
// can get Here
view.getUrl();
//Now set to TExtView
}
});
Click to expand...
Click to collapse
but i cant fidn right way to incorporate it into my java and only when page start not when page finished
if i do this
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
// can get Here
mwebview.getUrl();
url.setText(mWebView.getUrl());
[/COLOR]
i get setText error The method setText(String) is undefined for the type String
}
Click to expand...
Click to collapse
last 2 question and ill leave u alone for tonite lol, if im goign to insert a new piece of code , lets say the youtube intent or something else for a browser how do i know im inserting it in the right part fo the java file or should i keep going at the end, and is it possible to turn stuff on and off liek add to a menu the option to turn setJavaScriptEnabled(true) or false so the user can decide if they want it or not ?
Your code is giving error because onPageStarted has an argument named as your URL Bar (url), simply rename that argument and the error will be gone. Even better, you can use that argument to get the URL, without getUrl(), like this:
Code:
public void onPageStarted(WebView view, String URL, Bitmap favicon) {
super.onPageStarted(view, URL, favicon);
url.setText(URL); //Note the change in the argument's name (url to URL)
}
cubanito08 said:
...if im goign to insert a new piece of code , lets say the youtube intent or something else for a browser how do i know im inserting it in the right part fo the java file or should i keep going at the end...
Click to expand...
Click to collapse
While your code doesn't give you errors... don't matter the location haha
cubanito08 said:
and is it possible to turn stuff on and off liek add to a menu the option to turn setJavaScriptEnabled(true) or false so the user can decide if they want it or not ?
Click to expand...
Click to collapse
Sure. I think you mean an options menu as many other apps, right? Those menus are done using the SharedPreferences (relatively easy, the API makes almost all the job, you only need to recover the preferences values, and apply them to your app).
but im using WebChromeClient so i can run javascripts like youtube player so PageStarted doesnt show under it... so for some reason i dont know if PageStarted can be incorporated on it or if it has to be a regula WebViewClient only ?
this video show one of the issues
http://www.youtube.com/watch?v=K5DtJJ5zOe0
as you can see main page is google.com, then when i type yahoo.com , it writes google.com again then it goes to yahoo.com, then when i load msn sincew my signal is crap it takes a while to load and it wont let me type another website since it keep writing the url nonstop until site finish loading.
this is my new main.java, as you see i nthe red part i have a chromeclient and geturl and it get the url as u see in the video but i cant find how to set it to just pagestart since chromeclient doesnt let me add it
package com.foka.icecold;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.app.Activity;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.FactoryConfigurationError;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.MediaController;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.VideoView;
import android.widget.LinearLayout.LayoutParams;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
@SuppressWarnings("unused")
public class Main extends Activity implements View.OnClickListener{
private static final String TAG = "my webview 2";
private WebView mWebView;
EditText url;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.i(TAG, "onCreate");
String extra = getIntent().getStringExtra("VideosId");
mWebView = (WebView) findViewById(R.id.mywebview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setUseWideViewPort(true);
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setSupportZoom(true);
mWebView.getSettings().setPluginsEnabled(true);
mWebView.getSettings().setPluginState(PluginState.ON);
mWebView.loadUrl("http://www.google.com");
mWebView.setWebViewClient(new IceColdClient());
mWebView.getTouchables();
url = (EditText) findViewById(R.id.web);
Button go = (Button) findViewById(R.id.go);
go.setOnClickListener(this);
final Activity activity = this;
mWebView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
// Activities and WebViews measure progress with different scales.
// The progress meter will automatically disappear when we reach 100%
activity.setProgress(progress * 1000);
url.setText(mWebView.getUrl());
}
});
}
public void onClick(View v) {
switch (v.getId()){
case R.id.go:
String theWebsite = url.getText().toString();
if(!theWebsite.startsWith("www.")&& !theWebsite.startsWith("http://")){
theWebsite = "www."+theWebsite;
}
if(!theWebsite.startsWith("http://")){
theWebsite = "http://"+theWebsite;
mWebView.loadUrl(theWebsite);
}
break;
}
}
private class IceColdClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView webview, String url)
{
webview.loadUrl(url);
return true;
}
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.about_menuitem:
startActivity(new Intent(this, About.class));
return true;
case R.id.help_menuitem:
startActivity(new Intent(this, Help.class));
return true;
case R.id.foward_menuitem:
if (mWebView.canGoForward())
mWebView.goForward();
return true;
case R.id.refresh_menuitem:
mWebView.reload();
return true;
case R.id.home_menuitem:
mWebView.loadUrl("http://www.google.com");
return true;
case R.id.back_menuitem:
mWebView.goBack();
return true;
case R.id.clear_menuitem:
mWebView.clearHistory();
return true;
}
return true;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack())
{
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
protected void onStart()
{
super.onStart();
Log.i(TAG, "onStart");
}
@Override
protected void onRestart()
{
super.onStart();
Log.i(TAG, "onRestart");
}
@Override
protected void onPause()
{
super.onPause();
Log.i(TAG, "onPause");
}
@Override
protected void onResume()
{
super.onResume();
Log.i(TAG, "onResume");
}
@Override
protected void onDestroy()
{
super.onDestroy();
Log.i(TAG, "onDestroy");
}
@Override
protected void onStop()
{
super.onStop();
Log.i(TAG, "onStop");
}
@Override
protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
mWebView.saveState(outState);
Log.i(TAG, "onSaveInstanceState");
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
mWebView.restoreState(savedInstanceState);
Log.i(TAG, "onRestoreInstanceState");
}
public static boolean isAppInstalled(String uri, Context context) {
PackageManager pm = context.getPackageManager();
boolean installed = false;
try {
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
installed = true;
} catch (PackageManager.NameNotFoundException e) {
installed = false;
}
return installed;
}
}
Click to expand...
Click to collapse

[Q] maintain video when rotating the screen in android

I need help in this moment..
my code .
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore.Video;
import android.view.KeyEvent;
import android.view.Window;
import android.widget.MediaController;
import android.widget.ProgressBar;
import android.widget.Toast;
import android.widget.VideoView;
@suppressLint("HandlerLeak")
public class Activity_Splash extends Activity {
ProgressBar progressBar;
protected boolean isRunning;
VideoView myVideoView;
String SrcPath = "/storage/sdcard0/Video/Reik-ConlaCaraenAlto_HD.mp4";
@override
public void onCreate(Bundle savedInstanceState){
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
VideoView myVideoView = (VideoView)findViewById(R.id.videoView1);
myVideoView.setVideoPath(SrcPath);
myVideoView.setMediaController(new MediaController(this));
myVideoView.requestFocus();
myVideoView.start();
new Thread(new Runnable() {
public void run() {
for (int i = 0; i < 20; i++) {
try {
Thread.sleep(1000);
handler.sendMessage(handler.obtainMessage());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Intent mainIntent = new Intent().setClass(Activity_Splash.this, Activity_Home.class);
startActivity(mainIntent);
finish();
}
}).start();
}
Handler handler=new Handler() {
@override
public void handleMessage(Message msg) {
progressBar.incrementProgressBy(5);
}
};
@override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// Bloquiar la tecla volver para no cerrar el app
Toast.makeText(this, "No puede Retroceder", Toast.LENGTH_SHORT).show();
return false;
}
return super.onKeyDown(keyCode, event);
}
}
how maintain the data when rotate the screen???

Firebase authentication by different user types

I am new to noSQL database systems. In this app, there are two types of users (Members and Admins) with two different UI. I am using firebase email authentication to register and validating users. Now, Members can register and log in to their respective interface. But how to define admins in firebase and send admins to their interfaces? Thanks
Member login:
Code:
package com.*******************;
import android.app.ProgressDialog;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
public class LoginActivity extends AppCompatActivity {
private EditText txtEmailLogin;
private EditText txtPasswordLogin;
private FirebaseAuth firebaseAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
txtEmailLogin= findViewById(R.id.etLoginEmail);
txtPasswordLogin= findViewById(R.id.etLoginPassword);
firebaseAuth=FirebaseAuth.getInstance();
}
public void btnLogin_Click(View v){
final ProgressDialog progressDialog=ProgressDialog.show(LoginActivity.this, "Please Wait...", "Processing...",true);
(firebaseAuth.signInWithEmailAndPassword(txtEmailLogin.getText().toString(), txtPasswordLogin.getText().toString()))
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
progressDialog.dismiss();
if (task.isSuccessful()){
Intent i=new Intent(LoginActivity.this, LastOrder.class);
startActivity(i);
}else{
//Log e("ERROR", task.getException().toString());
Toast.makeText(LoginActivity.this, task.getException().getMessage(),Toast.LENGTH_LONG).show();
}
}
});
}
public void registrationLink_Click(View v){
Intent i=new Intent(LoginActivity.this,RegistrationActivity.class);
startActivity(i);
}
}
Member registration:
Code:
package com.*******************;
import android.app.ProgressDialog;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
public class RegistrationActivity extends AppCompatActivity {
private EditText txtEmail;
private EditText txtPassword;
private EditText txtConfirmPassword;
private FirebaseAuth firebaseAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration);
txtEmail = findViewById(R.id.etEmail);
txtPassword = findViewById(R.id.etPassword);
txtConfirmPassword=findViewById(R.id.etConfirmPassword);
firebaseAuth = FirebaseAuth.getInstance();
}
public void btnSubmit_Click(View v) {
final ProgressDialog progressDialog = ProgressDialog.show(RegistrationActivity.this, "Please Wait...", "Processing...", true);
(firebaseAuth.createUserWithEmailAndPassword(txtEmail.getText().toString(), txtPassword.getText().toString()))
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
progressDialog.dismiss();
if (task.isSuccessful()) {
Toast.makeText(RegistrationActivity.this, "Registration Successful", Toast.LENGTH_LONG).show();
Intent i = new Intent(RegistrationActivity.this, LoginActivity.class);
startActivity(i);
} else {
//Log e("ERROR", task.getException().toString());
Toast.makeText(RegistrationActivity.this, task.getException().getMessage(), Toast.LENGTH_LONG).show();
}
}
});
}
public void btnCancel_Click(View v){
Intent i=new Intent(RegistrationActivity.this, LoginActivity.class);
startActivity(i);
}
}

Categories

Resources