Keep Android application active on screen timeout - android

I'm fairly new to making Android application so I got a question.
If I got my application running and I wait for a few seconds my screen timeouts. The problem is that when I unlock my screen again my application is closed. I want the application to stay active when the screen times out, so that I don't have to start the application all over again.
I don't want the application to run in the background and also I do not want to disable the screen timeout. I just don't want the application to close itself after the screen automatically times out.
Is there any chance of doing so?
_EDIT > Added some of my code.
 
Android Manifest
<supports-screens
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:resizeable="true"
android:anyDensity="true"
/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" />
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".SIOM_AndroidActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden"
android:theme="#android:style/Theme.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.phonegap.DroidGap"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden" >
<intent-filter>
</intent-filter>
</activity>
</application>
</manifest>
 
Android Activity
package com.siom.android;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.phonegap.*;
public class SIOM_AndroidActivity extends DroidGap {
/** Called when the activity is first created. */
/** #Override */
WebView mWebView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.init();
super.clearCache();
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
mWebView.setWebViewClient(new HelloWebViewClient());
mWebView.setWebChromeClient(new MyWebChromeClient());
mWebView.loadUrl("http://www.example.com/test.html");
}
// Maakt een WebChromeClient class aan
final Context myApp = this;
final class MyWebChromeClient extends WebChromeClient {
// Zorgt er voor dat confirm werkt in WebView
#Override
public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {
new AlertDialog.Builder(myApp)
.setTitle("Title here")
.setMessage(message)
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
result.confirm();
}
})
.setNegativeButton(android.R.string.cancel,
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
result.cancel();
}
})
.create()
.show();
return true;
}
// Zorgt er voor dat alert werkt in WebView
#Override
public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
new AlertDialog.Builder(myApp)
.setTitle("Title here")
.setMessage(message)
.setPositiveButton(android.R.string.ok,
new AlertDialog.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
result.confirm();
}
})
.setCancelable(false)
.create()
.show();
return true;
}
}
// Alle links worden in webview geladen
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
// Geeft de back knop een goback actie
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
if (keyCode == KeyEvent.KEYCODE_MENU) {
return true;
}
return super.onKeyDown(keyCode, event);
}
}

There is no direct answer to ur question. Have a look at Figure 1. The activity lifecycle
The concept says that when screen is locked your activity is no longer visible (onPause()). When screen is turned back on onResume() is called. You can use these methods to get the desired results.
Generally, I initialize everything onCreate() of the activity and refresh the values if any onResume().
Hope that helps.

After so many hours, days, weeks and even months trying to solve this problem I finally found it.
If you look at the code I added in my first post you can see the follow two lines of code:
super.init();
super.clearCache();
These two little guys crashed my application over and over and now after I look at it it's no wonder they did. Added these because I was first working with PhoneGap and followed some tutorial somewhere that told me to add these lines. After removing these two lines again my application worked as usual again. Only removing one line didn't work either.
Sometimes programming can really be a hell.

Related

Access camera in android WebView to stream on agora

I am developing a web app which I would now like to make an android app for. For testing, a simple WebView is enough.
Now the issue is that whenever a regular desktop web browser asks for camera and microphone permissions, the app does nothing. It doesn't crash but it also doesn't join the stream.
What do I have to do in order to fix this?
So far, I have the following Code:
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.techadvice">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.CAMERA"></uses-permission>
<uses-permission android:name="android.webkit.PermissionRequest" />
<uses-feature android:name="android.hardware.camera"/>
<application
android:usesCleartextTraffic="true"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java:
package com.example.techadvice;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
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.webview);
WebSettings webSettings=myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setAllowFileAccessFromFileURLs(true);
webSettings.setAllowUniversalAccessFromFileURLs(true);
myWebView.getSettings().setCacheMode(webSettings.LOAD_NO_CACHE);
myWebView.setWebViewClient(new WebViewClient());
myWebView.loadUrl("%some url%");
}
public class mywebClient extends WebViewClient{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon){
super.onPageStarted(view,url,favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view,String url){
view.loadUrl(url);
return true;
}
}
#Override
public void onBackPressed(){
if(myWebView.canGoBack()) {
myWebView.goBack();
}
else{
super.onBackPressed();
}
}
}
I was mostly following this tutorial: https://youtu.be/2cWbepS1NZM
Thanks in advance!
In your onCreate you can check for permission and request it if the permission hasn't been granted already. You can try something like this:
int MY_PERMISSIONS_REQUEST_CAMERA = 0;
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO}, MY_PERMISSIONS_REQUEST_CAMERA);
}

Android GeoLocation doesn't work on device

I'm writing a basic mobile wrapper that connects to a site mainly using Javascript. This site has a login form that tries to access the geolocation of the device in question. When run on an emulator, there is no prompt to allow location (which makes sense in my mind since the PC has a physical location and an external IP address etc) and the GeoLocation just goes through no problem, which to me suggests that the code is at least functional as far as permissions and webView settings go. When run on a device which has location services enabled, even if I'm prompted and allow location the site cannot resolve my location.
The webView and settings are shown below, and the Android Manifest below that.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView wView = (WebView) findViewById(R.id.activity_main_webview);
WebView.setWebContentsDebuggingEnabled(true);
wView.setWebChromeClient(new WebChromeClient(){
#Override
public void onGeolocationPermissionsShowPrompt(final String origin,
final GeolocationPermissions.Callback callback) {
AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
builder.setMessage( origin + " would like to use your current location" );
builder.setNegativeButton( "Don't Allow", new DialogInterface.OnClickListener() {
#Override
public void onClick( DialogInterface dialog, int which ) {
callback.invoke( origin, false, false );
dialog.dismiss();
}
} );
builder.setPositiveButton( "Okay", new DialogInterface.OnClickListener() {
#Override
public void onClick( DialogInterface dialog, int which ) {
callback.invoke( origin, true, false );
dialog.dismiss();
}
});
}
});
wView.getSettings().setJavaScriptEnabled(true);
wView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
wView.getSettings().setGeolocationEnabled(true);
String url = BuildConfig.HOST_URL;
//String postData = "username="+ URLEncoder.encode(user,"UTF-8")+"&password="+URLEncoder.encode(password,"UTF-8");
//Log.e("TEST","postData " + postData);
wView.loadUrl(url);
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.toteireland.app" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.ACCESS_ASSISTED_GPS" />
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="Tote Ireland"
android:theme="#style/AppTheme"
>
<activity
android:name="com.toteireland.app.MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Is that the right place for the permissions?

how to mopub ads add in application

i want to add ads banner in my application i have integrate mopub sdk with my project and import and add library to the my project now my question is how to add banner disply and where code i have to write in my application java code and xml code about ads so please help enyone
my java code and mainifest file code is given below
mainactivity.java
package com.example.ration;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class MainActivity extends Activity {
private WebView web;
int k;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
web=(WebView)findViewById(R.id.web);
web.getSettings().setJavaScriptEnabled(true);
web.setWebViewClient(new WebViewClient());
web.getSettings().setBuiltInZoomControls(true);
web.loadUrl("http://dcs-dof.gujarat.gov.in/live-info.htm");
// web.getProgress();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0,1,menu.NONE,"About");
menu.add(0,2,menu.NONE,"Feedback");
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id=item.getItemId();
if(id == 1)
{
Toast.makeText(MainActivity.this,"About",Toast.LENGTH_LONG).show();
Intent i=new Intent(MainActivity.this,about.class);
startActivity(i);
}
else {
Toast.makeText(MainActivity.this,"Feedback",Toast.LENGTH_LONG).show();
Intent i2 =new Intent(MainActivity.this,feedback.class);
startActivity(i2);
}
return super.onOptionsItemSelected(item);
}
private boolean doubleBackToExitPressedOnce = false;
#Override
protected void onResume() {
super.onResume();
// .... other stuff in my onResume ....
this.doubleBackToExitPressedOnce = false;
}
#Override
public void onBackPressed() {
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
return;
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this,"Press Again to Exit", Toast.LENGTH_SHORT).show();
}
}
and my manifest file is
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ration"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/icon"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.ration.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.example.ration.about"></activity>
<activity android:name="com.example.ration.feedback"></activity>
<activity android:name="com.mopub.mobileads.MoPubActivity" android:configChanges="keyboardHidden|orientation"/>
<activity android:name="com.mopub.mobileads.MraidActivity" android:configChanges="keyboardHidden|orientation"/>
<activity android:name="com.mopub.mobileads.MraidBrowser" android:configChanges="keyboardHidden|orientation"/>
<activity android:name="com.mopub.mobileads.MraidVideoPlayerActivity" android:configChanges="keyboardHidden|orientation"/>
<activity android:name="com.google.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
<activity android:name="com.millennialmedia.android.MMActivity" android:theme="#android:style/Theme.Translucent.NoTitleBar" android:configChanges="keyboardHidden|orientation|keyboard" />
<activity android:name="com.millennialmedia.android.VideoPlayer" android:configChanges="keyboardHidden|orientation|keyboard" />
</application>
</manifest>
I think you should better go through the guide for Mopub Banner Ads Integration which explains you the steps of banner ads integration.
Hope this will help you.
Use view in XML file
<com.mopub.mobileads.MoPubView
android:id="#+id/mrect_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_weight="1" />
in activity Load code
pass id of view in below function ,unit id of ur app,and keywords of cataogry of ads
public void loadMoPubView(MoPubView moPubView, String adUnitId, String keywords) {
if (moPubView == null) {
Utils.logToast(LockScreenActivity.this, "Unable to inflate MoPubView from xml.");
//Toast.makeText(this, "Unable to inflate MoPubView from xml.", Toast.LENGTH_SHORT).show();
return;
}
try {
Utils.validateAdUnitId(adUnitId);
} catch (IllegalArgumentException exception) {
Utils.logToast(LockScreenActivity.this, exception.getMessage());
return;
}
moPubView.setBannerAdListener(this);
moPubView.setAdUnitId(adUnitId);
moPubView.setKeywords(keywords);
moPubView.setAutorefreshEnabled(true);
moPubView.loadAd();
}

NoClassDefFoundError Android Project [duplicate]

This question already has answers here:
Android app crashes after SDK-tools update version (NoClassDefFound, tool version 22)
(9 answers)
Closed 9 years ago.
First of all, I know there are plenty of questions/answers about this topic, I've read most of them but still getting the error:
05-17 02:57:06.522: E/AndroidRuntime(17073): java.lang.NoClassDefFoundError: ar.com.package.android.MainActivity
The project worked just fine until I updated Eclipse from 21 to 22.
I have tried everything I could: I checked the manifest; Cleaned the project; checked my build path, tried the app in different android version, set java compliance level to 1.6 (libraries too), etc. I just can't figure out what the problem is.
Here's my manifest, I couldn't find anything wrong with it:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ar.com.package.android"
android:versionCode="1"
android:versionName="1.0" >
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<permission
android:name="ar.com.package.android.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="ar.com.package.android.permission.MAPS_RECEIVE"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Theme.Sherlock.Light.DarkActionBar"
android:hardwareAccelerated="true">
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="my key"/>
<activity
android:name="ar.com.package.android.SplashScreen"
android:label="#string/app_name"
android:noHistory="true"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Black.NoTitleBar"
android:windowSoftInputMode="stateHidden"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="ar.com.package.android.MainActivity"
android:logo="#drawable/logo"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden"
>
</activity>
<activity
android:name="ar.com.package.android.SearchForm"
android:label="#string/title_activity_search_form"
android:parentActivityName="ar.com.package.android.MainActivity"
>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="ar.com.package.android.MainActivity" />
</activity>
</application>
</manifest>
Any help is welcome. I'll keep researching, if I get the answer I'll post it.
EDIT
Here's the MainActivity code:
package ar.com.package.android;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;
import com.actionbarsherlock.view.*;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu.OnCloseListener;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu.OnOpenListener;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;
#SuppressLint("NewApi")
public class MainActivity extends SlidingFragmentActivity {
private SlidingMenu menu;
private Toast toast;
private long lastBackPressTime = 0;
private GoogleMap map;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// slidemenu
setBehindContentView(R.layout.menu);
setSlidingActionBarEnabled(false);
menu = getSlidingMenu();
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
menu.setShadowWidthRes(R.dimen.shadow_width);
menu.setShadowDrawable(R.drawable.shadow);
menu.setBehindOffset(100);
menu.setFadeDegree(0.35f);
menu.setSlidingEnabled(false);
menu.setOnCloseListener(new OnCloseListener() {
#Override
public void onClose() {
menu.setSlidingEnabled(false);
}
});
menu.setOnOpenListener(new OnOpenListener() {
#Override
public void onOpen() {
menu.setSlidingEnabled(true);
}
});
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
// ---slide menu
map = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
// Getting Google Play availability status
int status = GooglePlayServicesUtil
.isGooglePlayServicesAvailable(getBaseContext());
if (status != ConnectionResult.SUCCESS) {
Toast.makeText(this, "Google Maps no esta disponible.",
Toast.LENGTH_LONG).show();
} else {
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(
-26.8175915814614, -65.22274105834958), 13));
// Enabling MyLocation Layer of Google Map
map.setMyLocationEnabled(true);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getSupportMenuInflater().inflate(R.menu.main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
toggle();
return true;
case R.id.action_search:
final int RESULT = 1;
startActivityForResult(new Intent(MainActivity.this,
SearchForm.class), RESULT);
return true;
case R.id.action_lineas:
showDialogLineas();
return true;
case R.id.action_acercade:
showDialogAcercaDe();
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && !menu.isMenuShowing()) {
if (this.lastBackPressTime < System.currentTimeMillis() - 4000) {
toast = Toast.makeText(this,
"Presione Atrás nuevamente para cerrar",
Toast.LENGTH_LONG);
toast.show();
this.lastBackPressTime = System.currentTimeMillis();
} else {
if (toast != null) {
toast.cancel();
}
super.onBackPressed();
}
return true;
}
return super.onKeyDown(keyCode, event);
}
public void onResultadosClicked(View view) {
showMenu();
}
public void showDialogLineas(){
DialogFragment dialog = new LineasDialog();
dialog.show(getSupportFragmentManager(), "Lineas");
}
public void showDialogAcercaDe(){
DialogFragment dialog = new AcercaDeDialog();
dialog.show(getSupportFragmentManager(), "Acerca");
}
}
Try going to Project -> Properties -> Java Build Path -> Order & Export and ensure Android Private Libraries are checked for your project and for all other library projects you are using.

How to hide android status bar just for my application on Android Tabs2 fro Android version4.0.3?

Can any one please tell me any way to hide status bar completely just for my application on all activities. My application only runs on android tabs with 4.0.3 Android. I actually want to remove the back, home and all the other click listeners on the status bar or just completely hide it. The requirement behind this is that if once my application starts on a tab it stays on until user switches off the device device and there should not be any exit point. I will be grateful for your humble response. I have added android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" manifest.xml.Still not able to disable the status bar. My activity class is given below
package com.plugin.myapp;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.CordovaWebViewClient;
import org.apache.cordova.DroidGap;
import org.apache.cordova.CordovaChromeClient;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView;
public class MainActivity extends DroidGap {
private static MainActivity instance;
// private WebView mWebView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.init();
//getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
getWindow().getDecorView().setSystemUiVisibility(View.STATUS_BAR_HIDDEN);
super.loadUrl("file:///android_asset/www/tdc_tutorial.html");
}
// handler for the background updating
Handler progressHandler = new Handler()
{
public void handleMessage(Message msg)
{
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
};
#Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
System.out.println("onConfigurationChanged");
}
#Override
public void init() {
super.init((CordovaWebView) new WebView(this), new GWTCordovaWebViewClient(this), new CordovaChromeClient(this));
}
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
//super.onBackPressed();
}
/* (non-Javadoc)
* #see org.apache.cordova.DroidGap#onConfigurationChanged(android.content.res.Configuration)
*/
}
and my manifest.xml is given below
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.plugin.myapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" />
<uses-permission android:name="android.permission.INTERNET" />
<supports-screens
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:xlargeScreens="true"
android:resizeable="true"
android:anyDensity="true"
/>
<application
android:icon="#drawable/ic_launcher"
android:label="Android Test"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main"
android:screenOrientation="sensorLandscape"
android:configChanges="orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
For devices without hardware key, unless you "Root" device, there's no way to completely hide the bar. You can, however, put it in "low profile" mode using setSystemUIVisiblity.
Take a look at GMD HideBar for ICS.
I used it on my 4.0.3 tablet and it works very good! if you use dex2jar to get jar from apk file, I could import jar file in your buildpath and use function SystemUI.hidebar to hide or SystemUI.showbar to show :)
By default, you shouldn`t remove the statusbar, because it is a service from the Android UI. Then Google let you hide it, however if the user swipes it up, it will come back.
But, yes it is possible to do if you have root access on the device.
This code can hide and show the StatusBar by killing it`s proccess and calling it back again.
package com.example.statusbar;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
public class MainActivity extends Activity {
String commandToExecute;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
commandToExecute = "/system/xbin/su";
executeShellCommand(commandToExecute);
Button btHideStatusBar = (Button) findViewById(R.id.buttonHide);
Button btShowStatusBar = (Button) findViewById(R.id.buttonShow);
btHideStatusBar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
commandToExecute = "/system/xbin/su -c /system/bin/service call activity 42 s16 com.android.systemui";
executeShellCommand(commandToExecute);
}
});
btShowStatusBar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
commandToExecute = "/system/xbin/su -c /system/bin/am startservice -n com.android.systemui/.SystemUIService";
executeShellCommand(commandToExecute);
}
});
}
private boolean executeShellCommand(String command) {
try {
Runtime.getRuntime().exec(command);
return true;
} catch (Exception e) {
return false;
}
}
}

Categories

Resources